xso-proc: add support for parsing attributes into Strings
This is bare-bones and is missing many features which we intend to add in future commits, such as parsing from attributes whose names differ from the field names and parsing into non-String types.
This commit is contained in:
parent
183bef5cf6
commit
212c5c4a83
10 changed files with 665 additions and 50 deletions
|
|
@ -183,3 +183,48 @@ fn namespace_lit_roundtrip() {
|
|||
};
|
||||
roundtrip_full::<NamespaceLit>("<baz xmlns='urn:example:ns2'/>");
|
||||
}
|
||||
|
||||
#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
|
||||
#[xml(namespace = NS1, name = "attr")]
|
||||
struct RequiredAttribute {
|
||||
#[xml(attribute)]
|
||||
foo: String,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn required_attribute_roundtrip() {
|
||||
#[allow(unused_imports)]
|
||||
use std::{
|
||||
option::Option::{None, Some},
|
||||
result::Result::{Err, Ok},
|
||||
};
|
||||
roundtrip_full::<RequiredAttribute>("<attr xmlns='urn:example:ns1' foo='bar'/>");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn required_attribute_positive() {
|
||||
#[allow(unused_imports)]
|
||||
use std::{
|
||||
option::Option::{None, Some},
|
||||
result::Result::{Err, Ok},
|
||||
};
|
||||
let data = parse_str::<RequiredAttribute>("<attr xmlns='urn:example:ns1' foo='bar'/>").unwrap();
|
||||
assert_eq!(data.foo, "bar");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn required_attribute_missing() {
|
||||
#[allow(unused_imports)]
|
||||
use std::{
|
||||
option::Option::{None, Some},
|
||||
result::Result::{Err, Ok},
|
||||
};
|
||||
match parse_str::<RequiredAttribute>("<attr xmlns='urn:example:ns1'/>") {
|
||||
Err(::xso::error::FromElementError::Invalid(::xso::error::Error::Other(e)))
|
||||
if e.contains("Required attribute field") && e.contains("missing") =>
|
||||
{
|
||||
()
|
||||
}
|
||||
other => panic!("unexpected result: {:?}", other),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue