Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Element Ref field Implementation #44

Merged
merged 5 commits into from
May 6, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 73 additions & 3 deletions xml_schema_derive/src/xsd/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@
context: &XsdContext,
prefix: &Option<String>,
) -> TokenStream {
if self.name.is_empty() {
let refers = self.get_refers();
if self.name.is_empty() && refers.is_none() {
return quote!();
}

Expand All @@ -112,8 +113,12 @@

let name = if self.name.to_lowercase() == "type" {
"kind".to_string()
} else {
} else if !self.name.is_empty() {
self.name.to_snake_case()
} else {
refers
.expect("[Element] refers should be defined")
.to_snake_case()
};

log::info!("Generate element {:?}", name);
Expand All @@ -125,14 +130,20 @@
};

let attribute_name = Ident::new(&name, Span::call_site());
let yaserde_rename = &self.name;
let yaserde_rename = if !self.name.is_empty() {
&self.name
} else {
refers.expect("[Element] refers should be defined")
};

let rust_type = if let Some(complex_type) = &self.complex_type {
complex_type.get_integrated_implementation(&self.name)
} else if let Some(simple_type) = &self.simple_type {
simple_type.get_type_implementation(context, &Some(self.name.to_owned()))
} else if let Some(kind) = &self.kind {
RustTypesMapping::get(context, kind)
} else if let Some(refers) = refers {
RustTypesMapping::get(context, refers)
} else {
panic!(
"[Element] {:?} unimplemented type: {:?}",
Expand Down Expand Up @@ -170,6 +181,16 @@
pub #attribute_name: #rust_type,
}
}

fn get_refers(&self) -> Option<&str> {
self.refers.as_ref().and_then(|refers| {
if refers.is_empty() {
None
} else {
Some(refers.as_str())
}
})
}
}

#[cfg(test)]
Expand Down Expand Up @@ -255,4 +276,53 @@

assert_eq!(implementation.to_string(), expected.to_string());
}

#[test]
fn refers_element_field_implementation() {
// <xs:element ref="OwnedType" />
let element = Element {
name: "".to_string(),
kind: None,
refers: Some("OwnedType".to_string()),
min_occurences: None,
max_occurences: None,
complex_type: None,
simple_type: None,
annotation: None,
};

let context =
XsdContext::new(r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"></xs:schema>"#)
.unwrap();

let implementation = element.get_field_implementation(&context, &None);

let expected = TokenStream::from_str(&format!(

Check failure on line 300 in xml_schema_derive/src/xsd/element.rs

View workflow job for this annotation

GitHub Actions / Clippy

useless use of `format!`
r#"#[yaserde(rename = "OwnedType")] pub owned_type : xml_schema_types :: OwnedType ,"#
))
.unwrap();

assert_eq!(implementation.to_string(), expected.to_string());

// <xs:element ref="OwnedType" minOccurs="0" maxOccurs="unbounded" />
let element = Element {
name: "".to_string(),
kind: None,
refers: Some("OwnedType".to_string()),
min_occurences: Some(0),
max_occurences: Some(MaxOccurences::Unbounded),
complex_type: None,
simple_type: None,
annotation: None,
};

let implementation = element.get_field_implementation(&context, &None);

let expected = TokenStream::from_str(&format!(

Check failure on line 321 in xml_schema_derive/src/xsd/element.rs

View workflow job for this annotation

GitHub Actions / Clippy

useless use of `format!`
r#"#[yaserde(rename = "OwnedType")] pub owned_type_list : Vec < xml_schema_types :: OwnedType > ,"#
))
.unwrap();

assert_eq!(implementation.to_string(), expected.to_string());
}
}
Loading