cbor 0.3.16

CBOR (binary JSON) with type based decoding and encoding.
Documentation
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use std::borrow::ToOwned;
use std::char;

use rustc_serialize::Decoder as RustcDecoder;

use {Cbor, CborUnsigned, CborBytes, Type, CborResult, CborError, ReadError};

pub struct CborDecoder {
    stack: Vec<Cbor>,
}

impl CborDecoder {
    pub fn new(val: Cbor) -> CborDecoder {
        CborDecoder { stack: vec![val] }
    }

    pub fn pop(&mut self, expected: Type) -> CborResult<Cbor> {
        match self.stack.pop() {
            Some(Cbor::Bytes(v)) => Ok(Cbor::Array(
                v.0.into_iter()
                 .map(|b| Cbor::Unsigned(CborUnsigned::UInt8(b)))
                 .collect::<Vec<_>>()
            )),
            Some(Cbor::Tag(tag)) => {
                self.stack.push(*tag.data);
                Ok(Cbor::Unsigned(CborUnsigned::UInt64(tag.tag)))
            }
            Some(v) => Ok(v),
            None => Err(self.errstr(format!(
                "No data items left (expected a data item with type '{:?}').",
                expected))),
        }
    }

    pub fn pop_expect(&mut self, expected: &str) -> CborResult<Cbor> {
        match self.stack.pop() {
            Some(v) => Ok(v),
            None => Err(self.errstr(format!(
                "No data items left (expected {}).", expected))),
        }
    }

    pub fn err(&self, err: ReadError) -> CborError {
        CborError::Decode(err)
    }

    pub fn errstr(&self, s: String) -> CborError {
        self.err(ReadError::Other(s))
    }
}

macro_rules! read_unsigned {
    ($dec:ident, $cbor_ty:expr, $to:ident) => ({
        let v = try!($dec.pop($cbor_ty));
        match v {
            Cbor::Unsigned(v) => v.$to().map_err(CborError::Decode),
            ref v => return Err($dec.err(ReadError::mismatch($cbor_ty, v))),
        }
    });
}

macro_rules! read_signed {
    ($dec:ident, $ty:ty, $cbor_ty:expr, $to:ident, $tou:ident) => ({
        let v = try!($dec.pop($cbor_ty));
        match v {
            Cbor::Signed(v) => v.$to().map_err(CborError::Decode),
            Cbor::Unsigned(v) =>
                v.$tou().map(|n| n as $ty).map_err(CborError::Decode),
            ref v => return Err($dec.err(ReadError::mismatch($cbor_ty, v))),
        }
    });
}

macro_rules! read_float {
    ($dec:ident, $ty:ty, $cbor_ty:expr,
     $to:ident, $toi:ident, $tou:ident) => ({
        let v = try!($dec.pop($cbor_ty));
        match v {
            Cbor::Float(v) => v.$to().map_err(CborError::Decode),
            Cbor::Signed(v) =>
                v.$toi().map(|n| n as $ty).map_err(CborError::Decode),
            Cbor::Unsigned(v) =>
                v.$tou().map(|n| n as $ty).map_err(CborError::Decode),
            ref v => return Err($dec.err(ReadError::mismatch($cbor_ty, v))),
        }
    });
}

impl RustcDecoder for CborDecoder {
    type Error = CborError;

    fn error(&mut self, err: &str) -> CborError {
        self.err(ReadError::Other(err.to_owned()))
    }

    fn read_nil(&mut self) -> CborResult<()> {
        match try!(self.pop(Type::Null)) {
            Cbor::Null => Ok(()),
            v => Err(self.err(ReadError::mismatch(Type::Null, &v))),
        }
    }

    fn read_usize(&mut self) -> CborResult<usize> {
        read_unsigned!(self, Type::UInt, to_usize)
    }

    fn read_u64(&mut self) -> CborResult<u64> {
        read_unsigned!(self, Type::UInt64, to_u64)
    }

    fn read_u32(&mut self) -> CborResult<u32> {
        read_unsigned!(self, Type::UInt32, to_u32)
    }

    fn read_u16(&mut self) -> CborResult<u16> {
        read_unsigned!(self, Type::UInt16, to_u16)
    }

    fn read_u8(&mut self) -> CborResult<u8> {
        read_unsigned!(self, Type::UInt8, to_u8)
    }

    fn read_isize(&mut self) -> CborResult<isize> {
        read_signed!(self, isize, Type::Int, to_isize, to_usize)
    }

    fn read_i64(&mut self) -> CborResult<i64> {
        read_signed!(self, i64, Type::Int64, to_i64, to_u64)
    }

    fn read_i32(&mut self) -> CborResult<i32> {
        read_signed!(self, i32, Type::Int32, to_i32, to_u32)
    }

