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

Implement quickxml support #31

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions xml_schema/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ readme = "../README.md"
exclude = ["/tests"]

[dependencies]
quick-xml = { version = "0.31.0", features = ["serialize"] }
serde = { version = "1.0.193", features = ["serde_derive"] }
xml-schema-derive = { version = "0.3.0", path = "../xml_schema_derive", optional = true }

[dev-dependencies]
Expand Down
10 changes: 4 additions & 6 deletions xml_schema_derive/src/xsd/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,12 @@ impl Implementation for Attribute {
quote!(#rust_type)
};

let attributes = if name == raw_name {
quote!(attribute)
} else {
quote!(attribute, rename=#raw_name)
};
let attribute_name = "@".to_string() + &raw_name;

let attributes = quote!(rename = #attribute_name);

quote!(
#[yaserde(#attributes)]
#[serde(#attributes)]
pub #field_name: #rust_type,
)
}
Expand Down
4 changes: 2 additions & 2 deletions xml_schema_derive/src/xsd/complex_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Implementation for ComplexType {
.map(|complex_content| {
let complex_content_type = complex_content.get_field_implementation(context, prefix);
quote!(
#[yaserde(flatten)]
#[serde(flatten)]
#complex_content_type,
)
})
Expand All @@ -84,7 +84,7 @@ impl Implementation for ComplexType {
quote! {
#docs

#[derive(Clone, Debug, Default, PartialEq, yaserde_derive::YaDeserialize, yaserde_derive::YaSerialize)]
#[derive(Clone, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)]
#namespace_definition
pub struct #struct_name {
#sequence
Expand Down
21 changes: 10 additions & 11 deletions xml_schema_derive/src/xsd/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl Implementation for Element {

quote! {
#docs
#[derive(Clone, Debug, Default, PartialEq, yaserde_derive::YaDeserialize, yaserde_derive::YaSerialize)]
#[derive(Clone, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)]
#namespace_definition
pub struct #struct_name {
#fields
Expand Down Expand Up @@ -101,7 +101,7 @@ impl Element {
pub fn get_field_implementation(
&self,
context: &XsdContext,
prefix: &Option<String>,
_prefix: &Option<String>,
) -> TokenStream {
if self.name.is_empty() {
return quote!();
Expand All @@ -118,10 +118,14 @@ impl Element {

log::info!("Generate element {:?}", name);

let name = if multiple { format!("{name}s") } else { name };
let name = if multiple {
format!("{name}_list")
} else {
name
};

let attribute_name = Ident::new(&name, Span::call_site());
let yaserde_rename = &self.name;
let rename = &self.name;

let rust_type = if let Some(complex_type) = &self.complex_type {
complex_type.get_integrated_implementation(&self.name)
Expand All @@ -148,11 +152,6 @@ impl Element {
rust_type
};

let prefix_attribute = prefix
.as_ref()
.map(|prefix| quote!(, prefix=#prefix))
.unwrap_or_default();

let module = (!context.is_in_sub_module()
&& !self
.kind
Expand All @@ -166,7 +165,7 @@ impl Element {
.unwrap_or_default();

quote! {
#[yaserde(rename=#yaserde_rename #prefix_attribute)]
#[serde(rename=#rename)]
pub #attribute_name: #module#rust_type,
}
}
Expand Down Expand Up @@ -210,7 +209,7 @@ mod tests {
{DOCS}
{DERIVES}
pub struct Volume {{
#[yaserde(flatten)]
#[serde(flatten)]
pub content: xml_schema_types::VolumeType,
}}"#
))
Expand Down
8 changes: 1 addition & 7 deletions xml_schema_derive/src/xsd/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,8 @@ impl Implementation for Extension {
.map(|attribute| attribute.implement(namespace_definition, prefix, context))
.collect();

let inner_attribute = if format!("{rust_type}") == "String" {
quote!(#[yaserde(text)])
} else {
TokenStream::new()
};

quote!(
#inner_attribute
#[serde(rename="$text")]
pub content: #rust_type,
#attributes
)
Expand Down
45 changes: 0 additions & 45 deletions xml_schema_derive/src/xsd/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,51 +94,6 @@ mod tests {
pub struct Parent {
pub items: Vec <String>
}

impl yaserde::YaDeserialize for Parent {
fn deserialize<R: std::io::Read>(reader: &mut yaserde::de::Deserializer<R>) -> Result<Self, String> {
loop {
match reader.next_event()? {
xml::reader::XmlEvent::StartElement{..} => { }
xml::reader::XmlEvent::Characters(ref text_content) => {
let items: Vec<String> = text_content.split(' ')
.map(|item| item.to_owned())
.map(|item| item.parse().unwrap())
.collect();

return Ok(Parent{items});
}
_ => { break; }
}
}

Err ("Unable to parse attribute" . to_string ())
}
}

impl yaserde::YaSerialize for Parent {
fn serialize<W: std::io::Write> (&self, writer: &mut yaserde::ser::Serializer<W>) -> Result<(), String> {
let content = self
.items
.iter()
.map(|item| item.to_string())
.collect:: <Vec<String>>().join(" ");

let data_event = xml::writer::XmlEvent::characters(&content);

writer.write(data_event).map_err(|e| e.to_string())? ;

Ok (())
}

fn serialize_attributes(
&self,
mut source_attributes: Vec<xml::attribute::OwnedAttribute> ,
mut source_namespace: xml::namespace::Namespace
) -> Result<(Vec<xml::attribute::OwnedAttribute> , xml::namespace::Namespace), String> {
Ok((source_attributes , source_namespace))
}
}
"#).unwrap();

assert_eq!(implementation.to_string(), expected.to_string());
Expand Down
2 changes: 1 addition & 1 deletion xml_schema_derive/src/xsd/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn generate_namespace_definition(
),
(Some(prefix), Some(target_namespace)) => {
let namespace = format!("{prefix}: {target_namespace}");
quote!(#[yaserde(prefix=#prefix, namespace=#namespace)])
quote!(#[serde(default)])
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions xml_schema_derive/src/xsd/simple_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ impl Implementation for SimpleType {
}

quote!(
#[derive(Clone, Debug, Default, PartialEq, yaserde_derive::YaDeserialize, yaserde_derive::YaSerialize)]
#[derive(Clone, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)]
#namespace_definition
pub struct #struct_name {
#[yaserde(text)]
pub content: std::string::String,
}
pub struct #struct_name (
#[serde(rename="$text")]
pub std::string::String
);
)
}
}
Expand Down