From 97a6a6326c83d50044109e880dfc5200d24b7aed Mon Sep 17 00:00:00 2001 From: Linus Kirkwood Date: Sat, 20 Apr 2024 21:56:30 +1000 Subject: [PATCH 1/9] Fix derive macros on enums missing newline --- xsd-parser/src/generator/enum.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xsd-parser/src/generator/enum.rs b/xsd-parser/src/generator/enum.rs index d314e72d..9ef980c1 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\ @@ -62,10 +62,10 @@ pub trait EnumGenerator { fn macros(&self, entity: &Enum, gen: &Generator) -> Cow<'static, str> { if entity.source == EnumSource::Union { - return "#[derive(PartialEq, Debug, UtilsUnionSerDe)]".into(); + return "#[derive(PartialEq, Debug, UtilsUnionSerDe)]\n".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() { From 2a97fcd2ddd3df0a176a168a76981c886933b3e0 Mon Sep 17 00:00:00 2001 From: Linus Kirkwood Date: Sun, 21 Apr 2024 01:11:20 +1000 Subject: [PATCH 2/9] Stop pascalizing enum case name to avoid clashes Pascalizing enum case names in restrictions can cause clashes (see #154) --- xsd-parser/src/generator/enum_case.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/xsd-parser/src/generator/enum_case.rs b/xsd-parser/src/generator/enum_case.rs index a5580db8..cc77ce05 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,10 @@ 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 { From 0fccc9c5413992ef53df0c6d2928690a1b79bfa9 Mon Sep 17 00:00:00 2001 From: Linus Kirkwood Date: Sun, 21 Apr 2024 01:13:23 +1000 Subject: [PATCH 3/9] Updated enumeration tests after pascalize change --- xsd-parser/tests/enumeration/expected.rs | 25 +++++++++++++++++------- xsd-parser/tests/enumeration/input.xsd | 8 ++++++++ xsd-parser/tests/enumeration/mod.rs | 2 +- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/xsd-parser/tests/enumeration/expected.rs b/xsd-parser/tests/enumeration/expected.rs index c74af0e3..373fe24b 100644 --- a/xsd-parser/tests/enumeration/expected.rs +++ b/xsd-parser/tests/enumeration/expected.rs @@ -1,12 +1,9 @@ #[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 +14,23 @@ impl Default for FooType { } impl Validate for FooType {} +#[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] From 93d699cdc0be956845392a66eb2f36d4f49d3735 Mon Sep 17 00:00:00 2001 From: Linus Kirkwood Date: Sun, 21 Apr 2024 01:17:16 +1000 Subject: [PATCH 4/9] Updated union tests after pascalize change --- xsd-parser/tests/union/expected.rs | 4 ++-- xsd-parser/tests/union/mod.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/xsd-parser/tests/union/expected.rs b/xsd-parser/tests/union/expected.rs index 47202c6f..9a9f83a7 100644 --- a/xsd-parser/tests/union/expected.rs +++ b/xsd-parser/tests/union/expected.rs @@ -1,7 +1,7 @@ #[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] From ca1cac5c5a7e40049c47350192c2a780b605f5dc Mon Sep 17 00:00:00 2001 From: Linus Kirkwood Date: Sun, 21 Apr 2024 02:06:17 +1000 Subject: [PATCH 5/9] Allowed non_camel_case_types on enums Added this macro because enums which contain non-pascalized cases due to 2a97fcd would trigger a clippy lint. --- xsd-parser/src/generator/enum.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/xsd-parser/src/generator/enum.rs b/xsd-parser/src/generator/enum.rs index 9ef980c1..e0034c97 100644 --- a/xsd-parser/src/generator/enum.rs +++ b/xsd-parser/src/generator/enum.rs @@ -61,8 +61,10 @@ pub trait EnumGenerator { } fn macros(&self, entity: &Enum, gen: &Generator) -> Cow<'static, str> { + let allows = "#[allow(non_camel_case_types)]\n"; + if entity.source == EnumSource::Union { - return "#[derive(PartialEq, Debug, UtilsUnionSerDe)]\n".into(); + return format!("{allows}#[derive(PartialEq, Debug, UtilsUnionSerDe)]").into(); } let derives = "#[derive(PartialEq, Debug, YaSerialize, YaDeserialize)]\n"; @@ -70,13 +72,13 @@ pub trait EnumGenerator { 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() ), From 5e9a0b6a8aa7a578cd6981c45116a26e8f3de39a Mon Sep 17 00:00:00 2001 From: Linus Kirkwood Date: Sun, 21 Apr 2024 02:08:04 +1000 Subject: [PATCH 6/9] Updated enum/union tests after adding allow macro --- xsd-parser/tests/enumeration/expected.rs | 2 ++ xsd-parser/tests/union/expected.rs | 1 + 2 files changed, 3 insertions(+) diff --git a/xsd-parser/tests/enumeration/expected.rs b/xsd-parser/tests/enumeration/expected.rs index 373fe24b..b3fcdf37 100644 --- a/xsd-parser/tests/enumeration/expected.rs +++ b/xsd-parser/tests/enumeration/expected.rs @@ -1,3 +1,4 @@ +#[allow(non_camel_case_types)] #[derive(PartialEq, Debug, YaSerialize, YaDeserialize)] #[yaserde(prefix = "tns", namespace = "tns: http://example.com")] pub enum FooType { @@ -14,6 +15,7 @@ impl Default for FooType { } impl Validate for FooType {} +#[allow(non_camel_case_types)] #[derive(PartialEq, Debug, YaSerialize, YaDeserialize)] #[yaserde(prefix = "tns", namespace = "tns: http://example.com")] pub enum FooType1 { diff --git a/xsd-parser/tests/union/expected.rs b/xsd-parser/tests/union/expected.rs index 9a9f83a7..27488c73 100644 --- a/xsd-parser/tests/union/expected.rs +++ b/xsd-parser/tests/union/expected.rs @@ -1,3 +1,4 @@ +#[allow(non_camel_case_types)] #[derive(PartialEq, Debug, UtilsUnionSerDe)] pub enum FooType { int(i32), From 938809c20f57707c59fa92a14924db1718cca54c Mon Sep 17 00:00:00 2001 From: Linus Kirkwood Date: Sun, 21 Apr 2024 12:46:29 +1000 Subject: [PATCH 7/9] Silence clippy warning --- xsd-parser/src/generator/enum_case.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/xsd-parser/src/generator/enum_case.rs b/xsd-parser/src/generator/enum_case.rs index cc77ce05..bd99621d 100644 --- a/xsd-parser/src/generator/enum_case.rs +++ b/xsd-parser/src/generator/enum_case.rs @@ -23,9 +23,7 @@ pub trait EnumCaseGenerator { } fn get_name(&self, entity: &EnumCase, _: &Generator) -> String { - sanitize(filter_type_name( - &entity.name.split(':').last().unwrap_or(&entity.name.to_owned()), - )) + sanitize(filter_type_name(entity.name.split(':').last().unwrap_or(&entity.name.to_owned()))) } fn get_type_name(&self, entity: &EnumCase, gen: &Generator) -> String { From 56642df617a78c5b1c3bea56430fe2afeaec4cd5 Mon Sep 17 00:00:00 2001 From: Linus Kirkwood Date: Sun, 21 Apr 2024 13:01:41 +1000 Subject: [PATCH 8/9] Allowed clippy::upper_case_acronyms on enums Much the same as non_camel_case_types but clippy will still complain without this. --- xsd-parser/src/generator/enum.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xsd-parser/src/generator/enum.rs b/xsd-parser/src/generator/enum.rs index e0034c97..cfd48270 100644 --- a/xsd-parser/src/generator/enum.rs +++ b/xsd-parser/src/generator/enum.rs @@ -61,7 +61,7 @@ pub trait EnumGenerator { } fn macros(&self, entity: &Enum, gen: &Generator) -> Cow<'static, str> { - let allows = "#[allow(non_camel_case_types)]\n"; + let allows = "#[allow(non_camel_case_types, clippy::upper_case_acronyms)]\n"; if entity.source == EnumSource::Union { return format!("{allows}#[derive(PartialEq, Debug, UtilsUnionSerDe)]").into(); From e754ded83278eb34891d7142cc73170a61adec45 Mon Sep 17 00:00:00 2001 From: Linus Kirkwood Date: Sun, 21 Apr 2024 13:02:17 +1000 Subject: [PATCH 9/9] Updated tests after allowing upper_case_acronyms --- xsd-parser/tests/enumeration/expected.rs | 4 ++-- xsd-parser/tests/union/expected.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/xsd-parser/tests/enumeration/expected.rs b/xsd-parser/tests/enumeration/expected.rs index b3fcdf37..e9995eb9 100644 --- a/xsd-parser/tests/enumeration/expected.rs +++ b/xsd-parser/tests/enumeration/expected.rs @@ -1,4 +1,4 @@ -#[allow(non_camel_case_types)] +#[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 { @@ -15,7 +15,7 @@ impl Default for FooType { } impl Validate for FooType {} -#[allow(non_camel_case_types)] +#[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 { diff --git a/xsd-parser/tests/union/expected.rs b/xsd-parser/tests/union/expected.rs index 27488c73..1af13131 100644 --- a/xsd-parser/tests/union/expected.rs +++ b/xsd-parser/tests/union/expected.rs @@ -1,4 +1,4 @@ -#[allow(non_camel_case_types)] +#[allow(non_camel_case_types, clippy::upper_case_acronyms)] #[derive(PartialEq, Debug, UtilsUnionSerDe)] pub enum FooType { int(i32),