YaSerDe
YaSerDe is a framework for serializing and deserializing Rust data structures efficiently and generically from and into XML.
YaSerDe makes it easy to serialize XML documents given an properly annotated struct.
Please refer to the examples directory for the complete code shown below.
Serialize
For instance, let's say that one wants to generate a XML file for the Rust-Embedded community. A well known XML file for microcontrollers is called SVD and it can be defined on YaSerDe via structs like so:
use YaSerialize;
The interspersed #[yaserde()] macros give some indication of what the resulting XML
Will look like, namely, a short snippet of the struct above in XML would be depending on
concrete values passed to the struct (not shown):
(...)
(...)
Notice the important difference in XML output representation between attributes vs
child, since SVD expects information in that particular arrangement. YaSerDe allows that
serialized XML to be valid unlike other Rust XML (de)serialization crates (i.e quick-xml).
Also the last DevAttrs struct field is indeed another struct, so one can chain several
structs to compose the XML structure (again, see examples folder for the complete
example).
Be mindful that the Cargo.toml should not only include yaserde and
yaserde_derive, but also xml-rs and log as your dependencies...
FIXME: Explain better why YaSerDe does not pull xml-rs and log automatically?
[]
# serde = { version = "1.0.123", features = [ "derive" ] }
# quick-xml = { version = "0.21.0", features = [ "serialize" ] }
= "0.5.1"
= "0.5.1"
= "0.8.3"
= "0.4"
Last but not least, in order to have a nice, pretty printed XML output one can do:
// Display pretty printed XML
let yaserde_cfg = yaserde::ser::Config{
perform_indent: true,
.. Default::default()
};
println!("{}", yaserde::ser::to_string_with_config(&dev, &yaserde_cfg).ok().unwrap());
Avoid using either {:?} or {:#?} println! formatters since it'll garble the output of your
XML.