From b44fcba9d5599b654410cedfa0a29c3629c421f4 Mon Sep 17 00:00:00 2001 From: Andrei Zisu Date: Thu, 21 Dec 2023 23:00:14 +0100 Subject: [PATCH 01/11] Convert elements to serde --- xml_schema_derive/src/xsd/element.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/xml_schema_derive/src/xsd/element.rs b/xml_schema_derive/src/xsd/element.rs index 076c694..4bab02e 100644 --- a/xml_schema_derive/src/xsd/element.rs +++ b/xml_schema_derive/src/xsd/element.rs @@ -73,7 +73,7 @@ impl Implementation for Element { quote! { #docs - #[derive(Clone, Debug, Default, PartialEq, yaserde_derive::YaDeserialize, yaserde_derive::YaSerialize)] + #[derive(Clone, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)] #namespace_definition pub struct #struct_name { #fields @@ -121,7 +121,7 @@ impl Element { let name = if multiple { format!("{name}s") } else { name }; let attribute_name = Ident::new(&name, Span::call_site()); - let yaserde_rename = &self.name; + let rename = &self.name; let rust_type = if let Some(complex_type) = &self.complex_type { complex_type.get_integrated_implementation(&self.name) @@ -166,7 +166,7 @@ impl Element { .unwrap_or_default(); quote! { - #[yaserde(rename=#yaserde_rename #prefix_attribute)] + #[serde(rename=#rename #prefix_attribute)] pub #attribute_name: #module#rust_type, } } @@ -210,7 +210,7 @@ mod tests { {DOCS} {DERIVES} pub struct Volume {{ - #[yaserde(flatten)] + #[serde(flatten)] pub content: xml_schema_types::VolumeType, }}"# )) From 19baa7bb4ec42449aa4cea65456b9f8b2a4979e5 Mon Sep 17 00:00:00 2001 From: Andrei Zisu Date: Thu, 21 Dec 2023 23:24:56 +0100 Subject: [PATCH 02/11] Disable prefixes They are not supported by quick-xml/serde --- xml_schema_derive/src/xsd/element.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/xml_schema_derive/src/xsd/element.rs b/xml_schema_derive/src/xsd/element.rs index 4bab02e..5585abd 100644 --- a/xml_schema_derive/src/xsd/element.rs +++ b/xml_schema_derive/src/xsd/element.rs @@ -101,7 +101,7 @@ impl Element { pub fn get_field_implementation( &self, context: &XsdContext, - prefix: &Option, + _prefix: &Option, ) -> TokenStream { if self.name.is_empty() { return quote!(); @@ -148,11 +148,6 @@ impl Element { rust_type }; - let prefix_attribute = prefix - .as_ref() - .map(|prefix| quote!(, prefix=#prefix)) - .unwrap_or_default(); - let module = (!context.is_in_sub_module() && !self .kind @@ -166,7 +161,7 @@ impl Element { .unwrap_or_default(); quote! { - #[serde(rename=#rename #prefix_attribute)] + #[serde(rename=#rename)] pub #attribute_name: #module#rust_type, } } From b4531148b82c2931138e4b23a18d409320a4efda Mon Sep 17 00:00:00 2001 From: Andrei Zisu Date: Thu, 21 Dec 2023 23:36:45 +0100 Subject: [PATCH 03/11] Convert attribute to quick-xml --- xml_schema_derive/src/xsd/attribute.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/xml_schema_derive/src/xsd/attribute.rs b/xml_schema_derive/src/xsd/attribute.rs index 522915e..8533c18 100644 --- a/xml_schema_derive/src/xsd/attribute.rs +++ b/xml_schema_derive/src/xsd/attribute.rs @@ -75,14 +75,12 @@ impl Implementation for Attribute { quote!(#rust_type) }; - let attributes = if name == raw_name { - quote!(attribute) - } else { - quote!(attribute, rename=#raw_name) - }; + let attribute_name = "@".to_string() + &raw_name; + + let attributes = quote!(rename = #attribute_name); quote!( - #[yaserde(#attributes)] + #[serde(#attributes)] pub #field_name: #rust_type, ) } From 699e81de2201b2fbcc295d25521b460038a7a03a Mon Sep 17 00:00:00 2001 From: Andrei Zisu Date: Thu, 21 Dec 2023 23:37:17 +0100 Subject: [PATCH 04/11] Convert complex type to quick-xml --- xml_schema_derive/src/xsd/complex_type.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xml_schema_derive/src/xsd/complex_type.rs b/xml_schema_derive/src/xsd/complex_type.rs index d531559..2bd78a7 100644 --- a/xml_schema_derive/src/xsd/complex_type.rs +++ b/xml_schema_derive/src/xsd/complex_type.rs @@ -57,7 +57,7 @@ impl Implementation for ComplexType { .map(|complex_content| { let complex_content_type = complex_content.get_field_implementation(context, prefix); quote!( - #[yaserde(flatten)] + #[serde(flatten)] #complex_content_type, ) }) @@ -84,7 +84,7 @@ impl Implementation for ComplexType { quote! { #docs - #[derive(Clone, Debug, Default, PartialEq, yaserde_derive::YaDeserialize, yaserde_derive::YaSerialize)] + #[derive(Clone, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)] #namespace_definition pub struct #struct_name { #sequence From a64a14656dbdfea2e389614bff0bcfb6986da4de Mon Sep 17 00:00:00 2001 From: Andrei Zisu Date: Thu, 21 Dec 2023 23:38:25 +0100 Subject: [PATCH 05/11] Fix multiple naming Using plural can go wrong when the field name has an irregular plural, like matrix. --- xml_schema_derive/src/xsd/element.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/xml_schema_derive/src/xsd/element.rs b/xml_schema_derive/src/xsd/element.rs index 5585abd..500caf6 100644 --- a/xml_schema_derive/src/xsd/element.rs +++ b/xml_schema_derive/src/xsd/element.rs @@ -118,7 +118,11 @@ impl Element { log::info!("Generate element {:?}", name); - let name = if multiple { format!("{name}s") } else { name }; + let name = if multiple { + format!("{name}_list") + } else { + name + }; let attribute_name = Ident::new(&name, Span::call_site()); let rename = &self.name; From 70dda53ab1f0afe161d0b3a692cbe2af2e386d33 Mon Sep 17 00:00:00 2001 From: Andrei Zisu Date: Thu, 21 Dec 2023 23:41:38 +0100 Subject: [PATCH 06/11] Generalize extensions for quick-xml --- xml_schema_derive/src/xsd/extension.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/xml_schema_derive/src/xsd/extension.rs b/xml_schema_derive/src/xsd/extension.rs index 27d2b47..d5320a3 100644 --- a/xml_schema_derive/src/xsd/extension.rs +++ b/xml_schema_derive/src/xsd/extension.rs @@ -34,14 +34,8 @@ impl Implementation for Extension { .map(|attribute| attribute.implement(namespace_definition, prefix, context)) .collect(); - let inner_attribute = if format!("{rust_type}") == "String" { - quote!(#[yaserde(text)]) - } else { - TokenStream::new() - }; - quote!( - #inner_attribute + #[serde(rename="$text")] pub content: #rust_type, #attributes ) From 52ffe23f73f4e2d353cfc65dcc6b51a6f6df4aaa Mon Sep 17 00:00:00 2001 From: Andrei Zisu Date: Thu, 21 Dec 2023 23:51:58 +0100 Subject: [PATCH 07/11] Simplify namespace literal quick-xml/serde have no support for prefixes. --- xml_schema_derive/src/xsd/schema.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xml_schema_derive/src/xsd/schema.rs b/xml_schema_derive/src/xsd/schema.rs index 8966828..4c0f27b 100644 --- a/xml_schema_derive/src/xsd/schema.rs +++ b/xml_schema_derive/src/xsd/schema.rs @@ -96,7 +96,7 @@ fn generate_namespace_definition( ), (Some(prefix), Some(target_namespace)) => { let namespace = format!("{prefix}: {target_namespace}"); - quote!(#[yaserde(prefix=#prefix, namespace=#namespace)]) + quote!(#[serde(default)]) } } } From f699d90f7501e4f61689859c9a84a88f8256517c Mon Sep 17 00:00:00 2001 From: Andrei Zisu Date: Thu, 21 Dec 2023 23:52:48 +0100 Subject: [PATCH 08/11] Convert simple type to serde --- xml_schema_derive/src/xsd/simple_type.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/xml_schema_derive/src/xsd/simple_type.rs b/xml_schema_derive/src/xsd/simple_type.rs index dc184aa..770385b 100644 --- a/xml_schema_derive/src/xsd/simple_type.rs +++ b/xml_schema_derive/src/xsd/simple_type.rs @@ -27,12 +27,12 @@ impl Implementation for SimpleType { } quote!( - #[derive(Clone, Debug, Default, PartialEq, yaserde_derive::YaDeserialize, yaserde_derive::YaSerialize)] + #[derive(Clone, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)] #namespace_definition - pub struct #struct_name { - #[yaserde(text)] - pub content: std::string::String, - } + pub struct #struct_name ( + #[serde(rename="$text")] + std::string::String + ); ) } } From 0dd5f6646c5b9e3c8a27c5659a8202624389ec1c Mon Sep 17 00:00:00 2001 From: Andrei Zisu Date: Thu, 21 Dec 2023 23:53:06 +0100 Subject: [PATCH 09/11] Add quick-xml and serde deps --- xml_schema/Cargo.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/xml_schema/Cargo.toml b/xml_schema/Cargo.toml index c7f31a8..b524b82 100644 --- a/xml_schema/Cargo.toml +++ b/xml_schema/Cargo.toml @@ -13,6 +13,8 @@ readme = "../README.md" exclude = ["/tests"] [dependencies] +quick-xml = { version = "0.31.0", features = ["serialize"] } +serde = { version = "1.0.193", features = ["serde_derive"] } xml-schema-derive = { version = "0.3.0", path = "../xml_schema_derive", optional = true } [dev-dependencies] From 3957a7f978475072f85fd279a2df4e85b13b5002 Mon Sep 17 00:00:00 2001 From: Andrei Date: Tue, 2 Jan 2024 20:06:04 +0000 Subject: [PATCH 10/11] Remove custom list yaserde quick-xml implements this natively --- xml_schema_derive/src/xsd/list.rs | 45 ------------------------------- 1 file changed, 45 deletions(-) diff --git a/xml_schema_derive/src/xsd/list.rs b/xml_schema_derive/src/xsd/list.rs index c64aae6..fb195c1 100644 --- a/xml_schema_derive/src/xsd/list.rs +++ b/xml_schema_derive/src/xsd/list.rs @@ -94,51 +94,6 @@ mod tests { pub struct Parent { pub items: Vec } - - impl yaserde::YaDeserialize for Parent { - fn deserialize(reader: &mut yaserde::de::Deserializer) -> Result { - loop { - match reader.next_event()? { - xml::reader::XmlEvent::StartElement{..} => { } - xml::reader::XmlEvent::Characters(ref text_content) => { - let items: Vec = text_content.split(' ') - .map(|item| item.to_owned()) - .map(|item| item.parse().unwrap()) - .collect(); - - return Ok(Parent{items}); - } - _ => { break; } - } - } - - Err ("Unable to parse attribute" . to_string ()) - } - } - - impl yaserde::YaSerialize for Parent { - fn serialize (&self, writer: &mut yaserde::ser::Serializer) -> Result<(), String> { - let content = self - .items - .iter() - .map(|item| item.to_string()) - .collect:: >().join(" "); - - let data_event = xml::writer::XmlEvent::characters(&content); - - writer.write(data_event).map_err(|e| e.to_string())? ; - - Ok (()) - } - - fn serialize_attributes( - &self, - mut source_attributes: Vec , - mut source_namespace: xml::namespace::Namespace - ) -> Result<(Vec , xml::namespace::Namespace), String> { - Ok((source_attributes , source_namespace)) - } - } "#).unwrap(); assert_eq!(implementation.to_string(), expected.to_string()); From 57c80470ce6a6aa81d62a153945dcf6ba755a908 Mon Sep 17 00:00:00 2001 From: Andrei Zisu Date: Sun, 21 Jan 2024 23:25:10 +0100 Subject: [PATCH 11/11] Make field publicly editable --- xml_schema_derive/src/xsd/simple_type.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xml_schema_derive/src/xsd/simple_type.rs b/xml_schema_derive/src/xsd/simple_type.rs index 770385b..3b1b91e 100644 --- a/xml_schema_derive/src/xsd/simple_type.rs +++ b/xml_schema_derive/src/xsd/simple_type.rs @@ -31,7 +31,7 @@ impl Implementation for SimpleType { #namespace_definition pub struct #struct_name ( #[serde(rename="$text")] - std::string::String + pub std::string::String ); ) }