messagepack_core/
formats.rs

1//! MessagePack format markers.
2//!
3//! See <https://github.com/msgpack/msgpack/blob/master/spec.md#formats>
4
5const POSITIVE_FIXINT: u8 = 0x00;
6#[allow(dead_code)]
7const NEGATIVE_FIXINT: u8 = 0xe0;
8const FIX_MAP: u8 = 0x80;
9const FIX_ARRAY: u8 = 0x90;
10const FIX_STR: u8 = 0xa0;
11
12const NIL: u8 = 0xc0;
13const NEVER_USED: u8 = 0xc1;
14const FALSE: u8 = 0xc2;
15const TRUE: u8 = 0xc3;
16const BIN8: u8 = 0xc4;
17const BIN16: u8 = 0xc5;
18const BIN32: u8 = 0xc6;
19const EXT8: u8 = 0xc7;
20const EXT16: u8 = 0xc8;
21const EXT32: u8 = 0xc9;
22const FLOAT32: u8 = 0xca;
23const FLOAT64: u8 = 0xcb;
24const UINT8: u8 = 0xcc;
25const UINT16: u8 = 0xcd;
26const UINT32: u8 = 0xce;
27const UINT64: u8 = 0xcf;
28const INT8: u8 = 0xd0;
29const INT16: u8 = 0xd1;
30const INT32: u8 = 0xd2;
31const INT64: u8 = 0xd3;
32const FIXEXT1: u8 = 0xd4;
33const FIXEXT2: u8 = 0xd5;
34const FIXEXT4: u8 = 0xd6;
35const FIXEXT8: u8 = 0xd7;
36const FIXEXT16: u8 = 0xd8;
37const STR8: u8 = 0xd9;
38const STR16: u8 = 0xda;
39const STR32: u8 = 0xdb;
40const ARRAY16: u8 = 0xdc;
41const ARRAY32: u8 = 0xdd;
42const MAP16: u8 = 0xde;
43const MAP32: u8 = 0xdf;
44
45/// MessagePack format marker.
46#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq)]
47pub enum Format {
48    /// Positive fixint (0xxxxxxx): stores a positive 7‑bit integer in the marker.
49    PositiveFixInt(u8),
50    /// Fixmap (1000xxxx): small map with length encoded in the marker.
51    FixMap(u8),
52    /// Fixarray (1001xxxx): small array with length encoded in the marker.
53    FixArray(u8),
54    /// Fixstr (101xxxxx): small string with byte length in the marker.
55    FixStr(u8),
56    /// Nil (0xc0).
57    Nil,
58    /// Reserved (0xc1): never used.
59    NeverUsed,
60    /// False (0xc2).
61    False,
62    /// True (0xc3).
63    True,
64    /// Binary with 8‑bit length (0xc4).
65    Bin8,
66    /// Binary with 16‑bit length (0xc5).
67    Bin16,
68    /// Binary with 32‑bit length (0xc6).
69    Bin32,
70    /// Extension with 8‑bit length (0xc7).
71    Ext8,
72    /// Extension with 16‑bit length (0xc8).
73    Ext16,
74    /// Extension with 32‑bit length (0xc9).
75    Ext32,
76    /// Float32 (0xca).
77    Float32,
78    /// Float64 (0xcb).
79    Float64,
80    /// Unsigned 8‑bit integer (0xcc).
81    Uint8,
82    /// Unsigned 16‑bit integer (0xcd).
83    Uint16,
84    /// Unsigned 32‑bit integer (0xce).
85    Uint32,
86    /// Unsigned 64‑bit integer (0xcf).
87    Uint64,
88    /// Signed 8‑bit integer (0xd0).
89    Int8,
90    /// Signed 16‑bit integer (0xd1).
91    Int16,
92    /// Signed 32‑bit integer (0xd2).
93    Int32,
94    /// Signed 64‑bit integer (0xd3).
95    Int64,
96    /// Fixext 1 (0xd4).
97    FixExt1,
98    /// Fixext 2 (0xd5).
99    FixExt2,
100    /// Fixext 4 (0xd6).
101    FixExt4,
102    /// Fixext 8 (0xd7).
103    FixExt8,
104    /// Fixext 16 (0xd8).
105    FixExt16,
106    /// Str8: UTF‑8 string with 8‑bit length (0xd9).
107    Str8,
108    /// Str16: UTF‑8 string with 16‑bit length (0xda).
109    Str16,
110    /// Str32: UTF‑8 string with 32‑bit length (0xdb).
111    Str32,
112    /// Array16: array with 16‑bit length (0xdc).
113    Array16,
114    /// Array32: array with 32‑bit length (0xdd).
115    Array32,
116    /// Map16: map with 16‑bit length (0xde).
117    Map16,
118    /// Map32: map with 32‑bit length (0xdf).
119    Map32,
120    /// Negative fixint (111xxxxx): stores a negative 5‑bit integer in the marker.
121    NegativeFixInt(i8),
122}
123
124impl Format {
125    /// Return the marker byte for this format.
126    pub const fn as_byte(&self) -> u8 {
127        match self {
128            Format::PositiveFixInt(v) => POSITIVE_FIXINT | *v,
129            Format::FixMap(l) => FIX_MAP | *l,
130            Format::FixArray(l) => FIX_ARRAY | *l,
131            Format::FixStr(l) => FIX_STR | *l,
132            Format::Nil => NIL,
133            Format::NeverUsed => NEVER_USED,
134            Format::False => FALSE,
135            Format::True => TRUE,
136            Format::Bin8 => BIN8,
137            Format::Bin16 => BIN16,
138            Format::Bin32 => BIN32,
139            Format::Ext8 => EXT8,
140            Format::Ext16 => EXT16,
141            Format::Ext32 => EXT32,
142            Format::Float32 => FLOAT32,
143            Format::Float64 => FLOAT64,
144            Format::Uint8 => UINT8,
145            Format::Uint16 => UINT16,
146            Format::Uint32 => UINT32,
147            Format::Uint64 => UINT64,
148            Format::Int8 => INT8,
149            Format::Int16 => INT16,
150            Format::Int32 => INT32,
151            Format::Int64 => INT64,
152            Format::FixExt1 => FIXEXT1,
153            Format::FixExt2 => FIXEXT2,
154            Format::FixExt4 => FIXEXT4,
155            Format::FixExt8 => FIXEXT8,
156            Format::FixExt16 => FIXEXT16,
157            Format::Str8 => STR8,
158            Format::Str16 => STR16,
159            Format::Str32 => STR32,
160            Format::Array16 => ARRAY16,
161            Format::Array32 => ARRAY32,
162            Format::Map16 => MAP16,
163            Format::Map32 => MAP32,
164            Format::NegativeFixInt(v) => *v as u8,
165        }
166    }
167
168    /// Parse a marker byte into a [`Format`] value.
169    pub const fn from_byte(byte: u8) -> Self {
170        match byte {
171            0x00..=0x7f => Self::PositiveFixInt(byte & !POSITIVE_FIXINT),
172            0x80..=0x8f => Self::FixMap(byte & !FIX_MAP),
173            0x90..=0x9f => Self::FixArray(byte & !FIX_ARRAY),
174            0xa0..=0xbf => Self::FixStr(byte & !FIX_STR),
175            NIL => Self::Nil,
176            NEVER_USED => Self::NeverUsed,
177            FALSE => Self::False,
178            TRUE => Self::True,
179            BIN8 => Self::Bin8,
180            BIN16 => Self::Bin16,
181            BIN32 => Self::Bin32,
182            EXT8 => Self::Ext8,
183            EXT16 => Self::Ext16,
184            EXT32 => Self::Ext32,
185            FLOAT32 => Self::Float32,
186            FLOAT64 => Self::Float64,
187            UINT8 => Self::Uint8,
188            UINT16 => Self::Uint16,
189            UINT32 => Self::Uint32,
190            UINT64 => Self::Uint64,
191            INT8 => Self::Int8,
192            INT16 => Self::Int16,
193            INT32 => Self::Int32,
194            INT64 => Self::Int64,
195            FIXEXT1 => Self::FixExt1,
196            FIXEXT2 => Self::FixExt2,
197            FIXEXT4 => Self::FixExt4,
198            FIXEXT8 => Self::FixExt8,
199            FIXEXT16 => Self::FixExt16,
200            STR8 => Self::Str8,
201            STR16 => Self::Str16,
202            STR32 => Self::Str32,
203            ARRAY16 => Self::Array16,
204            ARRAY32 => Self::Array32,
205            MAP16 => Self::Map16,
206            MAP32 => Self::Map32,
207            0xe0..=0xff => Self::NegativeFixInt(byte as i8),
208        }
209    }
210
211    /// Return the marker byte wrapped in a single‑byte array.
212    pub const fn as_slice(&self) -> [u8; 1] {
213        self.as_byte().to_be_bytes()
214    }
215}
216
217impl IntoIterator for Format {
218    type Item = u8;
219    type IntoIter = core::array::IntoIter<u8, 1>;
220    fn into_iter(self) -> Self::IntoIter {
221        [self.as_byte()].into_iter()
222    }
223}