xso: allow omission of namespace and name on extracts

This is a quality-of-life improvement, as it may save lots of typing in
the common case (see the diff in parsers).
This commit is contained in:
Jonas Schäfer 2024-08-03 14:06:41 +02:00
commit df63c2a78f
9 changed files with 129 additions and 54 deletions

View file

@ -1124,3 +1124,60 @@ fn nested_extract_roundtrip() {
};
roundtrip_full::<NestedExtract>("<parent xmlns='urn:example:ns1'><child><grandchild>hello world</grandchild></child></parent>")
}
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = NS1, name = "parent")]
struct ExtractOmitNamespace {
#[xml(extract(name = "child", fields(text)))]
contents: String,
}
#[test]
fn extract_omit_namespace_roundtrip() {
#[allow(unused_imports)]
use std::{
option::Option::{None, Some},
result::Result::{Err, Ok},
};
roundtrip_full::<ExtractOmitNamespace>(
"<parent xmlns='urn:example:ns1'><child>hello world!</child></parent>",
)
}
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = NS1, name = "parent")]
struct ExtractOmitName {
#[xml(extract(namespace = NS1, fields(text)))]
contents: String,
}
#[test]
fn extract_omit_name_roundtrip() {
#[allow(unused_imports)]
use std::{
option::Option::{None, Some},
result::Result::{Err, Ok},
};
roundtrip_full::<ExtractOmitName>(
"<parent xmlns='urn:example:ns1'><contents>hello world!</contents></parent>",
)
}
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = NS1, name = "parent")]
struct ExtractOmitNameAndNamespace {
#[xml(extract(fields(text)))]
contents: String,
}
#[test]
fn extract_omit_name_and_namespace_roundtrip() {
#[allow(unused_imports)]
use std::{
option::Option::{None, Some},
result::Result::{Err, Ok},
};
roundtrip_full::<ExtractOmitNameAndNamespace>(
"<parent xmlns='urn:example:ns1'><contents>hello world!</contents></parent>",
)
}