Add support for empty enum variants and enum variants that start with a digit

This commit is contained in:
Anton Danilkin 2018-08-04 00:19:06 +03:00 committed by Alex Crichton
parent 61b3d52dc9
commit 07b4ef5838
9 changed files with 9 additions and 2 deletions

View File

@ -30,6 +30,8 @@ pub fn rust_ident(name: &str) -> Ident {
panic!("tried to create empty Ident (from \"\")");
} else if is_rust_keyword(name) {
Ident::new(&format!("{}_", name), proc_macro2::Span::call_site())
} else if name.chars().next().unwrap().is_ascii_digit() {
Ident::new(&format!("N{}", name), proc_macro2::Span::call_site())
} else {
raw_ident(name)
}

View File

@ -1 +0,0 @@
These WebIDL files have enum variants that are not valid rust Idents, for example "2x", or "".

View File

@ -720,7 +720,13 @@ impl<'a> WebidlParse<()> for webidl::ast::Enum {
variants: self
.variants
.iter()
.map(|v| rust_ident(camel_case_ident(&v).as_str()))
.map(|v|
if !v.is_empty() {
rust_ident(camel_case_ident(&v).as_str())
} else {
rust_ident("None")
}
)
.collect(),
variant_values: self.variants.clone(),
rust_attrs: vec![parse_quote!(#[derive(Copy, Clone, PartialEq, Debug)])],