Move user-facing deserialization interface out of trait
This commit is contained in:
parent
921e094925
commit
2827bd404e
|
@ -30,11 +30,6 @@ pub enum FieldAttribute<'xml> {
|
|||
}
|
||||
|
||||
pub trait FromXml<'xml>: Sized {
|
||||
fn from_xml(input: &'xml str) -> Result<Self, Error> {
|
||||
let mut deserializer = Deserializer::new(input);
|
||||
Self::deserialize(&mut deserializer)
|
||||
}
|
||||
|
||||
fn deserialize(deserializer: &mut Deserializer<'xml>) -> Result<Self, Error>;
|
||||
|
||||
// If the missing field is of type `Option<T>` 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, Error> {
|
||||
T::deserialize(&mut Deserializer::new(input))
|
||||
}
|
||||
|
||||
pub enum Kind {
|
||||
Scalar,
|
||||
Element(Id<'static>),
|
||||
|
|
|
@ -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(
|
||||
"<StructDirectNamespace xmlns=\"URI\"><flag xmlns=\"BAZ\">true</flag></StructDirectNamespace>"
|
||||
)
|
||||
.unwrap(),
|
||||
StructDirectNamespace { flag: true }
|
||||
),
|
||||
Ok(StructDirectNamespace { flag: true })
|
||||
);
|
||||
|
||||
// Wrong direct namespace
|
||||
assert_eq!(
|
||||
StructDirectNamespace::from_xml(
|
||||
from_str(
|
||||
"<StructDirectNamespace xmlns=\"URI\"><flag xmlns=\"WRONG\">true</flag></StructDirectNamespace>"
|
||||
)
|
||||
.unwrap_err(),
|
||||
Error::MissingValue
|
||||
),
|
||||
Err::<StructDirectNamespace, _>(Error::MissingValue)
|
||||
);
|
||||
|
||||
// Wrong direct namespace - missing namespace
|
||||
assert_eq!(
|
||||
StructDirectNamespace::from_xml(
|
||||
"<StructDirectNamespace xmlns=\"URI\"><flag>true</flag></StructDirectNamespace>"
|
||||
)
|
||||
.unwrap_err(),
|
||||
Error::MissingValue
|
||||
from_str("<StructDirectNamespace xmlns=\"URI\"><flag>true</flag></StructDirectNamespace>"),
|
||||
Err::<StructDirectNamespace, _>(Error::MissingValue)
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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("<StructWithCustomFieldFromXml flag_attribute=\"true\" xmlns=\"URI\" xmlns:bar=\"BAZ\" xmlns:foo=\"BAR\"><bar:flag>false</bar:flag><NestedDe><bar:flag>true</bar:flag></NestedDe></StructWithCustomFieldFromXml>").unwrap(),
|
||||
from_str::<StructWithCustomFieldFromXml>("<StructWithCustomFieldFromXml flag_attribute=\"true\" xmlns=\"URI\" xmlns:bar=\"BAZ\" xmlns:foo=\"BAR\"><bar:flag>false</bar:flag><NestedDe><bar:flag>true</bar:flag></NestedDe></StructWithCustomFieldFromXml>").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("<StructWithCustomFieldFromXml xmlns=\"URI\" xmlns:bar=\"BAZ\" xmlns:foo=\"BAR\" flag_attribute=\"true\"><NestedDe><bar:flag>true</bar:flag></NestedDe><bar:flag>false</bar:flag></StructWithCustomFieldFromXml>").unwrap(),
|
||||
from_str::<StructWithCustomFieldFromXml>("<StructWithCustomFieldFromXml xmlns=\"URI\" xmlns:bar=\"BAZ\" xmlns:foo=\"BAR\" flag_attribute=\"true\"><NestedDe><bar:flag>true</bar:flag></NestedDe><bar:flag>false</bar:flag></StructWithCustomFieldFromXml>").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("<StructWithCustomFieldFromXml flag_attribute=\"true\" xmlns=\"URI\" xmlns:grr=\"BAZ\" xmlns:foo=\"BAR\"><grr:flag>false</grr:flag><NestedDe><grr:flag>true</grr:flag></NestedDe></StructWithCustomFieldFromXml>").unwrap(),
|
||||
from_str::<StructWithCustomFieldFromXml>("<StructWithCustomFieldFromXml flag_attribute=\"true\" xmlns=\"URI\" xmlns:grr=\"BAZ\" xmlns:foo=\"BAR\"><grr:flag>false</grr:flag><NestedDe><grr:flag>true</grr:flag></NestedDe></StructWithCustomFieldFromXml>").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::<NestedDe>(
|
||||
"<NestedDe xmlns=\"URI\" xmlns:bar=\"BAZ\"><bar:flag>true</bar:flag></NestedDe>"
|
||||
)
|
||||
.unwrap(),
|
||||
|
|
|
@ -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(
|
||||
"<NestedDe xmlns=\"URI\" xmlns:bar=\"BAZ\"><bar:flag>true</bar:flag></NestedDe>"
|
||||
)
|
||||
.unwrap(),
|
||||
NestedDe { flag: true }
|
||||
from_str("<NestedDe xmlns=\"URI\" xmlns:bar=\"BAZ\"><bar:flag>true</bar:flag></NestedDe>"),
|
||||
Ok(NestedDe { flag: true })
|
||||
);
|
||||
|
||||
// Default namespace not-nested - wrong namespace
|
||||
assert_eq!(
|
||||
NestedDe::from_xml(
|
||||
from_str(
|
||||
"<NestedDe xmlns=\"WRONG\" xmlns:bar=\"BAZ\"><bar:flag>true</bar:flag></NestedDe>"
|
||||
)
|
||||
.unwrap_err(),
|
||||
Error::WrongNamespace
|
||||
),
|
||||
Err::<NestedDe, _>(Error::WrongNamespace)
|
||||
);
|
||||
|
||||
// Correct child namespace
|
||||
assert_eq!(
|
||||
StructWithCorrectNestedNamespace::from_xml("<StructWithCorrectNestedNamespace xmlns=\"URI\" xmlns:bar=\"BAZ\"><NestedDe xmlns=\"URI\" xmlns:bar=\"BAZ\"><bar:flag>true</bar:flag></NestedDe></StructWithCorrectNestedNamespace>").unwrap(),
|
||||
StructWithCorrectNestedNamespace {
|
||||
from_str("<StructWithCorrectNestedNamespace xmlns=\"URI\" xmlns:bar=\"BAZ\"><NestedDe xmlns=\"URI\" xmlns:bar=\"BAZ\"><bar:flag>true</bar:flag></NestedDe></StructWithCorrectNestedNamespace>"),
|
||||
Ok(StructWithCorrectNestedNamespace {
|
||||
test: NestedDe { flag: true }
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
// Correct child namespace - without child redefinition
|
||||
assert_eq!(
|
||||
StructWithCorrectNestedNamespace::from_xml("<StructWithCorrectNestedNamespace xmlns=\"URI\" xmlns:bar=\"BAZ\"><NestedDe><bar:flag>true</bar:flag></NestedDe></StructWithCorrectNestedNamespace>").unwrap(),
|
||||
StructWithCorrectNestedNamespace {
|
||||
from_str("<StructWithCorrectNestedNamespace xmlns=\"URI\" xmlns:bar=\"BAZ\"><NestedDe><bar:flag>true</bar:flag></NestedDe></StructWithCorrectNestedNamespace>"),
|
||||
Ok(StructWithCorrectNestedNamespace {
|
||||
test: NestedDe { flag: true }
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
// Different child namespace
|
||||
assert_eq!(
|
||||
StructWithWrongNestedNamespace::from_xml("<StructWithWrongNestedNamespace xmlns=\"URI\" xmlns:dar=\"BAZ\"><NestedWrongNamespace xmlns=\"\"><flag>true</flag></NestedWrongNamespace></StructWithWrongNestedNamespace>").unwrap(),
|
||||
StructWithWrongNestedNamespace {
|
||||
from_str("<StructWithWrongNestedNamespace xmlns=\"URI\" xmlns:dar=\"BAZ\"><NestedWrongNamespace xmlns=\"\"><flag>true</flag></NestedWrongNamespace></StructWithWrongNestedNamespace>"),
|
||||
Ok(StructWithWrongNestedNamespace {
|
||||
test: NestedWrongNamespace {
|
||||
flag: true
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
// Wrong child namespace
|
||||
assert_eq!(
|
||||
StructWithWrongNestedNamespace::from_xml("<StructWithWrongNestedNamespace xmlns=\"URI\" xmlns:dar=\"BAZ\"><NestedWrongNamespace><flag>true</flag></NestedWrongNamespace></StructWithWrongNestedNamespace>").unwrap_err(),
|
||||
Error::MissingValue
|
||||
from_str("<StructWithWrongNestedNamespace xmlns=\"URI\" xmlns:dar=\"BAZ\"><NestedWrongNamespace><flag>true</flag></NestedWrongNamespace></StructWithWrongNestedNamespace>"),
|
||||
Err::<StructWithWrongNestedNamespace, _>(Error::MissingValue)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -96,72 +92,65 @@ struct StructOtherNamespace {
|
|||
fn other_namespaces() {
|
||||
// Other namespace not-nested
|
||||
assert_eq!(
|
||||
NestedOtherNamespace::from_xml(
|
||||
from_str(
|
||||
"<NestedOtherNamespace xmlns=\"URI\" xmlns:bar=\"BAZ\"><bar:flag>true</bar:flag></NestedOtherNamespace>"
|
||||
)
|
||||
.unwrap(),
|
||||
NestedOtherNamespace { flag: true }
|
||||
),
|
||||
Ok(NestedOtherNamespace { flag: true })
|
||||
);
|
||||
|
||||
// Other namespace not-nested - wrong defined namespace
|
||||
assert_eq!(
|
||||
NestedOtherNamespace::from_xml(
|
||||
from_str(
|
||||
"<NestedOtherNamespace xmlns=\"URI\" xmlns:bar=\"BAZ\"><wrong:flag>true</wrong:flag></NestedOtherNamespace>"
|
||||
)
|
||||
.unwrap_err(),
|
||||
Error::WrongNamespace
|
||||
),
|
||||
Err::<NestedOtherNamespace, _>(Error::WrongNamespace)
|
||||
);
|
||||
|
||||
// Other namespace not-nested - wrong parser namespace
|
||||
assert_eq!(
|
||||
NestedOtherNamespace::from_xml(
|
||||
from_str(
|
||||
"<NestedOtherNamespace xmlns=\"URI\" xmlns:bar=\"WRONG\"><bar:flag>true</bar:flag></NestedOtherNamespace>"
|
||||
)
|
||||
.unwrap_err(),
|
||||
Error::MissingValue
|
||||
),
|
||||
Err::<NestedOtherNamespace, _>(Error::MissingValue)
|
||||
);
|
||||
|
||||
// Other namespace not-nested - missing parser prefix
|
||||
assert_eq!(
|
||||
NestedOtherNamespace::from_xml(
|
||||
from_str(
|
||||
"<NestedOtherNamespace xmlns=\"URI\" xmlns:bar=\"BAR\"><flag>true</flag></NestedOtherNamespace>"
|
||||
)
|
||||
.unwrap_err(),
|
||||
Error::MissingValue
|
||||
),
|
||||
Err::<NestedOtherNamespace, _>(Error::MissingValue)
|
||||
);
|
||||
|
||||
// Correct child other namespace
|
||||
assert_eq!(
|
||||
StructOtherNamespace::from_xml(
|
||||
from_str(
|
||||
"<StructOtherNamespace xmlns=\"URI\" xmlns:bar=\"BAZ\"><NestedOtherNamespace xmlns=\"URI\" xmlns:bar=\"BAZ\"><bar:flag>true</bar:flag></NestedOtherNamespace></StructOtherNamespace>"
|
||||
)
|
||||
.unwrap(),
|
||||
StructOtherNamespace {
|
||||
),
|
||||
Ok(StructOtherNamespace {
|
||||
test: NestedOtherNamespace {
|
||||
flag: true,
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
// Correct child other namespace - without child redefinition
|
||||
assert_eq!(
|
||||
StructOtherNamespace::from_xml(
|
||||
from_str(
|
||||
"<StructOtherNamespace xmlns=\"URI\" xmlns:bar=\"BAZ\"><NestedOtherNamespace><bar:flag>true</bar:flag></NestedOtherNamespace></StructOtherNamespace>"
|
||||
)
|
||||
.unwrap(),
|
||||
StructOtherNamespace {
|
||||
),
|
||||
Ok(StructOtherNamespace {
|
||||
test: NestedOtherNamespace {
|
||||
flag: true,
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
// Wrong child other namespace - without child redefinition
|
||||
assert_eq!(
|
||||
StructOtherNamespace::from_xml(
|
||||
from_str(
|
||||
"<StructOtherNamespace xmlns=\"URI\" xmlns:bar=\"BAZ\"><NestedOtherNamespace><wrong:flag>true</wrong:flag></NestedOtherNamespace></StructOtherNamespace>"
|
||||
)
|
||||
.unwrap_err(),
|
||||
Error::WrongNamespace
|
||||
),
|
||||
Err::<StructOtherNamespace, _>(Error::WrongNamespace)
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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(
|
||||
"<StructSpecialEntities xmlns=\"URI\"><string><>&"'adsad"</string><str>str</str><cow>str&</cow></StructSpecialEntities>"
|
||||
)
|
||||
.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(
|
||||
"<StructSpecialEntities xmlns=\"URI\"><string><>&"'adsad"</string><str>str&</str></StructSpecialEntities>"
|
||||
)
|
||||
.unwrap_err(),
|
||||
Error::Other("Unsupported char: str&".to_string())
|
||||
),
|
||||
Err::<StructSpecialEntities, _>(Error::Other("Unsupported char: str&".to_string()))
|
||||
);
|
||||
|
||||
// Borrowed
|
||||
let escape_back = StructSpecialEntities::from_xml(
|
||||
let escape_back = from_str::<StructSpecialEntities>(
|
||||
"<StructSpecialEntities xmlns=\"URI\"><string><>&"'adsad"</string><str>str</str><cow>str</cow></StructSpecialEntities>"
|
||||
)
|
||||
.unwrap();
|
||||
|
@ -46,7 +44,7 @@ fn escape_back() {
|
|||
}
|
||||
|
||||
// Owned
|
||||
let escape_back = StructSpecialEntities::from_xml(
|
||||
let escape_back = from_str::<StructSpecialEntities>(
|
||||
"<StructSpecialEntities xmlns=\"URI\"><string><>&"'adsad"</string><str>str</str><cow>str&</cow></StructSpecialEntities>"
|
||||
)
|
||||
.unwrap();
|
||||
|
|
|
@ -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(
|
||||
"<StructDeserializerScalars xmlns=\"URI\"><bool_type>true</bool_type><i8_type>1</i8_type><u32_type>42</u32_type><string_type>string</string_type><str_type_a>lifetime a</str_type_a><str_type_b>lifetime b</str_type_b><char_type>c</char_type><f32_type>1.20</f32_type><NestedLifetimes><flag>true</flag><str_type_a>asd</str_type_a></NestedLifetimes><cow>123</cow></StructDeserializerScalars>"
|
||||
)
|
||||
.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(
|
||||
"<StructDeserializerScalars xmlns=\"URI\"><bool_type>true</bool_type><i8_type>1</i8_type><u32_type>42</u32_type><string_type>string</string_type><str_type_a>lifetime a</str_type_a><str_type_b>lifetime b</str_type_b><char_type>c</char_type><f32_type>1.2</f32_type><NestedLifetimes><flag>true</flag><str_type_a>asd</str_type_a></NestedLifetimes><cow>123</cow><option>asd</option></StructDeserializerScalars>"
|
||||
).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"),
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue