mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 08:12:24 +00:00
xml: Add serialization
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
use rustical_xml::{de::XmlRootParseStr, Unit, XmlDeserialize, XmlRoot};
|
||||
use std::io::BufRead;
|
||||
|
||||
#[test]
|
||||
fn test_struct_tagged_enum() {
|
||||
@@ -15,11 +14,17 @@ fn test_struct_tagged_enum() {
|
||||
prop: Vec<PropEnum>,
|
||||
}
|
||||
|
||||
#[derive(Debug, XmlDeserialize, PartialEq)]
|
||||
struct Displayname {
|
||||
#[xml(ty = "text")]
|
||||
name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, XmlDeserialize, PartialEq)]
|
||||
enum PropEnum {
|
||||
A,
|
||||
B,
|
||||
Displayname(String),
|
||||
Displayname(Displayname),
|
||||
}
|
||||
|
||||
let doc = Propfind::parse_str(
|
||||
@@ -41,7 +46,9 @@ fn test_struct_tagged_enum() {
|
||||
prop: vec![
|
||||
PropEnum::B,
|
||||
PropEnum::A,
|
||||
PropEnum::Displayname("Hello!".to_owned())
|
||||
PropEnum::Displayname(Displayname {
|
||||
name: "Hello!".to_owned()
|
||||
})
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ use rustical_xml::de::XmlRootParseStr;
|
||||
use rustical_xml::XmlRoot;
|
||||
use rustical_xml::{Unit, Unparsed, XmlDeserialize};
|
||||
use std::collections::HashSet;
|
||||
use std::io::BufRead;
|
||||
|
||||
#[test]
|
||||
fn test_struct_text_field() {
|
||||
|
||||
29
crates/xml/tests/se_struct.rs
Normal file
29
crates/xml/tests/se_struct.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use rustical_xml::{XmlRoot, XmlSerialize};
|
||||
use xml_derive::XmlDeserialize;
|
||||
|
||||
#[test]
|
||||
fn test_struct_document() {
|
||||
#[derive(Debug, XmlRoot, XmlSerialize, XmlDeserialize, PartialEq)]
|
||||
#[xml(root = b"document")]
|
||||
struct Document {
|
||||
child: Child,
|
||||
}
|
||||
|
||||
#[derive(Debug, XmlDeserialize, XmlSerialize, PartialEq, Default)]
|
||||
struct Child {
|
||||
#[xml(ty = "text")]
|
||||
text: String,
|
||||
}
|
||||
|
||||
let mut buf = Vec::new();
|
||||
let mut writer = quick_xml::Writer::new(&mut buf);
|
||||
Document {
|
||||
child: Child {
|
||||
text: "asd".to_owned(),
|
||||
},
|
||||
}
|
||||
.serialize(Some(Document::root_tag()), &mut writer)
|
||||
.unwrap();
|
||||
let out = String::from_utf8(buf).unwrap();
|
||||
dbg!(out);
|
||||
}
|
||||
Reference in New Issue
Block a user