Skip to content

Commit 89c2fc8

Browse files
committed
support choice in sequences
1 parent 5c8182f commit 89c2fc8

File tree

5 files changed

+95
-6
lines changed

5 files changed

+95
-6
lines changed

xml_schema/tests/choice.rs

+34
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,37 @@ fn choice() {
3939
r#"<?xml version="1.0" encoding="utf-8"?><Parent><XFirstname>John</XFirstname></Parent>"#
4040
);
4141
}
42+
43+
#[test]
44+
fn choice_sequence() {
45+
#[derive(Debug, XmlSchema)]
46+
#[xml_schema(source = "xml_schema/tests/choice_sequence.xsd")]
47+
struct ChoiceTypeSchema;
48+
49+
let xml_1 = r#"
50+
<?xml version="1.0" encoding="UTF-8"?>
51+
<Parent>
52+
<Name>Doe</Name>
53+
<XFirstname>John</XFirstname>
54+
</Parent>
55+
"#;
56+
57+
let sample_1: Parent = from_str(xml_1).unwrap();
58+
59+
let model = Parent {
60+
name: "Doe".to_string(),
61+
x_firstname: Some(Firstname {
62+
content: "John".to_string(),
63+
scope: None,
64+
}),
65+
x_lastname: None,
66+
};
67+
68+
assert_eq!(sample_1, model);
69+
70+
let data = to_string(&model).unwrap();
71+
assert_eq!(
72+
data,
73+
r#"<?xml version="1.0" encoding="utf-8"?><Parent><Name>Doe</Name><XFirstname>John</XFirstname></Parent>"#
74+
);
75+
}

xml_schema/tests/choice_sequence.xsd

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
2+
<xs:complexType name="Firstname">
3+
<xs:simpleContent>
4+
<xs:extension base="xs:string">
5+
<xs:attribute name="scope" type="xs:anyURI" use="optional" default="http://example.com#elements"/>
6+
</xs:extension>
7+
</xs:simpleContent>
8+
</xs:complexType>
9+
10+
<xs:complexType name="Lastname">
11+
<xs:simpleContent>
12+
<xs:extension base="xs:string">
13+
<xs:attribute name="scope" type="xs:anyURI" use="optional" default="http://example.com#elements"/>
14+
</xs:extension>
15+
</xs:simpleContent>
16+
</xs:complexType>
17+
18+
19+
<xs:complexType name="Parent">
20+
<xs:sequence>
21+
<xs:element name="Name" type="xsd:string" minOccurs="1"/>
22+
<xs:choice>
23+
<xs:element name="XFirstname" type="Firstname"/>
24+
<xs:element name="XLastname" type="Lastname"/>
25+
</xs:choice>
26+
</xs:sequence>
27+
</xs:complexType>
28+
</xs:schema>

xml_schema_derive/src/xsd/complex_type.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub struct ComplexType {
2020
pub name: String,
2121
#[yaserde(rename = "attribute")]
2222
pub attributes: Vec<Attribute>,
23+
#[yaserde(rename = "sequence")]
2324
pub sequence: Option<Sequence>,
2425
#[yaserde(rename = "simpleContent")]
2526
pub simple_content: Option<SimpleContent>,

xml_schema_derive/src/xsd/extension.rs

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub struct Extension {
2020
pub attributes: Vec<Attribute>,
2121
#[yaserde(rename = "sequence")]
2222
pub sequences: Vec<Sequence>,
23+
#[yaserde(rename = "choice")]
24+
pub choices: Vec<Choice>,
2325
}
2426

2527
impl Implementation for Extension {

xml_schema_derive/src/xsd/sequence.rs

+30-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::xsd::{element::Element, Implementation, XsdContext};
1+
use crate::xsd::{choice::Choice, element::Element, Implementation, XsdContext};
22
use log::{debug, info};
33
use proc_macro2::TokenStream;
44
use std::io::prelude::*;
@@ -9,6 +9,8 @@ use yaserde::YaDeserialize;
99
pub struct Sequence {
1010
#[yaserde(rename = "element")]
1111
pub elements: Vec<Element>,
12+
#[yaserde(rename = "choice")]
13+
pub choices: Vec<Choice>,
1214
}
1315

1416
impl Implementation for Sequence {
@@ -19,11 +21,22 @@ impl Implementation for Sequence {
1921
context: &XsdContext,
2022
) -> TokenStream {
2123
info!("Generate elements");
22-
self
24+
let elements: TokenStream = self
2325
.elements
2426
.iter()
2527
.map(|element| element.get_field_implementation(context, prefix, false, false))
26-
.collect()
28+
.collect();
29+
30+
let choices: TokenStream = self
31+
.choices
32+
.iter()
33+
.map(|choice| choice.get_field_implementation(context, prefix))
34+
.collect();
35+
36+
quote!(
37+
#elements
38+
#choices
39+
)
2740
}
2841
}
2942

@@ -47,10 +60,21 @@ impl Sequence {
4760
context: &XsdContext,
4861
prefix: &Option<String>,
4962
) -> TokenStream {
50-
self
63+
let elements: TokenStream = self
5164
.elements
5265
.iter()
53-
.map(|element| element.get_field_implementation(context, prefix, true, false))
54-
.collect()
66+
.map(|element| element.get_field_implementation(context, prefix, false, false))
67+
.collect();
68+
69+
let choices: TokenStream = self
70+
.choices
71+
.iter()
72+
.map(|choice| choice.get_field_implementation(context, prefix))
73+
.collect();
74+
75+
quote!(
76+
#elements
77+
#choices
78+
)
5579
}
5680
}

0 commit comments

Comments
 (0)