    fn read_i16(&mut self) -> CborResult<i16> {
        read_signed!(self, i16, Type::Int16, to_i16, to_u16)
    }

    fn read_i8(&mut self) -> CborResult<i8> {
        read_signed!(self, i8, Type::Int8, to_i8, to_u8)
    }

    fn read_bool(&mut self) -> CborResult<bool> {
        match try!(self.pop(Type::Bool)) {
            Cbor::Bool(s) => Ok(s),
            v => Err(self.err(ReadError::mismatch(Type::Bool, &v))),
        }
    }

    fn read_f64(&mut self) -> CborResult<f64> {
        read_float!(self, f64, Type::Float64, to_f64, to_i64, to_u64)
    }

    fn read_f32(&mut self) -> CborResult<f32> {
        read_float!(self, f32, Type::Float32, to_f32, to_i32, to_u32)
    }

    fn read_char(&mut self) -> CborResult<char> {
        let n = try!(self.read_u32());
        match char::from_u32(n) {
            Some(c) => Ok(c),
            None => Err(self.errstr(format!(
                "Could not convert '{:?}' to Unicode scalar value.", n))),
        }
    }

    fn read_str(&mut self) -> CborResult<String> {
        match self.stack.pop() {
            Some(Cbor::Unicode(s)) => Ok(s),
            Some(Cbor::Bytes(CborBytes(bytes))) => {
                String::from_utf8(bytes).map_err(|err| {
                    self.err(ReadError::Other(err.to_string()))
                })
            }
            Some(v) => Err(self.err(ReadError::mismatch(Type::Unicode, &v))),
            None => Err(self.errstr(format!(
                "No data items left (expected a data item with type '{:?}').",
                Type::Unicode))),
        }
    }

    fn read_enum<T, F>(&mut self, _name: &str, f: F) -> CborResult<T>
            where F: FnOnce(&mut CborDecoder) -> CborResult<T> {
        f(self)
    }

    fn read_enum_variant<T, F>(
        &mut self,
        names: &[&str],
        mut f: F,
    ) -> CborResult<T>
    where F: FnMut(&mut CborDecoder, usize) -> CborResult<T> {
        let name = match try!(self.pop_expect("Unicode or variant map")) {
            Cbor::Unicode(name) => name,
            Cbor::Map(mut map) => {
                let name = match map.remove("variant") {
                    Some(Cbor::Unicode(name)) => name,
                    Some(v) => return Err(self.errstr(format!(
                        "Expected 'variant' key in variant map to map to a \
                         Unicode string, but got {:?}", v.typ()))),
                    None => return Err(self.errstr(format!(
                        "Missing 'variant' key in variant map"))),
                };
                match map.remove("fields") {
                    Some(Cbor::Array(fields)) => {
                        self.stack.extend(fields.into_iter().rev());
                    },
                    Some(v) => return Err(self.errstr(format!(
                        "Expected 'fields' key in variant map to map to an \
                         Array, but got {:?}", v.typ()))),
                    None => return Err(self.errstr(format!(
                        "Missing 'fields' key in variant map."))),
                }
                name
            }
            v => return Err(self.errstr(format!(
                "Expected Unicode string or variant map, but got {:?}",
                v.typ()))),
        };
        let idx = match names.iter().position(|&n| n == name) {
            Some(idx) => idx,
            None => return Err(self.errstr(format!(
                "Unknown variant name '{}'.", name))),
        };
        f(self, idx)
    }

    fn read_enum_variant_arg<T, F>(
        &mut self,
        _a_idx: usize,
        f: F,
    ) -> CborResult<T>
    where F: FnOnce(&mut CborDecoder) -> CborResult<T> {
        f(self)
    }

    fn read_enum_struct_variant<T, F>(
        &mut self,
        names: &[&str],
        f: F,
    ) -> CborResult<T>
    where F: FnMut(&mut CborDecoder, usize) -> CborResult<T> {
        self.read_enum_variant(names, f)
    }

    fn read_enum_struct_variant_field<T, F>(
        &mut self,
        _f_name: &str,
        f_idx: usize,
        f: F,
    ) -> CborResult<T>
    where F: FnOnce(&mut CborDecoder) -> CborResult<T> {
        self.read_enum_variant_arg(f_idx, f)
    }

    fn read_struct<T, F>(
        &mut self,
        _s_name: &str,
        _len: usize,
        f: F,
    ) -> CborResult<T>
    where F: FnOnce(&mut CborDecoder) -> CborResult<T> {
        let val = try!(f(self));
        // When we read a struct field, we pop the CBOR map off the stack,
        // find and remove the field name and its associated value, and then
        // push the map back on the stack. Therefore, when we're done
        // processing all the struct fields, we'll have an extraneous map
        // left on the stack. So pop it off. If this assert fails, we have a
        // bug in the decoder. ---AG
        assert_eq!(self.stack.pop().unwrap().typ(), Type::Map);
        // Do we want to check if the map popped off here is empty? If it's
        // not, that means the data contains more than what the struct
        // specifies. We should probably be relaxed and let it pass. ---AG
        Ok(val)
    }

