use quick_xml::reader::Reader;
use serde::{Deserialize, Serialize};
use xml_schema_generator::{into_struct, Options};
const XML: &str = "\
To Kill a Mockingbird
Harper Lee
Drama
1960
One Hundred Years of Solitude
Gabriel Garcia Marquez
1984
George Orwell
Dystopian Fiction
1949
";
#[derive(Serialize, Deserialize)]
pub struct Library {
#[serde(rename = "@name")]
pub name: String,
#[serde(rename = "$text")]
pub text: Option,
pub book: Vec,
}
#[derive(Serialize, Deserialize)]
pub struct Book {
pub title: String,
pub author: String,
pub publication_year: Option,
pub genre: Option,
#[serde(rename = "$text")]
pub text: Option,
}
fn main() {
// create struct from XML
let mut reader = Reader::from_str(XML);
if let Ok(root) = into_struct(&mut reader) {
let struct_as_string = root.to_serde_struct(&Options::quick_xml_de());
println!("{}", struct_as_string); // this prints the struct Library and Book as listed above
}
// parse XML into generated struct
let library: Library = quick_xml::de::from_str(XML).unwrap();
assert_eq!(3, library.book.len());
}