-
Notifications
You must be signed in to change notification settings - Fork 0
/
exp-tmp.rs
95 lines (87 loc) · 2.59 KB
/
exp-tmp.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use yaserde::{YaDeserialize, YaSerialize};
fn main() {
let foo: Foo = yaserde::de::from_str(XML).unwrap();
println!("foo: {foo:?}");
let cfg = yaserde::ser::Config{
perform_indent: true,
write_document_declaration: true,
indent_string: Some(" ".to_string()),
};
let s = yaserde::ser::to_string_with_config(&foo, &cfg).unwrap();
println!("s: {s}");
}
#[derive(YaDeserialize, YaSerialize, Debug)]
// #[derive(YaDeserialize, Debug)]
struct Foo {
#[yaserde(child)]
pub bar: i64,
#[yaserde(child)]
pub baz: raw::OpaqueXml,
#[yaserde(child)]
pub bah: String,
}
const XML: &str = r#"
<Foo>
<bar>123</bar>
<baz x="7"><bleh>flab</bleh><zig zag="8"/><fee><foo/><fum/></fee></baz>
<bah>hello!</bah>
</Foo>
"#;
mod raw {
use xml::reader::XmlEvent;
use yaserde::{YaDeserialize, YaSerialize};
#[derive(Debug)]
pub struct OpaqueXml {
events: Vec<XmlEvent>,
}
impl YaDeserialize for OpaqueXml {
fn deserialize<R>(reader: &mut yaserde::de::Deserializer<R>) -> Result<Self, String>
where
R: std::io::Read,
{
use xml::reader::XmlEvent::*;
let mut events = vec![];
let start_depth = reader.depth();
loop {
let depth = reader.depth();
let peek = reader.peek()?;
match peek {
EndElement{..} if depth == start_depth+1 => {
events.push(peek.clone());
break;
},
_ => {},
}
events.push(reader.next_event()?);
}
Ok(Self{events})
}
}
impl YaSerialize for OpaqueXml {
fn serialize<W>(&self, writer: &mut yaserde::ser::Serializer<W>) -> Result<(), String>
where
W: std::io::Write,
{
for ev in &self.events {
writer.write(ev.as_writer_event().unwrap()).unwrap();
// if let Some(ev) = ev.as_writer_event() {
// }
}
Ok(())
}
fn serialize_attributes(
&self,
source_attributes: Vec<xml::attribute::OwnedAttribute>,
source_namespace: xml::namespace::Namespace,
) -> Result<
(
Vec<xml::attribute::OwnedAttribute>,
xml::namespace::Namespace,
),
String,
> {
Err("OpaqueXml cannot be serialized as attribute".to_string())
// Ok((source_attributes, source_namespace))
}
}
}