messagepack_core/
formats.rs

1//! See <https://github.com/msgpack/msgpack/blob/master/spec.md#formats>
2
3const POSITIVE_FIXINT: u8 = 0x00;
4#[allow(dead_code)]
5const NEGATIVE_FIXINT: u8 = 0xe0;
6const FIX_MAP: u8 = 0x80;
7const FIX_ARRAY: u8 = 0x90;
8const FIX_STR: u8 = 0xa0;
9
10const NIL: u8 = 0xc0;
11const NEVER_USED: u8 = 0xc1;
12const FALSE: u8 = 0xc2;
13const TRUE: u8 = 0xc3;
14const BIN8: u8 = 0xc4;
15const BIN16: u8 = 0xc5;
16const BIN32: u8 = 0xc6;
17const EXT8: u8 = 0xc7;
18const EXT16: u8 = 0xc8;
19const EXT32: u8 = 0xc9;
20const FLOAT32: u8 = 0xca;
21const FLOAT64: u8 = 0xcb;
22const UINT8: u8 = 0xcc;
23const UINT16: u8 = 0xcd;
24const UINT32: u8 = 0xce;
25const UINT64: u8 = 0xcf;
26const INT8: u8 = 0xd0;
27const INT16: u8 = 0xd1;
28const INT32: u8 = 0xd2;
29const INT64: u8 = 0xd3;
30const FIXEXT1: u8 = 0xd4;
31const FIXEXT2: u8 = 0xd5;
32const FIXEXT4: u8 = 0xd6;
33const FIXEXT8: u8 = 0xd7;
34const FIXEXT16: u8 = 0xd8;
35const STR8: u8 = 0xd9;
36const STR16: u8 = 0xda;
37const STR32: u8 = 0xdb;
38const ARRAY16: u8 = 0xdc;
39const ARRAY32: u8 = 0xdd;
40const MAP16: u8 = 0xde;
41const MAP32: u8 = 0xdf;
42
43pub const U8_MAX: usize = u8::MAX as usize;
44pub const U16_MAX: usize = u16::MAX as usize;
45pub const U32_MAX: usize = u32::MAX as usize;
46
47#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq)]
48pub enum Format {
49    PositiveFixInt(u8),
50    FixMap(u8),
51    FixArray(u8),
52    FixStr(u8),
53    Nil,
54    NeverUsed,
55    False,
56    True,
57    Bin8,
58    Bin16,
59    Bin32,
60    Ext8,
61    Ext16,
62    Ext32,
63    Float32,
64    Float64,
65    Uint8,
66    Uint16,
67    Uint32,
68    Uint64,
69    Int8,
70    Int16,
71    Int32,
72    Int64,
73    FixExt1,
74    FixExt2,
75    FixExt4,
76    FixExt8,
77    FixExt16,
78    Str8,
79    Str16,
80    Str32,
81    Array16,
82    Array32,
83    Map16,
84    Map32,
85    NegativeFixInt(i8),
86}
87
88impl Format {
89    pub const fn as_byte(&self) -> u8 {
90        match self {
91            Format::PositiveFixInt(v) => POSITIVE_FIXINT | *v,
92            Format::FixMap(l) => FIX_MAP | *l,
93            Format::FixArray(l) => FIX_ARRAY | *l,
94            Format::FixStr(l) => FIX_STR | *l,
95            Format::Nil => NIL,
96            Format::NeverUsed => NEVER_USED,
97            Format::False => FALSE,
98            Format::True => TRUE,
99            Format::Bin8 => BIN8,
100            Format::Bin16 => BIN16,
101            Format::Bin32 => BIN32,
102            Format::Ext8 => EXT8,
103            Format::Ext16 => EXT16,
104            Format::Ext32 => EXT32,
105            Format::Float32 => FLOAT32,
106            Format::Float64 => FLOAT64,
107            Format::Uint8 => UINT8,
108            Format::Uint16 => UINT16,
109            Format::Uint32 => UINT32,
110            Format::Uint64 => UINT64,
111            Format::Int8 => INT8,
112            Format::Int16 => INT16,
113            Format::Int32 => INT32,
114            Format::Int64 => INT64,
115            Format::FixExt1 => FIXEXT1,
116            Format::FixExt2 => FIXEXT2,
117            Format::FixExt4 => FIXEXT4,
118            Format::FixExt8 => FIXEXT8,
119            Format::FixExt16 => FIXEXT16,
120            Format::Str8 => STR8,
121            Format::Str16 => STR16,
122            Format::Str32 => STR32,
123            Format::Array16 => ARRAY16,
124            Format::Array32 => ARRAY32,
125            Format::Map16 => MAP16,
126            Format::Map32 => MAP32,
127            Format::NegativeFixInt(v) => *v as u8,
128        }
129    }
130
131    pub const fn from_byte(byte: u8) -> Self {
132        match byte {
133            0x00..=0x7f => Self::PositiveFixInt(byte & !POSITIVE_FIXINT),
134            0x80..=0x8f => Self::FixMap(byte & !FIX_MAP),
135            0x90..=0x9f => Self::FixArray(byte & !FIX_ARRAY),
136            0xa0..=0xbf => Self::FixStr(byte & !FIX_STR),
137            NIL => Self::Nil,
138            NEVER_USED => Self::NeverUsed,
139            FALSE => Self::False,
140            TRUE => Self::True,
141            BIN8 => Self::Bin8,
142            BIN16 => Self::Bin16,
143            BIN32 => Self::Bin32,
144            EXT8 => Self::Ext8,
145            EXT16 => Self::Ext16,
146            EXT32 => Self::Ext32,
147            FLOAT32 => Self::Float32,
148            FLOAT64 => Self::Float64,
149            UINT8 => Self::Uint8,
150            UINT16 => Self::Uint16,
151            UINT32 => Self::Uint32,
152            UINT64 => Self::Uint64,
153            INT8 => Self::Int8,
154            INT16 => Self::Int16,
155            INT32 => Self::Int32,
156            INT64 => Self::Int64,
157            FIXEXT1 => Self::FixExt1,
158            FIXEXT2 => Self::FixExt2,
159            FIXEXT4 => Self::FixExt4,
160            FIXEXT8 => Self::FixExt8,
161            FIXEXT16 => Self::FixExt16,
162            STR8 => Self::Str8,
163            STR16 => Self::Str16,
164            STR32 => Self::Str32,
165            ARRAY16 => Self::Array16,
166            ARRAY32 => Self::Array32,
167            MAP16 => Self::Map16,
168            MAP32 => Self::Map32,
169            0xe0..=0xff => Self::NegativeFixInt(byte as i8),
170        }
171    }
172
173    pub const fn as_slice(&self) -> [u8; 1] {
174        self.as_byte().to_be_bytes()
175    }
176}
177
178impl IntoIterator for Format {
179    type Item = u8;
180    type IntoIter = core::array::IntoIter<u8, 1>;
181    fn into_iter(self) -> Self::IntoIter {
182        [self.as_byte()].into_iter()
183    }
184}