1use super::primitives::*;
2
3#[derive(Debug)]
12pub enum DecoderLit {
13 Bytes(Vec<u8>),
15
16 Bool(Vec<u8>),
18
19 BoolVec(Vec<u8>),
21
22 Int32(Vec<u8>),
24
25 Int32Vec(Vec<u8>),
27
28 Int64(Vec<u8>),
30
31 Int64Vec(Vec<u8>),
33
34 UInt32(Vec<u8>),
36
37 UInt32Vec(Vec<u8>),
39
40 UInt64(Vec<u8>),
42
43 UInt64Vec(Vec<u8>),
45
46 Float(Vec<u8>),
48
49 FloatVec(Vec<u8>),
51
52 Double(Vec<u8>),
54
55 DoubleVec(Vec<u8>),
57
58 SInt32(Vec<u8>),
61
62 SInt32Vec(Vec<u8>),
65
66 SInt64(Vec<u8>),
69
70 SInt64Vec(Vec<u8>),
73
74 Fixed32(Vec<u8>),
76
77 Fixed32Vec(Vec<u8>),
79
80 Fixed64(Vec<u8>),
82
83 Fixed64Vec(Vec<u8>),
85
86 SFixed32(Vec<u8>),
88
89 SFixed32Vec(Vec<u8>),
91
92 SFixed64(Vec<u8>),
94
95 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}