Debian Codegraph @

third_party/rust/moz_cbor/src/lib.rs

raw
1 pub mod decoder;
2 pub mod serializer;
3 #[cfg(test)]
4 mod test_decoder;
5 #[cfg(test)]
6 mod test_serializer;
7
8 use std::cmp::Ordering;
9 use std::collections::BTreeMap;
10
11 #[derive(Debug, Clone, PartialEq, PartialOrd, Eq)]
12 pub enum CborType {
13 Integer(u64),
14 SignedInteger(i64),
15 Tag(u64, Box<CborType>),
16 Bytes(Vec<u8>),
17 String(String),
18 Array(Vec<CborType>),
19 Map(BTreeMap<CborType, CborType>),
20 Null,
21 }
22
23 #[derive(Debug, PartialEq)]
24 pub enum CborError {
25 DuplicateMapKey,
26 InputTooLarge,
27 InputValueOutOfRange,
28 LibraryError,
29 MalformedInput,
30 TruncatedInput,
31 UnsupportedType,
32 }
33
34 impl Ord for CborType {
35 /// Sorting for maps: RFC 7049 Section 3.9
36 ///
37 /// The keys in every map must be sorted lowest value to highest.
38 /// * If two keys have different lengths, the shorter one sorts
39 /// earlier;
40 ///
41 /// * If two keys have the same length, the one with the lower value
42 /// in (byte-wise) lexical order sorts earlier.
43 fn cmp(&self, other: &Self) -> Ordering {
44 let self_bytes = self.serialize();
45 let other_bytes = other.serialize();
46 if self_bytes.len() == other_bytes.len() {
47 return self_bytes.cmp(&other_bytes);
48 }
49 self_bytes.len().cmp(&other_bytes.len())
50 }
51 }