Skip to content

Commit a9dc932

Browse files
format code
1 parent 214dbb1 commit a9dc932

File tree

9 files changed

+146
-30
lines changed

9 files changed

+146
-30
lines changed

xml_schema/tests/complex_type.rs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#[macro_use]
2+
extern crate yaserde_derive;
3+
4+
use log::debug;
5+
use std::io::prelude::*;
6+
use xml_schema_derive::XmlSchema;
7+
use yaserde::de::from_str;
8+
use yaserde::ser::to_string;
9+
use yaserde::{YaDeserialize, YaSerialize};
10+
11+
#[test]
12+
fn complex_type_string() {
13+
#[derive(Debug, XmlSchema)]
14+
#[xml_schema(source = "xml_schema/tests/complex_type.xsd")]
15+
struct ComplexTypeSchema;
16+
17+
let xml_1 = r#"
18+
<?xml version="1.0" encoding="UTF-8"?>
19+
<ComplexListOfElements>
20+
<Annotation>Test content</Annotation>
21+
<Label>Label content</Label>
22+
</ComplexListOfElements>
23+
"#;
24+
25+
let sample_1: ComplexListOfElements = from_str(xml_1).unwrap();
26+
27+
let model = ComplexListOfElements {
28+
annotation: Some("Test content".to_string()),
29+
label: "Label content".to_string(),
30+
};
31+
32+
assert_eq!(sample_1, model);
33+
34+
let data = to_string(&model).unwrap();
35+
assert_eq!(
36+
data,
37+
r#"<?xml version="1.0" encoding="utf-8"?><ComplexListOfElements><Annotation>Test content</Annotation><Label>Label content</Label></ComplexListOfElements>"#
38+
);
39+
}

xml_schema/tests/complex_type.xsd

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
2+
<xs:complexType name="ComplexListOfElements">
3+
<xs:sequence>
4+
<xs:element name="Annotation" type="xs:string" minOccurs="0"/>
5+
<xs:element name="Label">
6+
<xs:complexType>
7+
<xs:simpleContent>
8+
<xs:extension base="xs:string">
9+
<xs:attribute name="scope" type="xs:anyURI" use="optional" default="http://example.com#elements"/>
10+
</xs:extension>
11+
</xs:simpleContent>
12+
</xs:complexType>
13+
</xs:element>
14+
</xs:sequence>
15+
</xs:complexType>
16+
</xs:schema>

xml_schema/tests/simple_type_list.xsd

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
3-
xmlns:am="http://www.smpte-ra.org/schemas/429-9/2007/AM">
2+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
43

54
<xs:complexType name="BaseType">
65
<xs:attribute name="strings" type="StringList"/>
+16-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use crate::xsd::extension::Extension;
1+
use crate::xsd::{extension::Extension, xsd_context::XsdContext};
22
use log::debug;
3+
use proc_macro2::TokenStream;
34
use std::io::Read;
45
use yaserde::YaDeserialize;
56

@@ -8,3 +9,17 @@ use yaserde::YaDeserialize;
89
pub struct ComplexContent {
910
pub extension: Option<Extension>,
1011
}
12+
13+
impl ComplexContent {
14+
pub fn get_field_implementation(
15+
&self,
16+
context: &XsdContext,
17+
prefix: &Option<String>,
18+
) -> TokenStream {
19+
self
20+
.extension
21+
.as_ref()
22+
.unwrap()
23+
.get_field_implementation(context, prefix)
24+
}
25+
}

xml_schema_derive/src/xsd/complex_type.rs

+26-13
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,17 @@ impl Implementation for ComplexType {
5454
.map(|simple_content| simple_content.implement(namespace_definition, prefix, context))
5555
.unwrap_or_else(TokenStream::new);
5656

57-
if self.complex_content.is_some() {
58-
debug!("Complex Content: {:?}", self);
59-
}
57+
let complex_content = self
58+
.complex_content
59+
.as_ref()
60+
.map(|complex_content| {
61+
let complex_content_type = complex_content.get_field_implementation(context, prefix);
62+
quote!(
63+
#[yaserde(flatten)]
64+
#complex_content_type,
65+
)
66+
})
67+
.unwrap_or_else(TokenStream::new);
6068

6169
let attributes: TokenStream = self
6270
.attributes
@@ -84,6 +92,7 @@ impl Implementation for ComplexType {
8492
pub struct #struct_name {
8593
#sequence
8694
#simple_content
95+
#complex_content
8796
#attributes
8897
}
8998

@@ -95,14 +104,22 @@ impl Implementation for ComplexType {
95104
impl ComplexType {
96105
pub fn get_field_implementation(
97106
&self,
98-
prefix: &Option<String>,
99107
context: &XsdContext,
108+
prefix: &Option<String>,
100109
) -> TokenStream {
101-
self
102-
.sequence
103-
.as_ref()
104-
.map(|sequence| sequence.get_field_implementation(context, prefix))
105-
.unwrap_or_else(TokenStream::new)
110+
if self.sequence.is_some() {
111+
self
112+
.sequence
113+
.as_ref()
114+
.map(|sequence| sequence.get_field_implementation(context, prefix))
115+
.unwrap_or_else(TokenStream::new)
116+
} else {
117+
self
118+
.simple_content
119+
.as_ref()
120+
.map(|simple_content| simple_content.get_field_implementation(context, prefix))
121+
.unwrap_or_else(TokenStream::new)
122+
}
106123
}
107124

108125
pub fn get_integrated_implementation(&self, parent_name: &str) -> TokenStream {
@@ -116,9 +133,5 @@ impl ComplexType {
116133
}
117134

118135
quote!(String)
119-
// println!("{:?}", self);
120-
// panic!(
121-
// "[Complex Type] Unimplemented complex type for parent element {:?}", parent_name
122-
// );
123136
}
124137
}

xml_schema_derive/src/xsd/element.rs

+26-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::xsd::{
22
annotation::Annotation, complex_type::ComplexType, max_occurences::MaxOccurences,
3-
rust_types_mapping::RustTypesMapping, Implementation, XsdContext,
3+
rust_types_mapping::RustTypesMapping, simple_type::SimpleType, Implementation, XsdContext,
44
};
55
use heck::{CamelCase, SnakeCase};
66
use log::{debug, info};
@@ -23,7 +23,9 @@ pub struct Element {
2323
#[yaserde(rename = "maxOccurs", attribute)]
2424
pub max_occurences: Option<MaxOccurences>,
2525
#[yaserde(rename = "complexType")]
26-
pub complex_type: Vec<ComplexType>,
26+
pub complex_type: Option<ComplexType>,
27+
#[yaserde(rename = "simpleType")]
28+
pub simple_type: Option<SimpleType>,
2729
#[yaserde(rename = "annotation")]
2830
pub annotation: Option<Annotation>,
2931
}
@@ -40,7 +42,7 @@ impl Implementation for Element {
4042
Span::call_site(),
4143
);
4244

43-
let fields = if let Some(kind) = &self.kind {
45+
let (fields, extra_structs) = if let Some(kind) = &self.kind {
4446
let subtype_mode = if RustTypesMapping::is_xs_string(context, kind) {
4547
quote!(text)
4648
} else {
@@ -49,16 +51,21 @@ impl Implementation for Element {
4951

5052
let extern_type = RustTypesMapping::get(context, kind);
5153

52-
quote!(
53-
#[yaserde(#subtype_mode)]
54-
pub content: #extern_type,
54+
(
55+
quote!(
56+
#[yaserde(#subtype_mode)]
57+
pub content: #extern_type,
58+
),
59+
quote!(),
5560
)
5661
} else {
57-
self
62+
let fields_definition = self
5863
.complex_type
5964
.iter()
60-
.map(|complex_type| complex_type.get_field_implementation(prefix, context))
61-
.collect()
65+
.map(|complex_type| complex_type.get_field_implementation(context, prefix))
66+
.collect();
67+
68+
(fields_definition, quote!())
6269
};
6370

6471
let docs = self
@@ -74,6 +81,8 @@ impl Implementation for Element {
7481
pub struct #struct_name {
7582
#fields
7683
}
84+
85+
#extra_structs
7786
}
7887
}
7988
}
@@ -85,7 +94,7 @@ impl Element {
8594
prefix: &Option<String>,
8695
context: &XsdContext,
8796
) -> TokenStream {
88-
if self.complex_type.is_empty() {
97+
if self.complex_type.is_none() {
8998
return quote!();
9099
}
91100

@@ -115,8 +124,10 @@ impl Element {
115124
let attribute_name = Ident::new(&name, Span::call_site());
116125
let yaserde_rename = &self.name;
117126

118-
let rust_type = if let Some(complex_type) = self.complex_type.first() {
127+
let rust_type = if let Some(complex_type) = &self.complex_type {
119128
complex_type.get_integrated_implementation(&self.name)
129+
} else if let Some(simple_type) = &self.simple_type {
130+
simple_type.get_type_implementation(context, &Some(self.name.to_owned()))
120131
} else if let Some(kind) = &self.kind {
121132
RustTypesMapping::get(context, kind)
122133
} else {
@@ -168,7 +179,8 @@ mod tests {
168179
refers: None,
169180
min_occurences: None,
170181
max_occurences: None,
171-
complex_type: vec![],
182+
complex_type: None,
183+
simple_type: None,
172184
annotation: Some(Annotation {
173185
id: None,
174186
attributes: vec![],
@@ -199,7 +211,8 @@ mod tests {
199211
refers: None,
200212
min_occurences: None,
201213
max_occurences: None,
202-
complex_type: vec![],
214+
complex_type: None,
215+
simple_type: None,
203216
annotation: Some(Annotation {
204217
id: None,
205218
attributes: vec![],

xml_schema_derive/src/xsd/extension.rs

+11
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ impl Implementation for Extension {
5151
}
5252
}
5353

54+
impl Extension {
55+
pub fn get_field_implementation(
56+
&self,
57+
context: &XsdContext,
58+
_prefix: &Option<String>,
59+
) -> TokenStream {
60+
let rust_type = RustTypesMapping::get(context, &self.base);
61+
quote!(pub content : #rust_type)
62+
}
63+
}
64+
5465
#[cfg(test)]
5566
mod tests {
5667
use super::*;

xml_schema_derive/src/xsd/rust_types_mapping.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl RustTypesMapping {
2323
RustTypesMapping::basic_type(*items.last().unwrap())
2424
}
2525
} else {
26-
unimplemented!();
26+
panic!("Unknown type {}", kind)
2727
}
2828
}
2929

xml_schema_derive/src/xsd/simple_content.rs

+10
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,13 @@ impl Implementation for SimpleContent {
2323
.implement(namespace_definition, prefix, context)
2424
}
2525
}
26+
27+
impl SimpleContent {
28+
pub fn get_field_implementation(
29+
&self,
30+
context: &XsdContext,
31+
prefix: &Option<String>,
32+
) -> TokenStream {
33+
self.extension.get_field_implementation(context, prefix)
34+
}
35+
}

0 commit comments

Comments
 (0)