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
//! # CBOR event library
//!
//! [`Deserializer`]: ./de/struct.Deserializer.html
//! [`Deserialize`]: ./de/trait.Deserialize.html
//! [`Serializer`]: ./se/struct.Serializer.html
//! [`Serialize`]: ./se/trait.Serialize.html
//! [`std::io::Write`]: https://doc.rust-lang.org/std/io/trait.Write.html
//! [`Error`]: ./enum.Error.html
//! [`Type`]: ./enum.Type.html
//!
//! `cbor_event` is a minimalist CBOR implementation of the CBOR binary
//! serialisation format. It provides a simple yet efficient way to parse
//! CBOR without the need for an intermediate type representation.
//!
//! Here is the list of supported CBOR primary [`Type`]:
//!
//! - Unsigned and Negative Integers;
//! - Bytes and UTF8 String (**finite length only**);
//! - Array and Map (of finite and indefinite size);
//! - Tag;
//! - Specials (`bool`, `null`... **except floating points**).
//!
//! ## Raw deserialisation: [`Deserializer`]
//!
//! Deserialisation works by consuming a `Deserializer` content. To avoid
//! performance issues some objects use a reference to the original
//! source [`Deserializer`] internal buffer. They are then linked to the object
//! by an associated lifetime, this is true for `Bytes`.
//!
//! ```
//! use cbor_event::de::*;
//! use std::io::Cursor;
//!
//! let vec = vec![0x43, 0x01, 0x02, 0x03];
//! let mut raw = Deserializer::from(Cursor::new(vec));
//! let bytes = raw.bytes().unwrap();
//!
//! # assert_eq!(bytes.as_slice(), [1,2,3].as_ref());
//! ```
//!
//! For convenience, we provide the trait [`Deserialize`] to help writing
//! simpler deserializers for your types.
//!
//! ## Serialisation: [`Serializer`]
//!
//! To serialise your objects into CBOR we provide a simple object
//! [`Serializer`]. It is configurable with any [`std::io::Write`]
//! objects. [`Serializer`] is meant to be simple to use and to have
//! limited overhead.
//!
//! ```
//! use cbor_event::se::{Serializer};
//!
//! let mut serializer = Serializer::new_vec();
//! serializer.write_negative_integer(-12)
//!     .expect("write a negative integer");
//!
//! # let bytes = serializer.finalize();
//! # assert_eq!(bytes, [0x2b].as_ref());
//! ```

#[cfg(test)]
#[macro_use]
extern crate quickcheck;

pub mod de;
mod error;
mod len;
mod macros;
mod result;
pub mod se;
mod types;
mod value;

pub use de::Deserialize;
pub use error::Error;
pub use len::*;
pub use result::Result;
pub use se::Serialize;
pub use types::*;
pub use value::{ObjectKey, Value};

const MAX_INLINE_ENCODING: u64 = 23;

const CBOR_PAYLOAD_LENGTH_U8: u8 = 24;
const CBOR_PAYLOAD_LENGTH_U16: u8 = 25;
const CBOR_PAYLOAD_LENGTH_U32: u8 = 26;
const CBOR_PAYLOAD_LENGTH_U64: u8 = 27;

/// exported as a convenient function to test the implementation of
/// [`Serialize`](./se/trait.Serialize.html) and
/// [`Deserialize`](./de/trait.Deserialize.html).
///
pub fn test_encode_decode<V: Sized + PartialEq + Serialize + Deserialize>(v: &V) -> Result<bool> {
    let mut se = se::Serializer::new_vec();
    v.serialize(&mut se)?;
    let bytes = se.finalize();

    let mut raw = de::Deserializer::from(std::io::Cursor::new(bytes));
    let v_ = Deserialize::deserialize(&mut raw)?;

    Ok(v == &v_)
}