1use super::BasicDecode;
5use byteordered::{ByteOrdered, Endianness};
6use std::io::Read;
7
8type Result<T> = std::io::Result<T>;
9
10#[derive(Debug, Default, Copy, Clone, Eq, Hash, PartialEq)]
12pub struct LittleEndianBasicDecoder;
13
14impl BasicDecode for LittleEndianBasicDecoder {
15 fn endianness(&self) -> Endianness {
16 Endianness::Little
17 }
18
19 fn decode_us<S>(&self, source: S) -> Result<u16>
20 where
21 S: Read,
22 {
23 ByteOrdered::le(source).read_u16()
24 }
25
26 fn decode_us_into<S>(&self, source: S, target: &mut [u16]) -> Result<()>
27 where
28 S: Read,
29 {
30 ByteOrdered::le(source).read_u16_into(target)
31 }
32
33 fn decode_ul<S>(&self, source: S) -> Result<u32>
34 where
35 S: Read,
36 {
37 ByteOrdered::le(source).read_u32()
38 }
39
40 fn decode_ul_into<S>(&self, source: S, target: &mut [u32]) -> Result<()>
41 where
42 S: Read,
43 {
44 ByteOrdered::le(source).read_u32_into(target)
45 }
46
47 fn decode_uv<S>(&self, source: S) -> Result<u64>
48 where
49 S: Read,
50 {
51 ByteOrdered::le(source).read_u64()
52 }
53
54 fn decode_uv_into<S>(&self, source: S, target: &mut [u64]) -> Result<()>
55 where
56 S: Read,
57 {
58 ByteOrdered::le(source).read_u64_into(target)
59 }
60
61 fn decode_ss<S>(&self, source: S) -> Result<i16>
62 where
63 S: Read,
64 {
65 ByteOrdered::le(source).read_i16()
66 }
67
68 fn decode_ss_into<S>(&self, source: S, target: &mut [i16]) -> Result<()>
69 where
70 S: Read,
71 {
72 ByteOrdered::le(source).read_i16_into(target)
73 }
74
75 fn decode_sl<S>(&self, source: S) -> Result<i32>
76 where
77 S: Read,
78 {
79 ByteOrdered::le(source).read_i32()
80 }
81
82 fn decode_sl_into<S>(&self, source: S, target: &mut [i32]) -> Result<()>
83 where
84 S: Read,
85 {
86 ByteOrdered::le(source).read_i32_into(target)
87 }
88
89 fn decode_sv<S>(&self, source: S) -> Result<i64>
90 where
91 S: Read,
92 {
93 ByteOrdered::le(source).read_i64()
94 }
95
96 fn decode_sv_into<S>(&self, source: S, target: &mut [i64]) -> Result<()>
97 where
98 S: Read,
99 {
100 ByteOrdered::le(source).read_i64_into(target)
101 }
102
103 fn decode_fl<S>(&self, source: S) -> Result<f32>
104 where
105 S: Read,
106 {
107 ByteOrdered::le(source).read_f32()
108 }
109
110 fn decode_fl_into<S>(&self, source: S, target: &mut [f32]) -> Result<()>
111 where
112 S: Read,
113 {
114 ByteOrdered::le(source).read_f32_into(target)
115 }
116
117 fn decode_fd<S>(&self, source: S) -> Result<f64>
118 where
119 S: Read,
120 {
121 ByteOrdered::le(source).read_f64()
122 }
123
124 fn decode_fd_into<S>(&self, source: S, target: &mut [f64]) -> Result<()>
125 where
126 S: Read,
127 {
128 ByteOrdered::le(source).read_f64_into(target)
129 }
130}
131
132#[derive(Debug, Default, Copy, Clone, Eq, Hash, PartialEq)]
134pub struct BigEndianBasicDecoder;
135
136impl BasicDecode for BigEndianBasicDecoder {
137 fn endianness(&self) -> Endianness {
138 Endianness::Big
139 }
140
141 fn decode_us<S>(&self, source: S) -> Result<u16>
142 where
143 S: Read,
144 {
145 ByteOrdered::be(source).read_u16()
146 }
147
148 fn decode_us_into<S>(&self, source: S, target: &mut [u16]) -> Result<()>
149 where
150 S: Read,
151 {
152 ByteOrdered::be(source).read_u16_into(target)
153 }
154
155 fn decode_ul<S>(&self, source: S) -> Result<u32>
156 where
157 S: Read,
158 {
159 ByteOrdered::be(source).read_u32()
160 }
161
162 fn decode_ul_into<S>(&self, source: S, target: &mut [u32]) -> Result<()>
163 where
164 S: Read,
165 {
166 ByteOrdered::be(source).read_u32_into(target)
167 }
168
169 fn decode_uv<S>(&self, source: S) -> Result<u64>
170 where
171 S: Read,
172 {
173 ByteOrdered::be(source).read_u64()
174 }
175
176 fn decode_uv_into<S>(&self, source: S, target: &mut [u64]) -> Result<()>
177 where
178 S: Read,
179 {
180 ByteOrdered::be(source).read_u64_into(target)
181 }
182
183 fn decode_ss<S>(&self, source: S) -> Result<i16>
184 where
185 S: Read,
186 {
187 ByteOrdered::be(source).read_i16()
188 }
189
190 fn decode_ss_into<S>(&self, source: S, target: &mut [i16]) -> Result<()>
191 where
192 S: Read,
193 {
194 ByteOrdered::be(source).read_i16_into(target)
195 }
196
197 fn decode_sl<S>(&self, source: S) -> Result<i32>
198 where
199 S: Read,
200 {
201 ByteOrdered::be(source).read_i32()
202 }
203
204 fn decode_sl_into<S>(&self, source: S, target: &mut [i32]) -> Result<()>
205 where
206 S: Read,
207 {
208 ByteOrdered::be(source).read_i32_into(target)
209 }
210
211 fn decode_sv<S>(&self, source: S) -> Result<i64>
212 where
213 S: Read,
214 {
215 ByteOrdered::be(source).read_i64()
216 }
217
218 fn decode_sv_into<S>(&self, source: S, target: &mut [i64]) -> Result<()>
219 where
220 S: Read,
221 {
222 ByteOrdered::be(source).read_i64_into(target)
223 }
224
225 fn decode_fl<S>(&self, source: S) -> Result<f32>
226 where
227 S: Read,
228 {
229 ByteOrdered::be(source).read_f32()
230 }
231
232 fn decode_fl_into<S>(&self, source: S, target: &mut [f32]) -> Result<()>
233 where
234 S: Read,
235 {
236 ByteOrdered::be(source).read_f32_into(target)
237 }
238
239 fn decode_fd<S>(&self, source: S) -> Result<f64>
240 where
241 S: Read,
242 {
243 ByteOrdered::be(source).read_f64()
244 }
245
246 fn decode_fd_into<S>(&self, source: S, target: &mut [f64]) -> Result<()>
247 where
248 S: Read,
249 {
250 ByteOrdered::be(source).read_f64_into(target)
251 }
252}
253
254#[derive(Debug, Clone, PartialEq)]
258pub enum BasicDecoder {
259 LE(LittleEndianBasicDecoder),
261 BE(BigEndianBasicDecoder),
263}
264
265impl BasicDecoder {
266 pub fn new(endianness: Endianness) -> Self {
268 match endianness {
269 Endianness::Little => LE(LittleEndianBasicDecoder),
270 Endianness::Big => BE(BigEndianBasicDecoder),
271 }
272 }
273}
274
275use self::BasicDecoder::{BE, LE};
276
277impl From<Endianness> for BasicDecoder {
278 fn from(endianness: Endianness) -> Self {
279 BasicDecoder::new(endianness)
280 }
281}
282
283macro_rules! for_both {
284 ($s: expr, |$e: ident| $f: expr) => {
285 match *$s {
286 LE(ref $e) => $f,
287 BE(ref $e) => $f,
288 }
289 };
290}
291
292impl BasicDecode for BasicDecoder {
293 fn endianness(&self) -> Endianness {
294 match *self {
295 LE(_) => Endianness::Little,
296 BE(_) => Endianness::Big,
297 }
298 }
299
300 fn decode_us<S>(&self, source: S) -> Result<u16>
301 where
302 S: Read,
303 {
304 for_both!(self, |e| e.decode_us(source))
305 }
306
307 fn decode_us_into<S>(&self, source: S, target: &mut [u16]) -> Result<()>
308 where
309 S: Read,
310 {
311 for_both!(self, |e| e.decode_us_into(source, target))
312 }
313
314 fn decode_ul<S>(&self, source: S) -> Result<u32>
315 where
316 S: Read,
317 {
318 for_both!(self, |e| e.decode_ul(source))
319 }
320
321 fn decode_ul_into<S>(&self, source: S, target: &mut [u32]) -> Result<()>
322 where
323 S: Read,
324 {
325 for_both!(self, |e| e.decode_ul_into(source, target))
326 }
327
328 fn decode_uv<S>(&self, source: S) -> Result<u64>
329 where
330 S: Read,
331 {
332 for_both!(self, |e| e.decode_uv(source))
333 }
334
335 fn decode_uv_into<S>(&self, source: S, target: &mut [u64]) -> Result<()>
336 where
337 S: Read,
338 {
339 for_both!(self, |e| e.decode_uv_into(source, target))
340 }
341
342 fn decode_ss<S>(&self, source: S) -> Result<i16>
343 where
344 S: Read,
345 {
346 for_both!(self, |e| e.decode_ss(source))
347 }
348
349 fn decode_ss_into<S>(&self, source: S, target: &mut [i16]) -> Result<()>
350 where
351 S: Read,
352 {
353 for_both!(self, |e| e.decode_ss_into(source, target))
354 }
355
356 fn decode_sl<S>(&self, source: S) -> Result<i32>
357 where
358 S: Read,
359 {
360 for_both!(self, |e| e.decode_sl(source))
361 }
362
363 fn decode_sl_into<S>(&self, source: S, target: &mut [i32]) -> Result<()>
364 where
365 S: Read,
366 {
367 for_both!(self, |e| e.decode_sl_into(source, target))
368 }
369
370 fn decode_sv<S>(&self, source: S) -> Result<i64>
371 where
372 S: Read,
373 {
374 for_both!(self, |e| e.decode_sv(source))
375 }
376
377 fn decode_sv_into<S>(&self, source: S, target: &mut [i64]) -> Result<()>
378 where
379 S: Read,
380 {
381 for_both!(self, |e| e.decode_sv_into(source, target))
382 }
383
384 fn decode_fl<S>(&self, source: S) -> Result<f32>
385 where
386 S: Read,
387 {
388 for_both!(self, |e| e.decode_fl(source))
389 }
390
391 fn decode_fl_into<S>(&self, source: S, target: &mut [f32]) -> Result<()>
392 where
393 S: Read,
394 {
395 for_both!(self, |e| e.decode_fl_into(source, target))
396 }
397
398 fn decode_fd<S>(&self, source: S) -> Result<f64>
399 where
400 S: Read,
401 {
402 for_both!(self, |e| e.decode_fd(source))
403 }
404
405 fn decode_fd_into<S>(&self, source: S, target: &mut [f64]) -> Result<()>
406 where
407 S: Read,
408 {
409 for_both!(self, |e| e.decode_fd_into(source, target))
410 }
411}
412
413#[cfg(test)]
414mod tests {
415
416 use super::*;
417
418 #[test]
419 fn test_read_integers() {
420 let data: &[u8] = &[0xC3, 0x3C, 0x33, 0xCC, 0x55, 0xAA, 0x55, 0xAA];
421
422 let le = LittleEndianBasicDecoder;
423 let be = BigEndianBasicDecoder;
424
425 assert_eq!(le.decode_us(data).unwrap(), 0x3CC3);
426 assert_eq!(be.decode_us(data).unwrap(), 0xC33C);
427 assert_eq!(le.decode_ul(data).unwrap(), 0xCC333CC3);
428 assert_eq!(be.decode_ul(data).unwrap(), 0xC33C33CC);
429 assert_eq!(le.decode_uv(data).unwrap(), 0xAA55AA55_CC333CC3);
430 assert_eq!(be.decode_uv(data).unwrap(), 0xC33C33CC_55AA55AA);
431
432 let le = BasicDecoder::new(Endianness::Little);
433 let be = BasicDecoder::new(Endianness::Big);
434
435 assert_eq!(le.decode_us(data).unwrap(), 0x3CC3);
436 assert_eq!(be.decode_us(data).unwrap(), 0xC33C);
437 assert_eq!(le.decode_ul(data).unwrap(), 0xCC333CC3);
438 assert_eq!(be.decode_ul(data).unwrap(), 0xC33C33CC);
439 assert_eq!(le.decode_uv(data).unwrap(), 0xAA55AA55_CC333CC3);
440 assert_eq!(be.decode_uv(data).unwrap(), 0xC33C33CC_55AA55AA);
441 }
442
443 #[test]
444 fn test_read_integers_into() {
445 let data: &[u8] = &[0xC3, 0x3C, 0x33, 0xCC, 0x55, 0xAA, 0x55, 0xAA];
446
447 let le = LittleEndianBasicDecoder;
448 let be = BigEndianBasicDecoder;
449
450 let mut out_le = [0; 4];
451 le.decode_us_into(data, &mut out_le).unwrap();
452 assert_eq!(out_le, [0x3CC3, 0xCC33, 0xAA55, 0xAA55]);
453
454 let mut out_be = [0; 4];
455 be.decode_us_into(data, &mut out_be).unwrap();
456 assert_eq!(out_be, [0xC33C, 0x33CC, 0x55AA, 0x55AA]);
457
458 let mut out_le = [0; 2];
459 le.decode_ul_into(data, &mut out_le).unwrap();
460 assert_eq!(out_le, [0xCC33_3CC3, 0xAA55_AA55]);
461
462 let mut out_be = [0; 2];
463 be.decode_ul_into(data, &mut out_be).unwrap();
464 assert_eq!(out_be, [0xC33C_33CC, 0x55AA_55AA]);
465
466 let le = BasicDecoder::new(Endianness::Little);
467 let be = BasicDecoder::new(Endianness::Big);
468
469 let mut out_le = [0; 4];
470 le.decode_us_into(data, &mut out_le).unwrap();
471 assert_eq!(out_le, [0x3CC3, 0xCC33, 0xAA55, 0xAA55]);
472
473 let mut out_be = [0; 4];
474 be.decode_us_into(data, &mut out_be).unwrap();
475 assert_eq!(out_be, [0xC33C, 0x33CC, 0x55AA, 0x55AA]);
476
477 let mut out_le = [0; 2];
478 le.decode_ul_into(data, &mut out_le).unwrap();
479 assert_eq!(out_le, [0xCC33_3CC3, 0xAA55_AA55]);
480
481 let mut out_be = [0; 2];
482 be.decode_ul_into(data, &mut out_be).unwrap();
483 assert_eq!(out_be, [0xC33C_33CC, 0x55AA_55AA]);
484 }
485}