ser_write_msgpack/
lib.rs

1//! A MessagePack serde serializer for [`ser-write`](`ser_write`) and a deserializer for convenience.
2/*!
3
4[`Serializer`] types:
5
6| Serde type ->     | MessagePack type
7|-------------------|--------------------
8| `()`              | `nil`
9| `Unit` struct     | `nil`
10| `bool`            | `bool`
11| `NewType(T)`      | `T` -> `MessagePack`
12| `None`            | `nil`
13| `Some(T)`         | `T` -> `MessagePack`
14| `u8`-`u64`        | `uint` (smallest representation)
15| `i8`-`i64`        | `int`, `uint` (sm. repr.)
16| `f23`             | `float-32`
17| `f64`             | `float-64`
18| `str`             | `str`
19| `bytes`           | `bin`
20| `array`, `tuple`  | `array` (sm. repr.)
21| `seq`-like        | `array` (sm. repr.)
22| `map`-like        | `map` (sm. repr.)
23| `struct`          | `map` or `array` (depending on implementation)
24| `unit variant`    | `str` or `uint` (depending on implementation)
25| `newtype variant` | `fixmap:1` `variant`, `T` (`variant`: `str` or `uint` impl. dep.)
26| `tuple variant`   | `fixmap:1` `variant`, `array` (impl. dep.)
27| `struct variant`  | `fixmap:1` `variant`, `struct` (impl. dep.)
28
29Currently neither [`Serializer`] nor [`Deserializer`] supports MessagePack extension types.
30The `ext` and `fixext` types are properly recognized and skipped over when a value ignored.
31
32* [`Deserializer`] supports self-describing formats ([`deserialize_any`]).
33* [`Deserializer`] deserializes structs from MessagePack maps or arrays using both
34  `uint` or `str` MessagePack types as field identifiers.
35
36[`Deserializer`] types:
37
38| MessagePack type -> | Serde type (depending on context)
39|---------------------|----------------------------------------
40| `nil`               | `unit`,`none`,`NaN`
41| `bool`              | `bool`
42| `fixint`, `int`     | `f64`,`f32`,`u8`-`u64`,`i8`-`i64`
43| `float-32`          | `f64` or `f32`
44| `float-64`          | `f64` or `f32`
45| `str`               | `str`, `enum variant`, `field name`
46| `bin`               | `bytes` (`&[u8]`, `Vec<u8>` with `std` or `alloc`)
47| `array`             | `array`,`tuple`,`tuple struct`,`typle variant`,`seq-like`,`struct`
48| `map`               | `enum variant`,`struct variant`,`map-like`,`struct`
49| `T`                 | `NewType(T)`, `Some(T)`
50| `fixext`, `ext`     | Unsupported
51
52[`Serializer`]: ser::CompactSerializer
53[`Deserializer`]: de::Deserializer
54[`deserialize_any`]: serde::de::Deserializer::deserialize_any
55*/
56#![no_std]
57#![cfg_attr(not(test), forbid(unsafe_code))]
58#![cfg_attr(docsrs, feature(doc_cfg))]
59
60#[cfg(feature = "std")]
61extern crate std;
62
63#[cfg(all(feature = "alloc",not(feature = "std")))]
64extern crate alloc;
65
66pub mod ser;
67pub mod de;
68
69pub use ser_write;
70pub use ser_write::SerWrite;
71
72#[cfg(any(feature = "std", feature = "alloc"))]
73pub use ser::{
74    to_vec_compact,
75    to_vec,
76    to_vec_named
77};
78
79pub use ser::{
80    to_writer_compact,
81    to_writer,
82    to_writer_named
83};
84
85pub use de::{
86    from_slice,
87    from_slice_split_tail
88};
89
90mod magick {
91    use core::ops::RangeInclusive;
92    /* MessagePack MAGICK */
93    pub const MIN_POSFIXINT: u8 = 0x00;
94    pub const MAX_POSFIXINT: u8 = 0x7f;
95    pub const NEGFIXINT: u8 = 0b11100000;
96    pub const MIN_NEGFIXINT: i8 = NEGFIXINT as i8; //-32
97    pub const FIXINT_I16: RangeInclusive<i16> = MIN_NEGFIXINT as i16..=MAX_POSFIXINT as i16;
98    pub const FIXINT_I32: RangeInclusive<i32> = MIN_NEGFIXINT as i32..=MAX_POSFIXINT as i32;
99    pub const FIXINT_I64: RangeInclusive<i64> = MIN_NEGFIXINT as i64..=MAX_POSFIXINT as i64;
100    pub const NIL: u8      = 0xc0;
101    pub const RESERVED: u8 = 0xc1;
102    pub const FALSE: u8    = 0xc2;
103    pub const TRUE: u8     = 0xc3;
104
105    pub const FIXMAP: u8   = 0x80; /* 1000xxxx */
106    pub const MAX_FIXMAP_SIZE: usize = 0b1111;
107    pub const FIXMAP_MAX: u8 = FIXMAP + MAX_FIXMAP_SIZE as u8; /* 10001111 */
108
109    pub const FIXARRAY: u8 = 0x90; /* 1001xxxx */
110    pub const MAX_FIXARRAY_SIZE: usize = 0b1111;
111    pub const FIXARRAY_MAX: u8 = FIXARRAY + MAX_FIXARRAY_SIZE as u8; /* 10011111 */
112
113    pub const FIXSTR: u8   = 0xa0; /* 101xxxxx */
114    pub const MAX_FIXSTR_SIZE: usize = 0b11111;
115    pub const FIXSTR_MAX: u8 = FIXSTR + MAX_FIXSTR_SIZE as u8; /* 10111111 */
116
117    pub const BIN_8: u8     = 0xc4;
118    pub const BIN_16: u8    = 0xc5;
119    pub const BIN_32: u8    = 0xc6;
120
121    pub const EXT_8: u8     = 0xc7;
122    pub const EXT_16: u8    = 0xc8;
123    pub const EXT_32: u8    = 0xc9;
124
125    pub const FLOAT_32: u8  = 0xca;
126    pub const FLOAT_64: u8  = 0xcb;
127
128    pub const UINT_8: u8    = 0xcc;
129    pub const UINT_16: u8   = 0xcd;
130    pub const UINT_32: u8   = 0xce;
131    pub const UINT_64: u8   = 0xcf;
132
133    pub const INT_8: u8     = 0xd0;
134    pub const INT_16: u8    = 0xd1;
135    pub const INT_32: u8    = 0xd2;
136    pub const INT_64: u8    = 0xd3;
137
138    pub const FIXEXT_1: u8  = 0xd4;
139    pub const FIXEXT_2: u8  = 0xd5;
140    pub const FIXEXT_4: u8  = 0xd6;
141    pub const FIXEXT_8: u8  = 0xd7;
142    pub const FIXEXT_16: u8 = 0xd8;
143
144    pub const STR_8: u8     = 0xd9;
145    pub const STR_16: u8    = 0xda;
146    pub const STR_32: u8    = 0xdb;
147
148    pub const ARRAY_16: u8  = 0xdc;
149    pub const ARRAY_32: u8  = 0xdd;
150
151    pub const MAP_16: u8    = 0xde;
152    pub const MAP_32: u8    = 0xdf;
153}