use quick_xml::reader::Reader; use serde::{Deserialize, Serialize}; use xml_schema_generator::{extend_struct, into_struct, Options}; const XML1: &str = "\ To Kill a Mockingbird Harper Lee 1960 "; const XML2: &str = "\ 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 first XML let root = match into_struct(&mut Reader::from_str(XML1)) { Ok(root) => root, Err(_) => panic!("expected to successfully parse into struct"), }; // create struct with structure from second XML let root = match extend_struct(&mut Reader::from_str(XML2), root) { Ok(root) => root, Err(_) => panic!("expected to successfully parse second xml into struct"), }; 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(XML1).unwrap(); assert_eq!(1, library.book.len()); let library: Library = quick_xml::de::from_str(XML2).unwrap(); assert_eq!(2, library.book.len()); }