diff --git a/xsd-parser/src/generator/enum.rs b/xsd-parser/src/generator/enum.rs index d314e72d..cfd48270 100644 --- a/xsd-parser/src/generator/enum.rs +++ b/xsd-parser/src/generator/enum.rs @@ -19,7 +19,7 @@ pub trait EnumGenerator { ); format!( - "{comment}{macros}\n\ + "{comment}{macros}\ pub enum {name} {{\n\ {cases}\n\ {indent}__Unknown__({typename}),\n\ @@ -61,22 +61,24 @@ pub trait EnumGenerator { } fn macros(&self, entity: &Enum, gen: &Generator) -> Cow<'static, str> { + let allows = "#[allow(non_camel_case_types, clippy::upper_case_acronyms)]\n"; + if entity.source == EnumSource::Union { - return "#[derive(PartialEq, Debug, UtilsUnionSerDe)]".into(); + return format!("{allows}#[derive(PartialEq, Debug, UtilsUnionSerDe)]").into(); } - let derives = "#[derive(PartialEq, Debug, YaSerialize, YaDeserialize)]"; + let derives = "#[derive(PartialEq, Debug, YaSerialize, YaDeserialize)]\n"; let tns = gen.target_ns.borrow(); match tns.as_ref() { Some(tn) => match tn.name() { Some(name) => format!( - "{derives}#[yaserde(prefix = \"{prefix}\", namespace = \"{prefix}: {uri}\")]\n", + "{allows}{derives}#[yaserde(prefix = \"{prefix}\", namespace = \"{prefix}: {uri}\")]\n", derives = derives, prefix = name, uri = tn.uri() ), None => format!( - "{derives}#[yaserde(namespace = \"{uri}\")]\n", + "{allows}{derives}#[yaserde(namespace = \"{uri}\")]\n", derives = derives, uri = tn.uri() ), diff --git a/xsd-parser/src/generator/enum_case.rs b/xsd-parser/src/generator/enum_case.rs index a5580db8..bd99621d 100644 --- a/xsd-parser/src/generator/enum_case.rs +++ b/xsd-parser/src/generator/enum_case.rs @@ -1,8 +1,10 @@ use crate::{ - generator::{default::default_format_type, utils::split_name, Generator}, + generator::{utils::split_name, Generator}, parser::types::{EnumCase, EnumSource}, }; +use super::utils::{filter_type_name, sanitize}; + pub trait EnumCaseGenerator { fn generate(&self, entity: &EnumCase, gen: &Generator) -> String { let typename = if entity.type_name.is_some() { @@ -20,12 +22,8 @@ pub trait EnumCaseGenerator { ) } - fn get_name(&self, entity: &EnumCase, gen: &Generator) -> String { - default_format_type(entity.name.as_str(), &gen.target_ns.borrow()) - .split("::") - .last() - .unwrap() - .to_string() + fn get_name(&self, entity: &EnumCase, _: &Generator) -> String { + sanitize(filter_type_name(entity.name.split(':').last().unwrap_or(&entity.name.to_owned()))) } fn get_type_name(&self, entity: &EnumCase, gen: &Generator) -> String { diff --git a/xsd-parser/tests/enumeration/expected.rs b/xsd-parser/tests/enumeration/expected.rs index c74af0e3..e9995eb9 100644 --- a/xsd-parser/tests/enumeration/expected.rs +++ b/xsd-parser/tests/enumeration/expected.rs @@ -1,12 +1,10 @@ +#[allow(non_camel_case_types, clippy::upper_case_acronyms)] #[derive(PartialEq, Debug, YaSerialize, YaDeserialize)] #[yaserde(prefix = "tns", namespace = "tns: http://example.com")] pub enum FooType { - #[yaserde(rename = "OFF")] - Off, - #[yaserde(rename = "ON")] - On, - #[yaserde(rename = "AUTO")] - Auto, + OFF, + ON, + AUTO, __Unknown__(String), } @@ -17,9 +15,24 @@ impl Default for FooType { } impl Validate for FooType {} +#[allow(non_camel_case_types, clippy::upper_case_acronyms)] +#[derive(PartialEq, Debug, YaSerialize, YaDeserialize)] +#[yaserde(prefix = "tns", namespace = "tns: http://example.com")] +pub enum FooType1 { + OFF, + ON, + On, + __Unknown__(String), +} + +impl Default for FooType1 { + fn default() -> FooType1 { + Self::__Unknown__("No valid variants".into()) + } +} +impl Validate for FooType1 {} #[derive(Default, PartialEq, Debug, UtilsTupleIo, UtilsDefaultSerde)] pub struct FooType2(pub String); impl Validate for FooType2 {} - diff --git a/xsd-parser/tests/enumeration/input.xsd b/xsd-parser/tests/enumeration/input.xsd index 10193155..8a19a135 100644 --- a/xsd-parser/tests/enumeration/input.xsd +++ b/xsd-parser/tests/enumeration/input.xsd @@ -12,6 +12,14 @@ + + + + + + + + diff --git a/xsd-parser/tests/enumeration/mod.rs b/xsd-parser/tests/enumeration/mod.rs index 28290037..0b83561d 100644 --- a/xsd-parser/tests/enumeration/mod.rs +++ b/xsd-parser/tests/enumeration/mod.rs @@ -16,7 +16,7 @@ fn deserialization_works() { let de: expected::FooType = yaserde::de::from_str(ser).unwrap(); - assert_eq!(de, expected::FooType::Auto); + assert_eq!(de, expected::FooType::AUTO); } #[test] diff --git a/xsd-parser/tests/union/expected.rs b/xsd-parser/tests/union/expected.rs index 47202c6f..1af13131 100644 --- a/xsd-parser/tests/union/expected.rs +++ b/xsd-parser/tests/union/expected.rs @@ -1,7 +1,8 @@ +#[allow(non_camel_case_types, clippy::upper_case_acronyms)] #[derive(PartialEq, Debug, UtilsUnionSerDe)] pub enum FooType { - Int(i32), - String(String), + int(i32), + string(String), __Unknown__(String), } diff --git a/xsd-parser/tests/union/mod.rs b/xsd-parser/tests/union/mod.rs index 8178cbcf..c5e36147 100644 --- a/xsd-parser/tests/union/mod.rs +++ b/xsd-parser/tests/union/mod.rs @@ -13,7 +13,7 @@ fn deserialization_works() { let de: expected::FooType = yaserde::de::from_str(ser).unwrap(); - assert_eq!(de, expected::FooType::String("string".to_string())); + assert_eq!(de, expected::FooType::string("string".to_string())); } #[test]