xml: untagged enums

This commit is contained in:
Lennart
2024-12-22 18:12:15 +01:00
parent 241b356e44
commit 9813fb5f95
4 changed files with 204 additions and 52 deletions

View File

@@ -1,8 +1,8 @@
use rustical_xml::{de::XmlRootParseStr, XmlDeserialize, XmlRoot};
use rustical_xml::{de::XmlRootParseStr, Unit, XmlDeserialize, XmlRoot};
use std::io::BufRead;
#[test]
fn test_struct_untagged_enum() {
fn test_struct_tagged_enum() {
#[derive(Debug, XmlDeserialize, XmlRoot, PartialEq)]
#[xml(root = b"propfind")]
struct Propfind {
@@ -11,14 +11,15 @@ fn test_struct_untagged_enum() {
#[derive(Debug, XmlDeserialize, PartialEq)]
struct Prop {
#[xml(ty = "untagged")]
prop: PropEnum,
#[xml(ty = "untagged", flatten)]
prop: Vec<PropEnum>,
}
#[derive(Debug, XmlDeserialize, PartialEq)]
enum PropEnum {
A,
B,
Displayname(String),
}
let doc = Propfind::parse_str(
@@ -26,6 +27,8 @@ fn test_struct_untagged_enum() {
<propfind>
<prop>
<b/>
<a/>
<displayname>Hello!</displayname>
</prop>
</propfind>
"#,
@@ -34,7 +37,55 @@ fn test_struct_untagged_enum() {
assert_eq!(
doc,
Propfind {
prop: Prop { prop: PropEnum::B }
prop: Prop {
prop: vec![
PropEnum::B,
PropEnum::A,
PropEnum::Displayname("Hello!".to_owned())
]
}
}
);
}
#[test]
fn test_tagged_enum_complex() {
#[derive(Debug, XmlDeserialize, XmlRoot, PartialEq)]
#[xml(root = b"propfind")]
struct Propfind {
prop: PropStruct,
}
#[derive(Debug, XmlDeserialize, PartialEq)]
struct PropStruct {
#[xml(ty = "untagged", flatten)]
prop: Vec<Prop>,
}
#[derive(Debug, XmlDeserialize, PartialEq)]
enum Prop {
Nice(Nice),
#[xml(other)]
Invalid,
}
#[derive(Debug, XmlDeserialize, PartialEq)]
struct Nice {
nice: Unit,
}
let asd = Propfind::parse_str(
r#"
<propfind>
<prop>
<nice>
<nice />
</nice>
<wtf />
</prop>
</propfind>
"#,
)
.unwrap();
dbg!(asd);
}