mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 22:52:22 +00:00
xml: untagged enums
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user