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

Support for mixed elements #185

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ description = "Examples for YaSerDe project"
documentation = "https://docs.rs/yaserde"

[dependencies]
xml = "0.8.20"
yaserde = {version = "0.10.1", path = "../yaserde", features = ["yaserde_derive"] }
262 changes: 262 additions & 0 deletions examples/src/custom_serializer_ab.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
use yaserde::{YaDeserialize, YaSerialize};

#[derive(Debug, PartialEq, YaDeserialize)]
struct RootElem {
children: Vec<AB>,
}
impl YaSerialize for RootElem {
fn serialize<W: std::io::prelude::Write>(
&self,
writer: &mut yaserde::ser::Serializer<W>,
) -> Result<(), String> {
writer.write("Root {\n").unwrap();
writer.write("/* A|B elements */\n").unwrap();
for e in &self.children {
e.serialize(writer).unwrap();
}
writer.write("}\n").unwrap();
Ok(())
}

fn serialize_attributes(
&self,
attributes: Vec<xml::attribute::OwnedAttribute>,
namespace: xml::namespace::Namespace,
) -> Result<
(
Vec<xml::attribute::OwnedAttribute>,
xml::namespace::Namespace,
),
String,
> {
Ok((attributes, namespace))
}
}

#[derive(Debug, PartialEq)]
// #[derive(YaDeserialize)]
// #[yaserde(rename = "a")]
struct A {
// #[yaserde(attribute)]
attr: String,
}
impl YaSerialize for A {
fn serialize<W: std::io::prelude::Write>(
&self,
writer: &mut yaserde::ser::Serializer<W>,
) -> Result<(), String> {
writer.write("A {").unwrap();
writer
.write(format!("attr: {:?},", self.attr.as_str()).as_str())
.unwrap();
writer.write("}\n").unwrap();
Ok(())
}

fn serialize_attributes(
&self,
attributes: Vec<xml::attribute::OwnedAttribute>,
namespace: xml::namespace::Namespace,
) -> Result<
(
Vec<xml::attribute::OwnedAttribute>,
xml::namespace::Namespace,
),
String,
> {
Ok((attributes, namespace))
}
}

#[derive(Debug, PartialEq)]
// #[derive(YaDeserialize)]
// #[yaserde(rename = "b")]
struct B {}
impl YaSerialize for B {
fn serialize<W: std::io::prelude::Write>(
&self,
writer: &mut yaserde::ser::Serializer<W>,
) -> Result<(), String> {
writer.write("B {}\n").unwrap();
Ok(())
}

fn serialize_attributes(
&self,
attributes: Vec<xml::attribute::OwnedAttribute>,
namespace: xml::namespace::Namespace,
) -> Result<
(
Vec<xml::attribute::OwnedAttribute>,
xml::namespace::Namespace,
),
String,
> {
Ok((attributes, namespace))
}
}

#[derive(Debug, PartialEq)]
// #[derive(Default)]
enum AB {
// #[default]
// None,
A(A),
B(B),
}
impl Default for AB {
fn default() -> Self {
// NOTE: for debugging only; will be None
Self::A(A {
attr: String::from("undefined"),
})
}
}
impl YaDeserialize for AB {
fn deserialize<R: std::io::Read>(
reader: &mut yaserde::de::Deserializer<R>,
) -> Result<Self, String> {
// dispatch to YaDeserialize for element
print!(" - - - - -DISPATCHING");
if let xml::reader::XmlEvent::StartElement { name, .. } = reader.peek()?.to_owned() {
match name.local_name.as_str() {
"a" => {
let deserialized = A { attr: String::from("NOT DESERIALIZED")}; // TODO: A::deserialize(reader)?;
return Ok(Self::A(deserialized));
}
"b" => {
let deserialized = B {}; // TODO: B::deserialize(reader)?;
return Ok(Self::B(deserialized));
}
_ => (),
}
}
Err(format!("Expected a StartElement"))
}
}
impl YaSerialize for AB {
fn serialize<W: std::io::prelude::Write>(
&self,
writer: &mut yaserde::ser::Serializer<W>,
) -> Result<(), String> {
writer.write("/* serialized AB */\n").unwrap();
match self {
// Self::None => {
// writer.write("UndefinedAB").unwrap();
// // return Err(format!("None element cannot be serialized"));
// }
Self::A(a) => {
a.serialize(writer).unwrap();
}
Self::B(b) => {
b.serialize(writer).unwrap();
}
}
writer.write("}\n").unwrap();
Ok(())
}

fn serialize_attributes(
&self,
attributes: Vec<xml::attribute::OwnedAttribute>,
namespace: xml::namespace::Namespace,
) -> Result<
(
Vec<xml::attribute::OwnedAttribute>,
xml::namespace::Namespace,
),
String,
> {
Ok((attributes, namespace))
}
}

#[test]
#[ignore = "TODO: fully support Rust enums (allow multiple XML element occurences and create a Vec<DispatcherEnum>)"]
fn serialize_ab() {
use std::fs;

let content =
fs::read_to_string("tests/data/ab.xml").expect("something went wrong reading the file");
let loaded: RootElem = yaserde::de::from_str(&content).unwrap();
println!("{:?}", &loaded);
let yaserde_conf = yaserde::ser::Config {
indent_string: Some(String::from(" ")),
perform_indent: true,
write_document_declaration: false,
};
let result = yaserde::ser::to_string_with_config(&loaded, &yaserde_conf).unwrap();
println!("\n\nSerialized output:\n{:?}", &result);

let reference = RootElem {
// ab: AB::default(),
children: vec![
AB::A(A {
attr: String::from("hallo 1"),
}),
AB::B(B {}),
AB::A(A {
attr: String::from("hallo 2"),
}),
AB::B(B {}),
AB::A(A {
attr: String::from("hallo 3"),
}),
AB::B(B {}),
AB::B(B {}),
AB::B(B {}),
AB::B(B {}),
AB::A(A {
attr: String::from("hallo 4"),
}),
AB::B(B {}),
],
// a_children: vec![
// A {
// attr: String::from("hallo 1"),
// },
// A {
// attr: String::from("hallo 2"),
// },
// A {
// attr: String::from("hallo 3"),
// },
// A {
// attr: String::from("hallo 4"),
// },
// ],
// b_children: vec![B {}, B {}, B {}, B {}, B {}, B {}, B {}],
};
assert_eq!(&loaded, &reference);

assert_eq!(
&result,
r##"Root {
/* A|B elements */
A{attr = "hallo 1"}
B{}
A{attr = "hallo 2"}
B{}
A{attr = "hallo 3"}
B{}
B{}
B{}
B{}
A{attr = "hallo 4"}
B{}
/* only A elements */
A{attr = "hallo 1"}
A{attr = "hallo 2"}
A{attr = "hallo 3"}
A{attr = "hallo 4"}
/* only B elements */
B{}
B{}
B{}
B{}
B{}
B{}
B{}
}"##,
)
}
1 change: 1 addition & 0 deletions examples/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod bbigras_namespace;
mod boscop;
mod custom_serializer_ab;
mod generic;
mod ln_dom;
mod svd;
18 changes: 18 additions & 0 deletions examples/tests/data/ab.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!--
Description: Example serializing mixed A,B elements
The elements must be in sequence [A,B,A,B,A,B,B,B,B,A,B]
-->

<RootElem>
<a attr="hello 1"/>
<b/>
<a attr="hello 2"/>
<b/>
<a attr="hello 3"/>
<b/>
<b/>
<b/>
<b/>
<a attr="hello 4"/>
<b/>
</RootElem>