Skip to content

Commit

Permalink
fix: use module for elements in complexType
Browse files Browse the repository at this point in the history
issue #28
  • Loading branch information
MarcAntoine-Arnaud committed Dec 18, 2023
1 parent fb03fee commit 30bb4af
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
4 changes: 2 additions & 2 deletions xml_schema/tests/complex_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ fn complex_type_string() {
</ComplexListOfElements>
"#;

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(),
};
Expand Down
14 changes: 7 additions & 7 deletions xml_schema/tests/simple_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ fn simple_type_string() {
</Sample-type>
"#;

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(),
};

Expand All @@ -38,14 +38,14 @@ fn simple_type_list() {
<BaseType strings="value1 value2" integers="3 6" booleans="true false" />
"#;

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],
}),
};
Expand Down
20 changes: 16 additions & 4 deletions xml_schema_derive/src/xsd/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(),
)
Expand Down Expand Up @@ -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,
}
}
}
Expand Down Expand Up @@ -200,7 +212,7 @@ mod tests {
{DERIVES}
pub struct Volume {{
#[yaserde(flatten)]
pub content: types::VolumeType,
pub content: xml_schema_types::VolumeType,
}}"#
))
.unwrap();
Expand Down Expand Up @@ -237,7 +249,7 @@ mod tests {
{DERIVES}
pub struct Volume {{
#[yaserde(text)]
pub content: types::String,
pub content: xml_schema_types::String,
}}"#
))
.unwrap();
Expand Down
14 changes: 14 additions & 0 deletions xml_schema_derive/src/xsd/rust_types_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions xml_schema_derive/src/xsd/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Implementation for Schema {
.collect();

quote!(
pub mod types {
pub mod xml_schema_types {
#simple_types
#complex_types
}
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 30bb4af

Please sign in to comment.