jbytes/decode/
mod.rs

1mod impls_bool;
2mod impls_int;
3mod impls_float;
4mod impls_char;
5mod impls_string;
6mod impls_bytes;
7mod impls_tuple;
8mod impls_array;
9mod impls_vec;
10#[cfg(feature = "std")]
11mod impls_hashmap;
12#[cfg(feature = "std")]
13mod impls_hashset;
14mod impls_hex;
15mod impls_other;
16mod impls_option;
17mod impls_ipaddress;
18mod impls_macaddress;
19mod impls_netaddress;
20mod impls_mark;
21
22use crate::{
23    JResult,
24    ContainerAttrModifiers, FieldAttrModifiers,
25    BufRead
26};
27
28
29/// This is bytes decoding trait. 
30/// 
31/// # Example
32/// 
33/// ```no_test
34/// use jbytes::{
35///     JResult, BufRead,
36///     ByteDecode, BorrowByteDecode,
37///     ContainerAttrModifiers, FieldAttrModifiers,
38/// };
39/// 
40/// 
41/// impl ByteDecode for bool {
42///     fn decode_inner<I: BufRead>(input: &I, _cattr: Option<&ContainerAttrModifiers>,
43///                                            _fattr: Option<&FieldAttrModifiers>) -> JResult<Self>
44///     where 
45///         Self: Sized
46///     {
47///         input.take_bool()
48///     }
49/// }
50/// ```
51pub trait ByteDecode {
52    fn decode_inner<I: BufRead>(input: &I, cattr: Option<&ContainerAttrModifiers>,
53                                           fattr: Option<&FieldAttrModifiers>) -> JResult<Self>
54    where 
55        Self: Sized
56    ;
57
58    #[inline]
59    fn decode<I: BufRead>(input: &I) -> JResult<Self>
60    where 
61        Self: Sized
62    {
63        Self::decode_inner(input, None, None)
64    }
65}
66
67
68/// This is bytes decoding trait of borrow type.
69/// 
70/// # Example
71/// 
72/// ```no_test
73/// use jbytes::{
74///     JResult, BufRead,
75///     ByteDecode, BorrowByteDecode,
76///     ContainerAttrModifiers, FieldAttrModifiers,
77/// };
78/// 
79/// 
80/// impl<'de> BorrowByteDecode<'de> for bool {
81///     fn decode_inner<I: BufRead>(input: &'de I, _cattr: Option<&ContainerAttrModifiers>,
82///                                                          _fattr: Option<&FieldAttrModifiers>) -> JResult<Self>
83///     where 
84///         Self: Sized
85///     {
86///         input.take_bool()
87///     }
88/// }
89/// ```
90pub trait BorrowByteDecode<'de> {
91    fn decode_inner<I: BufRead>(input: &'de I, cattr: Option<&ContainerAttrModifiers>,
92                                fattr: Option<&FieldAttrModifiers>) -> JResult<Self>
93    where 
94        Self: Sized
95    ;
96
97    #[inline]
98    fn decode<I: BufRead>(input: &'de I) -> JResult<Self>
99    where 
100        Self: Sized
101    {
102        Self::decode_inner(input, None, None)
103    }
104}
105
106
107#[inline]
108fn get_count_and_try_count<I: BufRead>(input: &I, cattr: Option<&ContainerAttrModifiers>, fattr: Option<&FieldAttrModifiers>) -> JResult<(usize, Option<usize>)>
109{
110    let mut count = 0;
111    let mut try_count = None;
112
113    if let Some(fr) = fattr {
114        if let Some(count_tmp) = fr.count {
115            count = count_tmp;
116        }
117        else if fr.try_count.is_some() {
118            try_count = fr.try_count;
119        } else if let Some(byte_count) = fr.byte_count_outside {
120            count = input.take_byteorder_uint(byte_count, crate::get_byteorder(cattr, fattr))? as usize;
121        }
122        else {
123            // default: take 1 byte
124            count = input.take_u8()? as usize;
125        }
126    } else {
127        // default: take 1 byte
128        count = input.take_u8()? as usize;
129    }
130
131    Ok((count, try_count))
132}