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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use {TokenWriterError, TokenWriterWithTree};
use binjs_shared::{FieldName, InterfaceName, SharedString};
use std::io::Write;
use std::rc::Rc;
use clap;
use xml_rs;
#[derive(Debug)]
pub enum SubTree {
String(Option<SharedString>),
Bool(Option<bool>),
Float(Option<f64>),
U32(u32),
List(Vec<Rc<SubTree>>),
Node {
name: SharedString,
children: Vec<(SharedString, Rc<SubTree>)>,
},
}
impl SubTree {
fn write<W: Write>(
&self,
out: &mut xml_rs::writer::EventWriter<W>,
) -> xml_rs::writer::Result<()> {
use self::SubTree::*;
use xml_rs::writer::*;
match *self {
String(Some(ref s)) => {
out.write(XmlEvent::characters(s.as_str()))?;
}
String(None) => {}
Bool(Some(true)) => {
out.write(XmlEvent::characters("true"))?;
}
Bool(Some(false)) => {
out.write(XmlEvent::characters("false"))?;
}
Bool(None) => {}
Float(Some(ref x)) => {
out.write(XmlEvent::characters(&format!("{}", x)))?;
}
Float(None) => {}
U32(ref value) => {
out.write(XmlEvent::characters(&format!("{}", value)))?;
}
List(ref children) => {
for c in children {
out.write(XmlEvent::start_element("_item"))?;
c.write(out)?;
out.write(XmlEvent::end_element())?;
}
}
Node {
ref name,
ref children,
} => {
if name.len() == 0 {
assert_eq!(children.len(), 0);
} else {
out.write(XmlEvent::start_element(name.as_str()))?;
for (ref name, ref c) in children {
out.write(XmlEvent::start_element(name.as_str()))?;
c.write(out)?;
out.write(XmlEvent::end_element())?;
}
out.write(XmlEvent::end_element())?;
}
}
}
Ok(())
}
}
pub struct Encoder {
root: Rc<SubTree>,
}
impl Encoder {
pub fn new() -> Self {
Self {
root: Rc::new(SubTree::Bool(None)),
}
}
fn register(&mut self, tree: SubTree) -> Result<Rc<SubTree>, TokenWriterError> {
let result = Rc::new(tree);
self.root = result.clone();
Ok(result)
}
}
impl TokenWriterWithTree for Encoder {
type Tree = Rc<SubTree>;
type Data = Box<[u8]>;
fn bool(&mut self, data: Option<bool>) -> Result<Self::Tree, TokenWriterError> {
self.register(SubTree::Bool(data))
}
fn unsigned_long(&mut self, data: u32) -> Result<Self::Tree, TokenWriterError> {
self.register(SubTree::U32(data))
}
fn float(&mut self, data: Option<f64>) -> Result<Self::Tree, TokenWriterError> {
self.register(SubTree::Float(data))
}
fn string(&mut self, data: Option<&SharedString>) -> Result<Self::Tree, TokenWriterError> {
self.register(SubTree::String(data.map(Clone::clone)))
}
fn tagged_tuple(
&mut self,
tag: &InterfaceName,
items: &[(&FieldName, Self::Tree)],
) -> Result<Self::Tree, TokenWriterError> {
self.register(SubTree::Node {
name: tag.as_shared_string().clone(),
children: items
.iter()
.map(|(attribute, tree)| (attribute.as_shared_string().clone(), tree.clone()))
.collect(),
})
}
fn list(&mut self, items: Vec<Self::Tree>) -> Result<Self::Tree, TokenWriterError> {
self.register(SubTree::List(items))
}
fn offset(&mut self) -> Result<Self::Tree, TokenWriterError> {
unimplemented!()
}
fn done(self) -> Result<Self::Data, TokenWriterError> {
use xml_rs::writer::*;
let mut buf = vec![];
{
let mut writer = EmitterConfig::new()
.perform_indent(true)
.create_writer(&mut buf);
self.root.write(&mut writer).unwrap();
}
Ok(buf.into())
}
}
pub struct FormatProvider;
impl ::FormatProvider for FormatProvider {
fn subcommand<'a, 'b>(&self) -> clap::App<'a, 'b> {
use clap::*;
SubCommand::with_name("xml")
.about("(EXPERIMENTAL) Encode to xml. This format is designed to help gather statistics, and is not considered useful for any other reason.")
}
fn handle_subcommand(
&self,
_spec: &binjs_meta::spec::Spec,
_matches: Option<&clap::ArgMatches>,
) -> Result<::Format, ::std::io::Error> {
Ok(::Format::XML)
}
}