use anyhow::Context;
use serde::{Deserialize, Serialize};
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename = "MetaInformation")]
pub struct MetaInformation {
#[serde(rename = "Property", default)]
pub property: Vec<MetaProperty>,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MetaProperty {
#[serde(rename = "@name")]
pub name: String,
#[serde(rename = "$text")]
pub property_text: String,
}
impl MetaInformation {
#[allow(dead_code)]
pub fn from_xml(xml_str: &str) -> anyhow::Result<MetaInformation> {
let result: MetaInformation = quick_xml::de::from_str(xml_str)
.map_err(anyhow::Error::msg)
.context("Failed to parse XML string")?;
Ok(result)
}
#[allow(dead_code)]
pub fn from_json(json_str: &str) -> anyhow::Result<MetaInformation> {
let result: MetaInformation = serde_json::from_str(json_str)
.map_err(anyhow::Error::msg)
.context("Failed to parse JSON string")?;
Ok(result)
}
#[allow(dead_code)]
pub fn detach(&mut self) -> anyhow::Result<()> {
Ok(())
}
#[allow(dead_code)]
pub fn to_json(&self) -> anyhow::Result<String> {
let json_str = serde_json::to_string(&self)?;
Ok(json_str)
}
#[allow(dead_code)]
pub fn to_xml(&self) -> anyhow::Result<String> {
let xml_str = quick_xml::se::to_string(&self)?;
Ok(xml_str)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
pub fn test_meta_informations() {
const META_INFORMATION: &str = r#"<MetaInformation>
<Property name="gldf_rs_file_product.xml">something</Property>
</MetaInformation>"#;
let mut meta = MetaInformation::from_xml(META_INFORMATION).unwrap();
let meta_json = meta.to_json();
let meta_xml = meta.to_xml();
let meta_from_json = MetaInformation::from_json(&meta_json.unwrap());
assert_eq!(meta, meta_from_json.unwrap());
let meta_from_xml = MetaInformation::from_xml(&meta_xml.unwrap());
assert_eq!(meta, meta_from_xml.unwrap());
let property = &meta.property[0];
let new_property = MetaProperty {
name: "test".to_string(),
property_text: "test".to_string(),
};
let new_properties: Vec<MetaProperty> = vec![property.clone(), new_property];
meta.property = new_properties;
println!("{:?}", meta.to_xml());
let meta_from_json = MetaInformation::from_json(&meta.clone().to_json().unwrap());
assert_eq!(&meta, meta_from_json.as_ref().unwrap());
let meta_from_xml = MetaInformation::from_xml(&meta.clone().to_xml().unwrap());
assert_eq!(&meta, meta_from_xml.as_ref().unwrap());
println!("{}", meta_from_json.as_ref().unwrap().to_json().unwrap());
println!("{}", meta_from_xml.as_ref().unwrap().to_xml().unwrap());
}
}