From 1cc259e75daa089e3dbd963ff2d3073536b42ef0 Mon Sep 17 00:00:00 2001 From: kurotakazuki Date: Sun, 5 May 2024 13:48:30 +0900 Subject: [PATCH 1/5] feat: support element ref attribute --- xml_schema_derive/src/xsd/element.rs | 50 ++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/xml_schema_derive/src/xsd/element.rs b/xml_schema_derive/src/xsd/element.rs index c8dd087..7235e19 100644 --- a/xml_schema_derive/src/xsd/element.rs +++ b/xml_schema_derive/src/xsd/element.rs @@ -103,7 +103,8 @@ impl Element { context: &XsdContext, prefix: &Option, ) -> TokenStream { - if self.name.is_empty() { + if self.name.is_empty() && (self.refers.is_none() || matches!(self.refers.as_deref(), Some(""))) + { return quote!(); } @@ -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 + .as_ref() + .expect("[Element] refers should be defined") + .to_snake_case() }; log::info!("Generate element {:?}", name); @@ -125,7 +132,14 @@ 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 + .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) @@ -133,6 +147,8 @@ impl Element { 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 { + RustTypesMapping::get(context, refers) } else { panic!( "[Element] {:?} unimplemented type: {:?}", @@ -255,4 +271,32 @@ mod tests { assert_eq!(implementation.to_string(), expected.to_string()); } + + #[test] + fn refers_element_field_implementation() { + // + 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#""#) + .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()); + } } From 3931b548095508412f9dc28f2ed0eeaecee0963c Mon Sep 17 00:00:00 2001 From: kurotakazuki Date: Sun, 5 May 2024 13:52:23 +0900 Subject: [PATCH 2/5] fix: cargo clippy --- xml_schema_derive/src/xsd/element.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xml_schema_derive/src/xsd/element.rs b/xml_schema_derive/src/xsd/element.rs index 7235e19..7ad8c2a 100644 --- a/xml_schema_derive/src/xsd/element.rs +++ b/xml_schema_derive/src/xsd/element.rs @@ -135,7 +135,7 @@ impl Element { let yaserde_rename = if !self.name.is_empty() { &self.name } else { - &self + self .refers .as_ref() .expect("[Element] refers should be defined") From e27bf16fab44831a3a3860056badc12872344180 Mon Sep 17 00:00:00 2001 From: kurotakazuki Date: Sun, 5 May 2024 14:00:26 +0900 Subject: [PATCH 3/5] add: refers_element_field_implementation test --- xml_schema_derive/src/xsd/element.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/xml_schema_derive/src/xsd/element.rs b/xml_schema_derive/src/xsd/element.rs index 7ad8c2a..b6010ff 100644 --- a/xml_schema_derive/src/xsd/element.rs +++ b/xml_schema_derive/src/xsd/element.rs @@ -274,7 +274,7 @@ mod tests { #[test] fn refers_element_field_implementation() { - // + // let element = Element { name: "".to_string(), kind: None, @@ -298,5 +298,26 @@ mod tests { .unwrap(); assert_eq!(implementation.to_string(), expected.to_string()); + + // + 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()); } } From d34bea2306bb4f0a29fcc1bf4d485dce7222c33f Mon Sep 17 00:00:00 2001 From: kurotakazuki Date: Mon, 6 May 2024 22:40:14 +0900 Subject: [PATCH 4/5] refact: use get_refers --- xml_schema_derive/src/xsd/element.rs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/xml_schema_derive/src/xsd/element.rs b/xml_schema_derive/src/xsd/element.rs index b6010ff..790872b 100644 --- a/xml_schema_derive/src/xsd/element.rs +++ b/xml_schema_derive/src/xsd/element.rs @@ -103,8 +103,8 @@ impl Element { context: &XsdContext, prefix: &Option, ) -> TokenStream { - if self.name.is_empty() && (self.refers.is_none() || matches!(self.refers.as_deref(), Some(""))) - { + let refers = self.get_refers(); + if self.name.is_empty() && refers.is_none() { return quote!(); } @@ -116,9 +116,7 @@ impl Element { } else if !self.name.is_empty() { self.name.to_snake_case() } else { - self - .refers - .as_ref() + refers .expect("[Element] refers should be defined") .to_snake_case() }; @@ -135,10 +133,7 @@ impl Element { let yaserde_rename = if !self.name.is_empty() { &self.name } else { - self - .refers - .as_ref() - .expect("[Element] refers should be defined") + refers.expect("[Element] refers should be defined") }; let rust_type = if let Some(complex_type) = &self.complex_type { @@ -147,7 +142,7 @@ impl Element { 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 { + } else if let Some(refers) = refers { RustTypesMapping::get(context, refers) } else { panic!( @@ -186,6 +181,16 @@ impl Element { 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)] From ede0bcf9ab0446cc43c081ca880a3ab0783870d6 Mon Sep 17 00:00:00 2001 From: kurotakazuki Date: Mon, 6 May 2024 23:51:31 +0900 Subject: [PATCH 5/5] refact: cargo clippy --- xml_schema_derive/src/xsd/element.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/xml_schema_derive/src/xsd/element.rs b/xml_schema_derive/src/xsd/element.rs index 790872b..d3775de 100644 --- a/xml_schema_derive/src/xsd/element.rs +++ b/xml_schema_derive/src/xsd/element.rs @@ -297,9 +297,9 @@ mod tests { 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 ,"# - )) + let expected = TokenStream::from_str( + r#"#[yaserde(rename = "OwnedType")] pub owned_type : xml_schema_types :: OwnedType ,"#, + ) .unwrap(); assert_eq!(implementation.to_string(), expected.to_string()); @@ -318,9 +318,9 @@ mod tests { let implementation = element.get_field_implementation(&context, &None); - let expected = TokenStream::from_str(&format!( + let expected = TokenStream::from_str( r#"#[yaserde(rename = "OwnedType")] pub owned_type_list : Vec < xml_schema_types :: OwnedType > ,"# - )) + ) .unwrap(); assert_eq!(implementation.to_string(), expected.to_string());