-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from lirkwood/main
Added support for all element.
- Loading branch information
Showing
9 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use std::cell::RefCell; | ||
|
||
use roxmltree::Node; | ||
|
||
use crate::parser::node_parser::parse_node; | ||
use crate::parser::types::{RsEntity, Struct, StructField, TypeModifier}; | ||
use crate::parser::utils::{enum_to_field, get_documentation, get_parent_name}; | ||
use crate::parser::xsd_elements::{ElementType, XsdNode}; | ||
|
||
pub fn parse_all(node: &Node, parent: &Node) -> RsEntity { | ||
let name = get_parent_name(node); | ||
RsEntity::Struct(Struct { | ||
name: name.into(), | ||
comment: get_documentation(parent), | ||
subtypes: vec![], | ||
fields: RefCell::new(elements_to_fields(node, name)), | ||
..Default::default() | ||
}) | ||
} | ||
|
||
fn elements_to_fields(choice: &Node, parent_name: &str) -> Vec<StructField> { | ||
choice | ||
.children() | ||
.filter(|n| n.is_element() && n.xsd_type() != ElementType::Annotation) | ||
.map(|n| match parse_node(&n, choice) { | ||
RsEntity::StructField(mut sf) => { | ||
if sf.type_name.ends_with(parent_name) { | ||
sf.type_modifiers.push(TypeModifier::Recursive) | ||
} | ||
sf | ||
} | ||
RsEntity::Enum(mut en) => { | ||
en.name = format!("{}Choice", parent_name); | ||
enum_to_field(en) | ||
} | ||
_ => unreachable!("\nError: {:?}\n{:?}", n, parse_node(&n, choice)), | ||
}) | ||
.collect() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod all; | ||
mod any; | ||
mod any_attribute; | ||
mod attribute; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<exam:Foo xmlns:exam="http://example.com"> | ||
<exam:TwiceOrMore>111</exam:TwiceOrMore> | ||
<exam:Once>222</exam:Once> | ||
<exam:TwiceOrMore>333</exam:TwiceOrMore> | ||
<exam:OnceSpecify>444</exam:OnceSpecify> | ||
<exam:TwiceOrMore>555</exam:TwiceOrMore> | ||
</exam:Foo> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#[derive(Default, PartialEq, Debug, YaSerialize, YaDeserialize)] | ||
#[yaserde(prefix = "tns", namespace = "tns: http://example.com")] | ||
pub struct FooType { | ||
#[yaserde(prefix = "tns", rename = "Once")] | ||
pub once: i32, | ||
|
||
#[yaserde(prefix = "tns", rename = "Optional")] | ||
pub optional: Option<i32>, | ||
|
||
#[yaserde(prefix = "tns", rename = "OnceSpecify")] | ||
pub once_specify: i32, | ||
|
||
#[yaserde(prefix = "tns", rename = "TwiceOrMore")] | ||
pub twice_or_more: Vec<i32>, | ||
} | ||
|
||
impl Validate for FooType {} | ||
|
||
// pub type Foo = FooType; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" | ||
xmlns:tns="http://example.com" | ||
targetNamespace="http://example.com" | ||
elementFormDefault="qualified"> | ||
<xs:complexType name="FooType"> | ||
<xs:all> | ||
<xs:element name="Once" type="xs:int"/> | ||
<xs:element name="Optional" type="xs:int" minOccurs="0"/> | ||
<xs:element name="OnceSpecify" type="xs:int" minOccurs="1"/> | ||
<xs:element name="TwiceOrMore" type="xs:int" minOccurs="2" maxOccurs="unbounded"/> | ||
</xs:all> | ||
</xs:complexType> | ||
|
||
<xs:element name="Foo" type="tns:FooType"/> | ||
</xs:schema> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use super::utils; | ||
|
||
#[test] | ||
fn deserialization_works() { | ||
mod expected { | ||
use xsd_parser::generator::validator::Validate; | ||
use yaserde_derive::{YaDeserialize, YaSerialize}; | ||
|
||
include!("expected.rs"); | ||
} | ||
|
||
let ser = include_str!("example.xml"); | ||
|
||
let de: expected::FooType = yaserde::de::from_str(ser).unwrap(); | ||
|
||
assert_eq!( | ||
de, | ||
expected::FooType { | ||
once: 222, | ||
optional: None, | ||
once_specify: 444, | ||
twice_or_more: vec![111, 333, 555] | ||
} | ||
); | ||
} | ||
|
||
#[test] | ||
fn generator_does_not_panic() { | ||
println!("{}", utils::generate(include_str!("input.xsd"))) | ||
} | ||
|
||
#[test] | ||
fn generator_output_has_correct_ast() { | ||
utils::ast_test(include_str!("input.xsd"), include_str!("expected.rs")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#[macro_use] | ||
mod utils; | ||
mod all; | ||
mod any; | ||
mod choice; | ||
mod complex_type; | ||
|