httlib_protos/decoder/
lit.rs

1use super::primitives::*;
2
3/// Provides decoder output format options.
4/// 
5/// This is a list of all binary formats supported by the decoder.
6/// 
7/// Note that bytes held by each key are considered "safe". Therefore, we should
8/// only pass valid vectors as key arguments when instantiating this object.
9/// According to this fact, the 'std::convert::From' property is implemented
10/// instead of 'std::convert::TryFrom'.
11#[derive(Debug)]
12pub enum DecoderLit {
13    /// Represents `binary` format of wire type `2`.
14    Bytes(Vec<u8>),
15
16    /// Represents `bool` format of wire type `0`.
17    Bool(Vec<u8>),
18
19    /// Represents `bool` format of wire type `2` for packed repeated fields.
20    BoolVec(Vec<u8>),
21
22    /// Represents `int32` format of wire type `0`.
23    Int32(Vec<u8>),
24
25    /// Represents `int32` format of wire type `0` for packed repeated fields.
26    Int32Vec(Vec<u8>),
27
28    /// Represents `int64` format of wire type `0`.
29    Int64(Vec<u8>),
30
31    /// Represents `int64` format of wire type `0` for packed repeated fields.
32    Int64Vec(Vec<u8>),
33
34    /// Represents `uint32` format of wire type `0`.
35    UInt32(Vec<u8>),
36
37    /// Represents `uint32` format of wire type `0` for packed repeated fields.
38    UInt32Vec(Vec<u8>),
39
40    /// Represents `uint64` format of wire type `0`.
41    UInt64(Vec<u8>),
42
43    /// Represents `uint64` format of wire type `0` for packed repeated fields.
44    UInt64Vec(Vec<u8>),
45
46    /// Represents `float` format of wire type `5`.
47    Float(Vec<u8>),
48
49    /// Represents `float` format of wire type `5` for packed repeated fields.
50    FloatVec(Vec<u8>),
51
52    /// Represents `uint32` format of wire type `1`.
53    Double(Vec<u8>),
54
55    /// Represents `double` format of wire type `1` for packed repeated fields.
56    DoubleVec(Vec<u8>),
57
58    /// Represents `sint32` format of wire type `0`. Use it when the value is
59    /// likely to be negative.
60    SInt32(Vec<u8>),
61
62    /// Represents `sint32` format of wire type `0` for packed repeated fields.
63    /// Use it when the values are likely to be negative.
64    SInt32Vec(Vec<u8>),
65
66    /// Represents `sint64` format of wire type `0`. Use it when the value is
67    /// likely to be negative.
68    SInt64(Vec<u8>),
69
70    /// Represents `sint64` format of wire type `0` for packed repeated fields.
71    /// Use it when the values are likely to be negative.
72    SInt64Vec(Vec<u8>),
73
74    /// Represents `fixed32` format of wire type `5`.
75    Fixed32(Vec<u8>),
76
77    /// Represents `fixed32` format of wire type `5` for packed repeated fields.
78    Fixed32Vec(Vec<u8>),
79
80    /// Represents `fixed64` format of wire type `1`.
81    Fixed64(Vec<u8>),
82
83    /// Represents `fixed64` format of wire type `1` for packed repeated fields.
84    Fixed64Vec(Vec<u8>),
85
86    /// Represents `sfixed32` format of wire type `5`.
87    SFixed32(Vec<u8>),
88
89    /// Represents `sfixed32` format of wire type `5` for packed repeated fields.
90    SFixed32Vec(Vec<u8>),
91
92    /// Represents `sfixed64` format of wire type `1`.
93    SFixed64(Vec<u8>),
94
95    /// Represents `sfixed64` format of wire type `1` for packed repeated
96    /// fields.
97    SFixed64Vec(Vec<u8>),
98}
99
100impl From<DecoderLit> for bool {
101    fn from(lit: DecoderLit) -> Self {
102        let mut dst = false;
103        match lit {
104            DecoderLit::Bool(byt) => decode_bool(&byt, &mut dst).unwrap_or(0),
105            _ => return dst,
106        };
107        dst
108    }
109}
110
111impl From<DecoderLit> for Vec<bool> {
112    fn from(lit: DecoderLit) -> Self {
113        let mut dst = vec![];
114        match lit {
115            DecoderLit::BoolVec(byt) => decode_bool_vec(&byt, &mut dst).unwrap_or(0),
116            _ => return dst,
117        };
118        dst
119    }
120}
121
122impl From<DecoderLit> for i32 {
123    fn from(lit: DecoderLit) -> Self {
124        let mut dst = 0i32;
125        match lit {
126            DecoderLit::Int32(byt) => decode_int32(&byt, &mut dst).unwrap_or(0),
127            DecoderLit::SInt32(byt) => decode_sint32(&byt, &mut dst).unwrap_or(0),
128            DecoderLit::SFixed32(byt) => decode_sfixed32(&byt, &mut dst).unwrap_or(0),
129            _ => return dst,
130        };
131        dst
132    }
133}
134
135impl From<DecoderLit> for Vec<i32> {
136    fn from(lit: DecoderLit) -> Self {
137        let mut dst = vec![];
138        match lit {
139            DecoderLit::Int32Vec(byt) => decode_int32_vec(&byt, &mut dst).unwrap_or(0),
140            DecoderLit::SInt32Vec(byt) => decode_sint32_vec(&byt, &mut dst).unwrap_or(0),
141            DecoderLit::SFixed32Vec(byt) => decode_sfixed32_vec(&byt, &mut dst).unwrap_or(0),
142            _ => return dst,
143        };
144        dst
145    }
146}
147
148impl From<DecoderLit> for i64 {
149    fn from(lit: DecoderLit) -> Self {
150        let mut dst = 0i64;
151        match lit {
152            DecoderLit::Int64(byt) => decode_int64(&byt, &mut dst).unwrap_or(0),
153            DecoderLit::SInt64(byt) => decode_sint64(&byt, &mut dst).unwrap_or(0),
154            DecoderLit::SFixed64(byt) => decode_sfixed64(&byt, &mut dst).unwrap_or(0),
155            _ => return dst,
156        };
157        dst
158    }
159}
160
161impl From<DecoderLit> for Vec<i64> {
162    fn from(lit: DecoderLit) -> Self {
163        let mut dst = vec![];
164        match lit {
165            DecoderLit::Int64Vec(byt) => decode_int64_vec(&byt, &mut dst).unwrap_or(0),
166            DecoderLit::SInt64Vec(byt) => decode_sint64_vec(&byt, &mut dst).unwrap_or(0),
167            DecoderLit::SFixed64Vec(byt) => decode_sfixed64_vec(&byt, &mut dst).unwrap_or(0),
168            _ => return dst,
169        };
170        dst
171    }
172}
173
174impl From<DecoderLit> for u32 {
175    fn from(lit: DecoderLit) -> Self {
176        let mut dst = 0u32;
177        match lit {
178            DecoderLit::UInt32(byt) => decode_uint32(&byt, &mut dst).unwrap_or(0),
179            DecoderLit::Fixed32(byt) => decode_fixed32(&byt, &mut dst).unwrap_or(0),
180            _ => return dst,
181        };
182        dst
183    }
184}
185
186impl From<DecoderLit> for Vec<u32> {
187    fn from(lit: DecoderLit) -> Self {
188        let mut dst = vec![];
189        match lit {
190            DecoderLit::UInt32Vec(byt) => decode_uint32_vec(&byt, &mut dst).unwrap_or(0),
191            DecoderLit::Fixed32Vec(byt) => decode_fixed32_vec(&byt, &mut dst).unwrap_or(0),
192            _ => return dst,
193        };
194        dst
195    }
196}
197
198impl From<DecoderLit> for u64 {
199    fn from(lit: DecoderLit) -> Self {
200        let mut dst = 0u64;
201        match lit {
202            DecoderLit::UInt64(byt) => decode_uint64(&byt, &mut dst).unwrap_or(0),
203            DecoderLit::Fixed64(byt) => decode_fixed64(&byt, &mut dst).unwrap_or(0),
204            _ => return dst,
205        };
206        dst
207    }
208}
209
210impl From<DecoderLit> for Vec<u64> {
211    fn from(lit: DecoderLit) -> Self {
212        let mut dst = vec![];
213        match lit {
214            DecoderLit::UInt64Vec(byt) => decode_uint64_vec(&byt, &mut dst).unwrap_or(0),
215            DecoderLit::Fixed64Vec(byt) => decode_fixed64_vec(&byt, &mut dst).unwrap_or(0),
216            _ => return dst,
217        };
218        dst
219    }
220}
221
222impl From<DecoderLit> for f32 {
223    fn from(lit: DecoderLit) -> Self {
224        let mut dst = 0.0f32;
225        match lit {
226            DecoderLit::Float(byt) => decode_float(&byt, &mut dst).unwrap_or(0),
227            _ => return dst,
228        };
229        dst
230    }
231}
232
233impl From<DecoderLit> for Vec<f32> {
234    fn from(lit: DecoderLit) -> Self {
235        let mut dst = vec![];
236        match lit {
237            DecoderLit::FloatVec(byt) => decode_float_vec(&byt, &mut dst).unwrap_or(0),
238            _ => return dst,
239        };
240        dst
241    }
242}
243
244impl From<DecoderLit> for f64 {
245    fn from(lit: DecoderLit) -> Self {
246        let mut dst = 0.0f64;
247        match lit {
248            DecoderLit::Double(byt) => decode_double(&byt, &mut dst).unwrap_or(0),
249            _ => return dst,
250        };
251        dst
252    }
253}
254
255impl From<DecoderLit> for Vec<f64> {
256    fn from(lit: DecoderLit) -> Self {
257        let mut dst = vec![];
258        match lit {
259            DecoderLit::DoubleVec(byt) => decode_double_vec(&byt, &mut dst).unwrap_or(0),
260            _ => return dst,
261        };
262        dst
263    }
264}
265
266impl From<DecoderLit> for Vec<u8> {
267    fn from(lit: DecoderLit) -> Self {
268        match lit {
269            DecoderLit::Bytes(byt) => byt,
270            _ => Vec::new(),
271        }
272    }
273}
274
275impl From<DecoderLit> for String {
276    fn from(lit: DecoderLit) -> Self {
277        match lit {
278            DecoderLit::Bytes(byt) => {
279                match String::from_utf8(byt) {
280                    Ok(s) => s,
281                    _ => String::new(),
282                }
283            },
284            _ => String::new(),
285        }
286    }
287}