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

@ -24,7 +24,7 @@ pub struct Option_ {
pub label: Option<String>,
/// The value returned to the server when selecting this option.
#[xml(extract(namespace = ns::DATA_FORMS, name = "value", fields(text)))]
#[xml(extract(fields(text)))]
pub value: String,
}

View file

@ -38,11 +38,11 @@ generate_id!(
#[xml(namespace = ns::MIX_CORE, name = "participant")]
pub struct Participant {
/// The nick of this participant.
#[xml(extract(namespace = ns::MIX_CORE, name = "nick", fields(text)))]
#[xml(extract(fields(text)))]
pub nick: String,
/// The bare JID of this participant.
#[xml(extract(namespace = ns::MIX_CORE, name = "jid", fields(text)))]
#[xml(extract(fields(text)))]
pub jid: BareJid,
}
@ -85,7 +85,7 @@ pub struct Join {
pub id: Option<ParticipantId>,
/// The nick requested by the user or set by the service.
#[xml(extract(namespace = ns::MIX_CORE, name = "nick", fields(text)))]
#[xml(extract(fields(text)))]
pub nick: String,
/// Which MIX nodes to subscribe to.
@ -164,7 +164,7 @@ impl IqResultPayload for Leave {}
#[xml(namespace = ns::MIX_CORE, name = "setnick")]
pub struct SetNick {
/// The new requested nick.
#[xml(extract(namespace = ns::MIX_CORE, name = "nick", fields(text)))]
#[xml(extract(fields(text)))]
pub nick: String,
}
@ -184,11 +184,11 @@ impl SetNick {
#[xml(namespace = ns::MIX_CORE, name = "mix")]
pub struct Mix {
/// The nick of the user who said something.
#[xml(extract(namespace = ns::MIX_CORE, name = "nick", fields(text)))]
#[xml(extract(fields(text)))]
pub nick: String,
/// The JID of the user who said something.
#[xml(extract(namespace = ns::MIX_CORE, name = "jid", fields(text)))]
#[xml(extract(fields(text)))]
pub jid: BareJid,
}

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>",
)
}