From 2827bd404eea3775fec5ed3f76755a07bfcf3bd3 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Mon, 5 Sep 2022 12:59:30 +0200 Subject: [PATCH] Move user-facing deserialization interface out of trait --- instant-xml/src/lib.rs | 9 ++-- instant-xml/tests/de-direct.rs | 23 ++++----- instant-xml/tests/de-nested.rs | 10 ++-- instant-xml/tests/de-ns.rs | 91 +++++++++++++++------------------- instant-xml/tests/entities.rs | 22 ++++---- instant-xml/tests/scalar.rs | 19 ++++--- 6 files changed, 77 insertions(+), 97 deletions(-) diff --git a/instant-xml/src/lib.rs b/instant-xml/src/lib.rs index 5c27b5f..a1ade13 100644 --- a/instant-xml/src/lib.rs +++ b/instant-xml/src/lib.rs @@ -30,11 +30,6 @@ pub enum FieldAttribute<'xml> { } pub trait FromXml<'xml>: Sized { - fn from_xml(input: &'xml str) -> Result { - let mut deserializer = Deserializer::new(input); - Self::deserialize(&mut deserializer) - } - fn deserialize(deserializer: &mut Deserializer<'xml>) -> Result; // If the missing field is of type `Option` then treat is as `None`, @@ -46,6 +41,10 @@ pub trait FromXml<'xml>: Sized { const KIND: Kind; } +pub fn from_str<'xml, T: FromXml<'xml>>(input: &'xml str) -> Result { + T::deserialize(&mut Deserializer::new(input)) +} + pub enum Kind { Scalar, Element(Id<'static>), diff --git a/instant-xml/tests/de-direct.rs b/instant-xml/tests/de-direct.rs index 95297d6..b15c882 100644 --- a/instant-xml/tests/de-direct.rs +++ b/instant-xml/tests/de-direct.rs @@ -1,6 +1,6 @@ use similar_asserts::assert_eq; -use instant_xml::{Error, FromXml}; +use instant_xml::{from_str, Error, FromXml}; #[derive(Debug, Eq, PartialEq, FromXml)] #[xml(ns("URI"))] @@ -13,28 +13,23 @@ struct StructDirectNamespace { fn direct_namespaces() { // Correct direct namespace assert_eq!( - StructDirectNamespace::from_xml( + from_str( "true" - ) - .unwrap(), - StructDirectNamespace { flag: true } + ), + Ok(StructDirectNamespace { flag: true }) ); // Wrong direct namespace assert_eq!( - StructDirectNamespace::from_xml( + from_str( "true" - ) - .unwrap_err(), - Error::MissingValue + ), + Err::(Error::MissingValue) ); // Wrong direct namespace - missing namespace assert_eq!( - StructDirectNamespace::from_xml( - "true" - ) - .unwrap_err(), - Error::MissingValue + from_str("true"), + Err::(Error::MissingValue) ); } diff --git a/instant-xml/tests/de-nested.rs b/instant-xml/tests/de-nested.rs index 17e8f3a..5c7b08e 100644 --- a/instant-xml/tests/de-nested.rs +++ b/instant-xml/tests/de-nested.rs @@ -1,6 +1,6 @@ use similar_asserts::assert_eq; -use instant_xml::FromXml; +use instant_xml::{from_str, FromXml}; #[derive(Debug, Eq, PartialEq, FromXml)] #[xml(ns("URI", bar = "BAZ"))] @@ -22,7 +22,7 @@ struct StructWithCustomFieldFromXml { #[test] fn struct_with_custom_field_from_xml() { assert_eq!( - StructWithCustomFieldFromXml::from_xml("falsetrue").unwrap(), + from_str::("falsetrue").unwrap(), StructWithCustomFieldFromXml { flag: false, flag_attribute: true, @@ -31,7 +31,7 @@ fn struct_with_custom_field_from_xml() { ); // Different order assert_eq!( - StructWithCustomFieldFromXml::from_xml("truefalse").unwrap(), + from_str::("truefalse").unwrap(), StructWithCustomFieldFromXml { flag: false, flag_attribute: true, @@ -41,7 +41,7 @@ fn struct_with_custom_field_from_xml() { // Different prefixes then in definition assert_eq!( - StructWithCustomFieldFromXml::from_xml("falsetrue").unwrap(), + from_str::("falsetrue").unwrap(), StructWithCustomFieldFromXml { flag: false, flag_attribute: true, @@ -50,7 +50,7 @@ fn struct_with_custom_field_from_xml() { ); assert_eq!( - NestedDe::from_xml( + from_str::( "true" ) .unwrap(), diff --git a/instant-xml/tests/de-ns.rs b/instant-xml/tests/de-ns.rs index 9f447d8..7e39b6d 100644 --- a/instant-xml/tests/de-ns.rs +++ b/instant-xml/tests/de-ns.rs @@ -1,6 +1,6 @@ use similar_asserts::assert_eq; -use instant_xml::{Error, FromXml}; +use instant_xml::{from_str, Error, FromXml}; #[derive(Debug, Eq, PartialEq, FromXml)] struct NestedWrongNamespace { @@ -30,52 +30,48 @@ struct StructWithWrongNestedNamespace { fn default_namespaces() { // Default namespace not-nested assert_eq!( - NestedDe::from_xml( - "true" - ) - .unwrap(), - NestedDe { flag: true } + from_str("true"), + Ok(NestedDe { flag: true }) ); // Default namespace not-nested - wrong namespace assert_eq!( - NestedDe::from_xml( + from_str( "true" - ) - .unwrap_err(), - Error::WrongNamespace + ), + Err::(Error::WrongNamespace) ); // Correct child namespace assert_eq!( - StructWithCorrectNestedNamespace::from_xml("true").unwrap(), - StructWithCorrectNestedNamespace { + from_str("true"), + Ok(StructWithCorrectNestedNamespace { test: NestedDe { flag: true } - } + }) ); // Correct child namespace - without child redefinition assert_eq!( - StructWithCorrectNestedNamespace::from_xml("true").unwrap(), - StructWithCorrectNestedNamespace { + from_str("true"), + Ok(StructWithCorrectNestedNamespace { test: NestedDe { flag: true } - } + }) ); // Different child namespace assert_eq!( - StructWithWrongNestedNamespace::from_xml("true").unwrap(), - StructWithWrongNestedNamespace { + from_str("true"), + Ok(StructWithWrongNestedNamespace { test: NestedWrongNamespace { flag: true } - } + }) ); // Wrong child namespace assert_eq!( - StructWithWrongNestedNamespace::from_xml("true").unwrap_err(), - Error::MissingValue + from_str("true"), + Err::(Error::MissingValue) ); } @@ -96,72 +92,65 @@ struct StructOtherNamespace { fn other_namespaces() { // Other namespace not-nested assert_eq!( - NestedOtherNamespace::from_xml( + from_str( "true" - ) - .unwrap(), - NestedOtherNamespace { flag: true } + ), + Ok(NestedOtherNamespace { flag: true }) ); // Other namespace not-nested - wrong defined namespace assert_eq!( - NestedOtherNamespace::from_xml( + from_str( "true" - ) - .unwrap_err(), - Error::WrongNamespace + ), + Err::(Error::WrongNamespace) ); // Other namespace not-nested - wrong parser namespace assert_eq!( - NestedOtherNamespace::from_xml( + from_str( "true" - ) - .unwrap_err(), - Error::MissingValue + ), + Err::(Error::MissingValue) ); // Other namespace not-nested - missing parser prefix assert_eq!( - NestedOtherNamespace::from_xml( + from_str( "true" - ) - .unwrap_err(), - Error::MissingValue + ), + Err::(Error::MissingValue) ); // Correct child other namespace assert_eq!( - StructOtherNamespace::from_xml( + from_str( "true" - ) - .unwrap(), - StructOtherNamespace { + ), + Ok(StructOtherNamespace { test: NestedOtherNamespace { flag: true, } - } + }) ); // Correct child other namespace - without child redefinition assert_eq!( - StructOtherNamespace::from_xml( + from_str( "true" - ) - .unwrap(), - StructOtherNamespace { + ), + Ok(StructOtherNamespace { test: NestedOtherNamespace { flag: true, } - } + }) ); // Wrong child other namespace - without child redefinition assert_eq!( - StructOtherNamespace::from_xml( + from_str( "true" - ) - .unwrap_err(), - Error::WrongNamespace + ), + Err::(Error::WrongNamespace) ); } diff --git a/instant-xml/tests/entities.rs b/instant-xml/tests/entities.rs index 12589e6..c8e5a58 100644 --- a/instant-xml/tests/entities.rs +++ b/instant-xml/tests/entities.rs @@ -2,7 +2,7 @@ use std::borrow::Cow; use similar_asserts::assert_eq; -use instant_xml::{Error, FromXml, ToXml}; +use instant_xml::{from_str, Error, FromXml, ToXml}; #[derive(Debug, PartialEq, Eq, FromXml, ToXml)] #[xml(ns("URI"))] @@ -15,28 +15,26 @@ struct StructSpecialEntities<'a> { #[test] fn escape_back() { assert_eq!( - StructSpecialEntities::from_xml( + from_str( "<>&"'adsad"strstr&" - ) - .unwrap(), - StructSpecialEntities { + ), + Ok(StructSpecialEntities { string: String::from("<>&\"'adsad\""), str: "str", cow: Cow::Owned("str&".to_string()), - } + }) ); // Wrong str char assert_eq!( - StructSpecialEntities::from_xml( + from_str( "<>&"'adsad"str&" - ) - .unwrap_err(), - Error::Other("Unsupported char: str&".to_string()) + ), + Err::(Error::Other("Unsupported char: str&".to_string())) ); // Borrowed - let escape_back = StructSpecialEntities::from_xml( + let escape_back = from_str::( "<>&"'adsad"strstr" ) .unwrap(); @@ -46,7 +44,7 @@ fn escape_back() { } // Owned - let escape_back = StructSpecialEntities::from_xml( + let escape_back = from_str::( "<>&"'adsad"strstr&" ) .unwrap(); diff --git a/instant-xml/tests/scalar.rs b/instant-xml/tests/scalar.rs index 88d85e9..9b806a9 100644 --- a/instant-xml/tests/scalar.rs +++ b/instant-xml/tests/scalar.rs @@ -2,7 +2,7 @@ use std::borrow::Cow; use similar_asserts::assert_eq; -use instant_xml::{FromXml, ToXml}; +use instant_xml::{from_str, FromXml, ToXml}; #[derive(Debug, PartialEq, Eq, FromXml, ToXml)] #[xml(ns("URI"))] @@ -30,11 +30,10 @@ struct StructDeserializerScalars<'a, 'b> { #[test] fn scalars() { assert_eq!( - StructDeserializerScalars::from_xml( + from_str( "true142stringlifetime alifetime bc1.20trueasd123" - ) - .unwrap(), - StructDeserializerScalars{ + ), + Ok(StructDeserializerScalars{ bool_type: true, i8_type: 1, u32_type: 42, @@ -49,15 +48,15 @@ fn scalars() { }, cow: Cow::from("123"), option: None, - } + }) ); // Option none assert_eq!( - StructDeserializerScalars::from_xml( + from_str( "true142stringlifetime alifetime bc1.2trueasd123" - ).unwrap(), - StructDeserializerScalars{ + ), + Ok(StructDeserializerScalars{ bool_type: true, i8_type: 1, u32_type: 42, @@ -72,6 +71,6 @@ fn scalars() { }, cow: Cow::from("123"), option: Some("asd"), - } + }) ); }