use raxb::{value::ConstStr, XmlDeserialize, XmlSerialize};
const DEFAULT_NS: &str = "https://my.example.org/";
const SCHEMA_LOCATION: &str = "https://myexample.org/examplev100 example.xsd";
#[derive(Default, Debug, XmlSerialize, XmlDeserialize)]
#[xml(root = b"a")]
struct A {
#[xml(name = b"product", ty = "attr", value = "H & D")]
product: ConstStr,
#[xml(ns = b"xmlns", name = b"xsi", ty = "attr", value = DEFAULT_NS)]
xmlns_xsi: ConstStr,
#[xml(ns = b"xsi", name = b"schemaLocation", ty = "attr", value = SCHEMA_LOCATION)]
schema_location: ConstStr,
}
#[test]
fn test_const_value_serde() -> anyhow::Result<()> {
let expected_xml = r#"<a product="H & D" xmlns:xsi="https://my.example.org/" xsi:schemaLocation="https://myexample.org/examplev100 example.xsd"/>"#;
let a1 = A::default();
let s = raxb::ser::to_string(&a1)?;
assert_eq!(s, expected_xml);
let a2: A = raxb::de::from_str(expected_xml)?;
assert_eq!(a2.product.as_ref(), "H & D");
assert_eq!(a2.xmlns_xsi.as_ref(), "https://my.example.org/");
assert_eq!(
a2.schema_location.as_ref(),
"https://myexample.org/examplev100 example.xsd"
);
let expected_xml = r#"<a product="H & D" xmlns:xsi="https://my.example.org/" xsi:schemaLocation="https://myexample.org/examplev101 example.xsd"/>"#;
let a2: A = raxb::de::from_str(expected_xml)?;
assert_eq!(a2.product.as_ref(), "H & D");
assert_eq!(a2.xmlns_xsi.as_ref(), "https://my.example.org/");
assert_eq!(
a2.schema_location.as_ref(),
"https://myexample.org/examplev101 example.xsd"
);
Ok(())
}