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 3 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
71 changes: 68 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 @@ impl Element {
context: &XsdContext,
prefix: &Option<String>,
) -> TokenStream {
if self.name.is_empty() {
if self.name.is_empty() && (self.refers.is_none() || matches!(self.refers.as_deref(), Some("")))
MarcAntoine-Arnaud marked this conversation as resolved.
Show resolved Hide resolved
{
return quote!();
}

Expand All @@ -112,8 +113,14 @@ impl Element {

let name = if self.name.to_lowercase() == "type" {
"kind".to_string()
} else {
} else if !self.name.is_empty() {
self.name.to_snake_case()
} else {
self
.refers
MarcAntoine-Arnaud marked this conversation as resolved.
Show resolved Hide resolved
.as_ref()
.expect("[Element] refers should be defined")
.to_snake_case()
};

log::info!("Generate element {:?}", name);
Expand All @@ -125,14 +132,23 @@ impl Element {
};

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 {
self
.refers
MarcAntoine-Arnaud marked this conversation as resolved.
Show resolved Hide resolved
.as_ref()
.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) = &self.refers {
MarcAntoine-Arnaud marked this conversation as resolved.
Show resolved Hide resolved
RustTypesMapping::get(context, refers)
} else {
panic!(
"[Element] {:?} unimplemented type: {:?}",
Expand Down Expand Up @@ -255,4 +271,53 @@ mod tests {

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!(
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!(
r#"#[yaserde(rename = "OwnedType")] pub owned_type_list : Vec < xml_schema_types :: OwnedType > ,"#
))
.unwrap();

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