use quick_xml::name::Namespace; use rustical_xml::de::XmlDocument; use rustical_xml::{NamespaceOwned, Unparsed, XmlDeserialize, XmlRootTag}; use std::collections::HashSet; #[test] fn test_struct_text_field() { #[derive(Debug, XmlDeserialize, XmlRootTag, PartialEq)] #[xml(root = "document")] struct Document { #[xml(ty = "text")] text: String, #[xml(ty = "text")] text2: String, } let doc = Document::parse_str(r#"Hello!"#).unwrap(); assert_eq!( doc, Document { text: "Hello!".to_owned(), text2: "Hello!".to_owned() } ); } #[test] fn test_struct_document() { #[derive(Debug, XmlDeserialize, XmlRootTag, PartialEq)] #[xml(root = "document")] struct Document { child: Child, } #[derive(Debug, XmlDeserialize, PartialEq, Default)] struct Child { #[xml(ty = "text")] text: String, } let doc = Document::parse_str(r#"Hello!"#).unwrap(); assert_eq!( doc, Document { child: Child { text: "Hello!".to_owned() } } ); } #[test] fn test_struct_rename_field() { #[derive(Debug, XmlDeserialize, XmlRootTag, PartialEq)] #[xml(root = "document")] struct Document { #[xml(rename = "ok-wow")] child: Child, } #[derive(Debug, XmlDeserialize, PartialEq, Default)] struct Child { #[xml(ty = "text")] text: String, } let doc = Document::parse_str(r#"Hello!"#).unwrap(); assert_eq!( doc, Document { child: Child { text: "Hello!".to_owned() }, } ); } #[test] fn test_struct_optional_field() { #[derive(Debug, XmlDeserialize, XmlRootTag, PartialEq)] #[xml(root = "document")] struct Document { child: Option, } #[derive(Debug, XmlDeserialize, PartialEq, Default)] struct Child; let doc = Document::parse_str(r#""#).unwrap(); assert_eq!(doc, Document { child: Some(Child) }); let doc = Document::parse_str(r#""#).unwrap(); assert_eq!(doc, Document { child: None }); } #[test] fn test_struct_vec() { #[derive(Debug, XmlDeserialize, XmlRootTag, PartialEq)] #[xml(root = "document")] struct Document { #[xml(rename = "child", flatten)] children: Vec, } #[derive(Debug, XmlDeserialize, PartialEq, Default)] struct Child; let doc = Document::parse_str( r#" "#, ) .unwrap(); assert_eq!( doc, Document { children: vec![Child, Child] } ); } #[test] fn test_struct_set() { #[derive(Debug, XmlDeserialize, XmlRootTag, PartialEq)] #[xml(root = "document")] struct Document { #[xml(rename = "child", flatten)] children: HashSet, } #[derive(Debug, XmlDeserialize, PartialEq, Default, Eq, Hash)] struct Child; let doc = Document::parse_str( r#" "#, ) .unwrap(); assert_eq!( doc, Document { children: HashSet::from_iter(vec![Child].into_iter()) } ); } #[test] fn test_struct_ns() { const NS_HELLO: Namespace = Namespace(b"hello"); #[derive(Debug, XmlDeserialize, XmlRootTag, PartialEq)] #[xml(root = "document")] struct Document { #[xml(ns = "NS_HELLO")] child: (), } let doc = Document::parse_str(r#""#).unwrap(); assert_eq!(doc, Document { child: () }); } #[test] fn test_struct_attr() { const NS_HELLO: Namespace = Namespace(b"hello"); #[derive(Debug, XmlDeserialize, XmlRootTag, PartialEq)] #[xml(root = "document")] struct Document { #[xml(ns = "NS_HELLO")] child: (), #[xml(ty = "attr", default = "Default::default")] test: String, #[xml(ty = "attr")] number: usize, } let doc = Document::parse_str( r#""#, ) .unwrap(); assert_eq!( doc, Document { child: (), test: "hello!".to_owned(), number: 2 } ); } #[test] fn test_struct_generics() { #[derive(XmlDeserialize, XmlRootTag)] #[xml(root = "document")] struct Document { #[allow(dead_code)] child: T, } let _ = Document::::parse_str( r#" Hello!

Nice

"#, ) .unwrap(); } #[test] fn test_struct_unparsed() { #[derive(XmlDeserialize, XmlRootTag)] #[xml(root = "document")] struct Document { #[allow(dead_code)] child: Unparsed, } let _ = Document::parse_str( r#" Hello!

Nice

"#, ) .unwrap(); } #[test] fn test_xml_values() { #[derive(XmlDeserialize, XmlRootTag, PartialEq, Debug)] #[xml(root = "document")] struct Document { href: String, } let doc = Document::parse_str( r#" /asd "#, ) .unwrap(); assert_eq!( doc, Document { href: "/asd".to_owned() } ); } #[test] fn test_xml_cdata() { #[derive(XmlDeserialize, XmlRootTag, PartialEq, Debug)] #[xml(root = "document")] struct Document { #[xml(ty = "text")] hello: String, href: String, okay: String, } let doc = Document::parse_str( r#" nice>text "#, ) .unwrap(); assert_eq!( doc, Document { hello: "some text".to_owned(), href: "some stuff".to_owned(), okay: "nice>text".to_owned() } ); } #[test] fn test_quickxml_bytesref() { let gt = quick_xml::events::BytesRef::new("gt"); assert!(!gt.is_char_ref()); let result = if !gt.is_char_ref() { quick_xml::escape::resolve_xml_entity(>.xml_content().unwrap()) .unwrap() .to_string() } else { gt.xml_content().unwrap().to_string() }; assert_eq!(result, ">"); } #[test] fn test_struct_xml_decl() { #[derive(Debug, XmlDeserialize, XmlRootTag, PartialEq)] #[xml(root = "document")] struct Document { child: Child, } #[derive(Debug, XmlDeserialize, PartialEq, Default)] struct Child { #[xml(ty = "text")] text: String, } let doc = Document::parse_str( r#" Hello!&"#, ) .unwrap(); assert_eq!( doc, Document { child: Child { text: "Hello!&".to_owned() } } ); } #[test] fn test_struct_tuple() { #[derive(Debug, XmlDeserialize, XmlRootTag, PartialEq)] #[xml(root = "document")] struct Document { child: Child, } #[derive(Debug, XmlDeserialize, PartialEq, Default)] struct Child(#[xml(ty = "tag_name")] String, #[xml(ty = "text")] String); let doc = Document::parse_str( r#" Hello!"#, ) .unwrap(); assert_eq!( doc, Document { child: Child("child".to_owned(), "Hello!".to_owned()) } ); } #[test] fn test_struct_untagged_ns() { #[derive(Debug, XmlDeserialize, XmlRootTag, PartialEq)] #[xml(root = "document")] struct Document { #[xml(ty = "untagged")] child: Child, } #[derive(Debug, XmlDeserialize, PartialEq, Default)] struct Child( #[xml(ty = "tag_name")] String, #[xml(ty = "namespace")] Option, ); let doc = Document::parse_str( r#" "#, ) .unwrap(); assert_eq!( doc, Document { child: Child("test".to_owned(), Some("hello".to_string().into())) } ); }