use std::borrow::Cow; use similar_asserts::assert_eq; use instant_xml::{from_str, FromXml, ToXml}; #[derive(Debug, PartialEq, Eq, FromXml, ToXml)] #[xml(ns("URI"))] struct NestedLifetimes<'a> { flag: bool, str_type_a: Cow<'a, str>, } #[derive(Debug, PartialEq, FromXml, ToXml)] #[xml(ns("URI"))] struct StructDeserializerScalars<'a, 'b> { bool_type: bool, i8_type: i8, u32_type: u32, string_type: String, str_type_a: Cow<'a, str>, str_type_b: Cow<'b, str>, char_type: char, f32_type: f32, nested: NestedLifetimes<'a>, cow: Cow<'a, str>, option: Option>, slice: Cow<'a, [u8]>, } #[test] fn scalars() { assert_eq!( from_str( "true142stringlifetime alifetime bc1.20trueasd123123" ), Ok(StructDeserializerScalars{ bool_type: true, i8_type: 1, u32_type: 42, string_type: "string".to_string(), str_type_a: "lifetime a".into(), str_type_b: "lifetime b".into(), char_type: 'c', f32_type: 1.20, nested: NestedLifetimes { flag: true, str_type_a: "asd".into() }, cow: Cow::from("123"), option: None, slice: Cow::Borrowed(&[1, 2, 3]), }) ); // Option none assert_eq!( from_str( "true142stringlifetime alifetime bc1.2trueasd123123" ), Ok(StructDeserializerScalars{ bool_type: true, i8_type: 1, u32_type: 42, string_type: "string".to_string(), str_type_a: "lifetime a".into(), str_type_b: "lifetime b".into(), char_type: 'c', f32_type: 1.20, nested: NestedLifetimes { flag: true, str_type_a: "asd".into(), }, cow: Cow::from("123"), option: Some("asd".into()), slice: Cow::Borrowed(&[1, 2, 3]), }) ); } #[derive(Debug, FromXml, PartialEq)] struct ScalarElementAttr { s: String, } #[test] fn scalar_element_attr() { assert_eq!( from_str::( "hello" ) .unwrap(), ScalarElementAttr { s: "hello".to_string(), } ); }