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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use std::io;
use std::time;
use amf3;
use Pair;
use DecodeResult;
pub use self::decode::Decoder;
pub use self::encode::Encoder;
mod decode;
mod encode;
mod marker {
pub const NUMBER: u8 = 0x00;
pub const BOOLEAN: u8 = 0x01;
pub const STRING: u8 = 0x02;
pub const OBJECT: u8 = 0x03;
pub const MOVIECLIP: u8 = 0x04;
pub const NULL: u8 = 0x05;
pub const UNDEFINED: u8 = 0x06;
pub const REFERENCE: u8 = 0x07;
pub const ECMA_ARRAY: u8 = 0x08;
pub const OBJECT_END_MARKER: u8 = 0x09;
pub const STRICT_ARRAY: u8 = 0x0A;
pub const DATE: u8 = 0x0B;
pub const LONG_STRING: u8 = 0x0C;
pub const UNSUPPORTED: u8 = 0x0D;
pub const RECORDSET: u8 = 0x0E;
pub const XML_DOCUMENT: u8 = 0x0F;
pub const TYPED_OBJECT: u8 = 0x10;
pub const AVMPLUS_OBJECT: u8 = 0x11;
}
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum Value {
Number(f64),
Boolean(bool),
String(String),
Object {
class_name: Option<String>,
entries: Vec<Pair<String, Value>>,
},
Null,
Undefined,
EcmaArray {
entries: Vec<Pair<String, Value>>,
},
Array {
entries: Vec<Value>,
},
Date {
unix_time: time::Duration,
},
XmlDocument(String),
AvmPlus(amf3::Value),
}
impl Value {
pub fn read_from<R>(reader: R) -> DecodeResult<Self>
where R: io::Read
{
Decoder::new(reader).decode()
}
pub fn write_to<W>(&self, writer: W) -> io::Result<()>
where W: io::Write
{
Encoder::new(writer).encode(self)
}
pub fn try_as_str(&self) -> Option<&str> {
match *self {
Value::String(ref x) => Some(x.as_ref()),
Value::XmlDocument(ref x) => Some(x.as_ref()),
Value::AvmPlus(ref x) => x.try_as_str(),
_ => None,
}
}
pub fn try_as_f64(&self) -> Option<f64> {
match *self {
Value::Number(x) => Some(x),
Value::AvmPlus(ref x) => x.try_as_f64(),
_ => None,
}
}
pub fn try_into_values(self) -> Result<Box<Iterator<Item = super::Value>>, Self> {
match self {
Value::Array { entries } => Ok(Box::new(entries.into_iter().map(super::Value::Amf0))),
Value::AvmPlus(x) => {
x.try_into_values()
.map(|iter| iter.map(super::Value::Amf3))
.map(super::iter_boxed)
.map_err(Value::AvmPlus)
}
_ => Err(self),
}
}
pub fn try_into_pairs(self) -> Result<Box<Iterator<Item = (String, super::Value)>>, Self> {
match self {
Value::EcmaArray { entries } => {
Ok(Box::new(entries.into_iter().map(|p| (p.key, super::Value::Amf0(p.value)))))
}
Value::Object { entries, .. } => {
Ok(Box::new(entries.into_iter().map(|p| (p.key, super::Value::Amf0(p.value)))))
}
Value::AvmPlus(x) => {
x.try_into_pairs()
.map(|ps| ps.map(|(k, v)| (k, super::Value::Amf3(v))))
.map(super::iter_boxed)
.map_err(Value::AvmPlus)
}
_ => Err(self),
}
}
}
pub fn string<T>(t: T) -> Value
where String: From<T>
{
Value::String(From::from(t))
}
pub fn number<T>(t: T) -> Value
where f64: From<T>
{
Value::Number(From::from(t))
}
pub fn object<I, K>(entries: I) -> Value
where I: Iterator<Item = (K, Value)>,
String: From<K>
{
Value::Object {
class_name: None,
entries: entries.map(|(k, v)| {
Pair {
key: From::from(k),
value: v,
}
})
.collect(),
}
}
pub fn array(entries: Vec<Value>) -> Value {
Value::Array { entries: entries }
}