use quick_xml::reader::Reader;
use serde::{Deserialize, Serialize};
use xml_schema_generator::{into_struct, Options};
const XML: &str = "\
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<library name=\"Evergreen Books & Beyond\">
<book>
<title>To Kill a Mockingbird</title>
<author>Harper Lee</author>
<genre>Drama</genre>
<publication_year>1960</publication_year>
</book>
<book>
<title>One Hundred Years of Solitude</title>
<author>Gabriel Garcia Marquez</author>
</book>
<book>
<title>1984</title>
<author>George Orwell</author>
<genre>Dystopian Fiction</genre>
<publication_year>1949</publication_year>
</book>
</library>
";
#[derive(Serialize, Deserialize)]
pub struct Library {
pub name: String,
#[serde(rename = "$text")]
pub text: Option<String>,
pub book: Vec<Book>,
}
#[derive(Serialize, Deserialize)]
pub struct Book {
pub title: String,
pub author: String,
pub publication_year: Option<String>,
pub genre: Option<String>,
#[serde(rename = "$text")]
pub text: Option<String>,
}
fn main() {
let mut reader = Reader::from_str(XML);
if let Ok(root) = into_struct(&mut reader) {
let struct_as_string = root.to_serde_struct(&Options::serde_xml_rs());
println!("{struct_as_string}"); }
let library: Library = serde_xml_rs::from_str(XML).unwrap();
assert_eq!(3, library.book.len());
}