From 30bb4afec69f2de1600459ef60b04d83f268ca17 Mon Sep 17 00:00:00 2001 From: Marc-Antoine Arnaud Date: Mon, 18 Dec 2023 16:28:45 +0100 Subject: [PATCH] fix: use module for elements in complexType issue #28 --- xml_schema/tests/complex_type.rs | 4 ++-- xml_schema/tests/simple_type.rs | 14 ++++++------- xml_schema_derive/src/xsd/element.rs | 20 +++++++++++++++---- .../src/xsd/rust_types_mapping.rs | 14 +++++++++++++ xml_schema_derive/src/xsd/schema.rs | 4 ++-- 5 files changed, 41 insertions(+), 15 deletions(-) diff --git a/xml_schema/tests/complex_type.rs b/xml_schema/tests/complex_type.rs index 01019af..e3a0195 100644 --- a/xml_schema/tests/complex_type.rs +++ b/xml_schema/tests/complex_type.rs @@ -16,9 +16,9 @@ fn complex_type_string() { "#; - let sample_1: types::ComplexListOfElements = from_str(xml_1).unwrap(); + let sample_1: xml_schema_types::ComplexListOfElements = from_str(xml_1).unwrap(); - let model = types::ComplexListOfElements { + let model = xml_schema_types::ComplexListOfElements { annotation: Some("Test content".to_string()), label: "Label content".to_string(), }; diff --git a/xml_schema/tests/simple_type.rs b/xml_schema/tests/simple_type.rs index e9efac3..f6b97e0 100644 --- a/xml_schema/tests/simple_type.rs +++ b/xml_schema/tests/simple_type.rs @@ -18,9 +18,9 @@ fn simple_type_string() { "#; - let sample_1: types::SampleType = from_str(xml_1).unwrap(); + let sample_1: xml_schema_types::SampleType = from_str(xml_1).unwrap(); - let model = types::SampleType { + let model = xml_schema_types::SampleType { content: "Test content".to_string(), }; @@ -38,14 +38,14 @@ fn simple_type_list() { "#; - let sample_1: types::BaseType = from_str(xml_1).unwrap(); + let sample_1: xml_schema_types::BaseType = from_str(xml_1).unwrap(); - let model = types::BaseType { - strings: Some(types::StringList { + let model = xml_schema_types::BaseType { + strings: Some(xml_schema_types::StringList { items: vec!["value1".to_string(), "value2".to_string()], }), - integers: Some(types::IntegerList { items: vec![3, 6] }), - booleans: Some(types::BooleanList { + integers: Some(xml_schema_types::IntegerList { items: vec![3, 6] }), + booleans: Some(xml_schema_types::BooleanList { items: vec![true, false], }), }; diff --git a/xml_schema_derive/src/xsd/element.rs b/xml_schema_derive/src/xsd/element.rs index 659661a..1ac423d 100644 --- a/xml_schema_derive/src/xsd/element.rs +++ b/xml_schema_derive/src/xsd/element.rs @@ -51,7 +51,7 @@ impl Implementation for Element { ( quote!( #[yaserde(#subtype_mode)] - pub content: types::#extern_type, + pub content: xml_schema_types::#extern_type, ), quote!(), ) @@ -154,9 +154,21 @@ impl Element { quote!() }; + let module = if let Some(kind) = &self.kind { + if RustTypesMapping::is_xs_string(context, kind) { + quote!() + } else if RustTypesMapping::is_xs_int(context, kind) { + quote!() + } else { + quote!(xml_schema_types::) + } + } else { + quote!() + }; + quote! { #[yaserde(rename=#yaserde_rename #prefix_attribute)] - pub #attribute_name: #rust_type, + pub #attribute_name: #module#rust_type, } } } @@ -200,7 +212,7 @@ mod tests { {DERIVES} pub struct Volume {{ #[yaserde(flatten)] - pub content: types::VolumeType, + pub content: xml_schema_types::VolumeType, }}"# )) .unwrap(); @@ -237,7 +249,7 @@ mod tests { {DERIVES} pub struct Volume {{ #[yaserde(text)] - pub content: types::String, + pub content: xml_schema_types::String, }}"# )) .unwrap(); diff --git a/xml_schema_derive/src/xsd/rust_types_mapping.rs b/xml_schema_derive/src/xsd/rust_types_mapping.rs index be1e632..f7f7bde 100644 --- a/xml_schema_derive/src/xsd/rust_types_mapping.rs +++ b/xml_schema_derive/src/xsd/rust_types_mapping.rs @@ -41,6 +41,20 @@ impl RustTypesMapping { false } + pub fn is_xs_int(context: &XsdContext, kind: &str) -> bool { + let items: Vec<&str> = kind.split(':').collect(); + + if items.len() == 2 { + if context.match_xml_schema_prefix(items.first().unwrap()) { + return *items.last().unwrap() == "int"; + } + } else if items.len() == 1 && !context.has_xml_schema_prefix() { + return *items.last().unwrap() == "int"; + } + + false + } + fn basic_type(item: &str) -> TokenStream { match item { "bool" => quote!(bool), diff --git a/xml_schema_derive/src/xsd/schema.rs b/xml_schema_derive/src/xsd/schema.rs index 3c91a5e..df9e23a 100644 --- a/xml_schema_derive/src/xsd/schema.rs +++ b/xml_schema_derive/src/xsd/schema.rs @@ -62,7 +62,7 @@ impl Implementation for Schema { .collect(); quote!( - pub mod types { + pub mod xml_schema_types { #simple_types #complex_types } @@ -104,7 +104,7 @@ mod tests { .unwrap(); let implementation = format!("{}", schema.implement(&TokenStream::new(), &None, &context)); - assert_eq!(implementation, "pub mod types { }"); + assert_eq!(implementation, "pub mod xml_schema_types { }"); } #[test]