    fn read_struct_field<T, F>(
        &mut self,
        f_name: &str,
        _f_idx: usize,
        f: F,
    ) -> CborResult<T>
    where F: FnOnce(&mut CborDecoder) -> CborResult<T> {
        let mut map = match try!(self.pop(Type::Map)) {
            Cbor::Map(map) => map,
            v => {
                return Err(self.err(ReadError::mismatch(Type::Map, &v)));
            }
        };
        let val = match map.remove(f_name) {
            Some(val) => { self.stack.push(val); try!(f(self)) }
            None => {
                self.stack.push(Cbor::Null);
                match f(self) {
                    Ok(val) => val,
                    Err(_) => return Err(self.errstr(format!(
                        "Missing field '{}' in map object.", f_name))),
                }
            }
        };
        self.stack.push(Cbor::Map(map));
        Ok(val)
    }

    fn read_tuple<T, F>(
        &mut self,
        len: usize,
        f: F,
    ) -> CborResult<T>
    where F: FnOnce(&mut CborDecoder) -> CborResult<T> {
        let array = match try!(self.pop(Type::Array)) {
            Cbor::Array(v) => v,
            v => return Err(self.err(ReadError::mismatch(Type::Array, &v))),
        };
        let got_len = array.len();
        if len != got_len {
            return Err(self.errstr(format!(
                "Expected tuple of length {:?}, but got array of length {:?}",
                len, got_len)));
        }
        self.stack.extend(array.into_iter().rev());
        f(self)
    }

    fn read_tuple_arg<T, F>(
        &mut self,
        _a_idx: usize,
        f: F,
    ) -> CborResult<T>
    where F: FnOnce(&mut CborDecoder) -> CborResult<T> {
        f(self)
    }

    fn read_tuple_struct<T, F>(
        &mut self,
        _s_name: &str,
        len: usize,
        f: F,
    ) -> CborResult<T>
    where F: FnOnce(&mut CborDecoder) -> CborResult<T> {
        let array = match try!(self.pop(Type::Array)) {
            Cbor::Array(v) => v,
            v => return Err(self.err(ReadError::mismatch(Type::Array, &v))),
        };
        let got_len = array.len();
        if len != got_len {
            return Err(self.errstr(format!(
                "Expected tuple of length {:?}, but got array of length {:?}",
                len, got_len)));
        }
        self.stack.extend(array.into_iter().rev());
        f(self)
    }

    fn read_tuple_struct_arg<T, F>(
        &mut self,
        _a_idx: usize,
        f: F,
    ) -> CborResult<T>
    where F: FnOnce(&mut CborDecoder) -> CborResult<T> {
        f(self)
    }

    fn read_option<T, F>(&mut self, mut f: F) -> CborResult<T>
            where F: FnMut(&mut CborDecoder, bool) -> CborResult<T> {
        match try!(self.pop(Type::Any)) {
            Cbor::Null => f(self, false),
            v => { self.stack.push(v); f(self, true) }
        }
    }

    fn read_seq<T, F>(&mut self, f: F) -> CborResult<T>
            where F: FnOnce(&mut CborDecoder, usize) -> CborResult<T> {
        let array = match try!(self.pop(Type::Array)) {
            Cbor::Array(v) => v,
            v => return Err(self.err(ReadError::mismatch(Type::Array, &v))),
        };
        let len = array.len();
        self.stack.extend(array.into_iter().rev());
        f(self, len)
    }

    fn read_seq_elt<T, F>(&mut self, _idx: usize, f: F) -> CborResult<T>
            where F: FnOnce(&mut CborDecoder) -> CborResult<T> {
        f(self)
    }

    fn read_map<T, F>(&mut self, f: F) -> CborResult<T>
            where F: FnOnce(&mut CborDecoder, usize) -> CborResult<T> {
        let map = match try!(self.pop(Type::Map)) {
            Cbor::Map(v) => v,
            v => return Err(self.err(ReadError::mismatch(Type::Map, &v))),
        };
        let len = map.len();
        for (k, v) in map { // order doesn't matter for HashMap
            self.stack.push(v);
            self.stack.push(Cbor::Unicode(k));
        }
        f(self, len)
    }

    fn read_map_elt_key<T, F>(&mut self, _idx: usize, f: F) -> CborResult<T>
            where F: FnOnce(&mut CborDecoder) -> CborResult<T> {
        f(self)
    }

    fn read_map_elt_val<T, F>(&mut self, _idx: usize, f: F) -> CborResult<T>
            where F: FnOnce(&mut CborDecoder) -> CborResult<T> {
        f(self)
    }
}