1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
use std::{convert::TryFrom, error::Error};
use std::{fmt, vec::Vec};

const MOST_SIGNIFICANT_BIT_PATTERN: u8 = 0b10000000;
const CLEARED_PATTERN: u8 = 0b00000000;
/// Type for representing a bit.
#[derive(Debug, PartialOrd, PartialEq, Clone)]
pub struct Bit(pub u8);

/// Wrapper for `Vec<Bit>` used for implementing `From` trait.
#[derive(Debug)]
pub struct BitVec(Vec<Bit>);

impl BitVec {
    /// Constructs a BitVec of given `length` filled with `bit_val`.
    ///
    /// # Arguments
    ///
    /// * `bit_val` - value that will be set to the [Bit]
    /// * `length` - length of the output vector
    ///
    /// # Examples
    ///
    /// ## Create a [BitVec] filled with 8 zero bits
    /// ```
    /// use ptero::binary::{Bit, BitVec};
    ///
    /// let vec: Vec<Bit> = BitVec::filled_with(0, 8).into();
    /// assert_eq!(vec, &[Bit(0), Bit(0), Bit(0), Bit(0), Bit(0), Bit(0), Bit(0), Bit(0)]);    
    /// ```
    pub fn filled_with(bit_val: u8, length: usize) -> BitVec {
        vec![bit_val]
            .repeat(length)
            .iter()
            .map(|v| Bit(*v))
            .collect::<Vec<Bit>>()
            .into()
    }
}

impl From<BitVec> for Vec<Bit> {
    fn from(bit_vec: BitVec) -> Self {
        bit_vec.0
    }
}

impl From<Vec<Bit>> for BitVec {
    fn from(bit_vec: Vec<Bit>) -> Self {
        BitVec(bit_vec)
    }
}

impl From<BitVec> for u32 {
    /// Conversion implementation for `u32`.
    /// Converts array of [Bits](Bit) to the corresponding number. The function
    /// expects that the first element is the most significant bit.
    ///
    /// # Examples
    ///
    /// ## Convert 5 bits to number
    /// ```
    /// use ptero::binary::{Bit, BitVec};
    ///
    /// let array: BitVec = vec![1, 0, 1, 1, 1]
    ///                         .iter()
    ///                         .map(|v| Bit(*v))
    ///                         .collect::<Vec<Bit>>()
    ///                         .into();
    /// let number: u32 = array.into();
    /// assert_eq!(number, 23);
    /// ```   
    fn from(bit_vec: BitVec) -> Self {
        let mut number: u32 = 0;
        for bit in bit_vec.0.into_iter() {
            number <<= 1;
            number += u32::from(bit.0);
        }
        number
    }
}

impl From<u32> for BitVec {
    /// Conversion implementation for `u32`.
    /// Converts `u32` number to the vector of [Bits](Bit).
    /// The result vector has the most significant bit at the beginning.
    ///
    /// # Examples
    ///
    /// ## Convert the 65 number
    /// ```
    /// use ptero::binary::{Bit, BitVec};
    ///
    /// let array: BitVec = vec![1, 0, 1, 1, 1]
    ///                         .iter()
    ///                         .map(|v| Bit(*v))
    ///                         .collect::<Vec<Bit>>()
    ///                         .into();
    /// let number: u32 = array.into();
    /// assert_eq!(number, 23);
    /// ```   
    fn from(number: u32) -> Self {
        let byte_array = [
            ((number >> 24) & 0xff) as u8,
            ((number >> 16) & 0xff) as u8,
            ((number >> 8) & 0xff) as u8,
            (number & 0xff) as u8,
        ];
        BitIterator::new(&byte_array).collect::<Vec<Bit>>().into()
    }
}

impl TryFrom<BitVec> for Vec<u8> {
    type Error = BinaryConversionError;

    /// Tries to convert array of [Bits](Bit) to the array of bytes. The function
    /// expects that each left most bit in byte-size boundary is the
    /// most significant bit.
    ///
    /// # Arguments
    ///
    /// * `bits` - reference to array of bits `&[Bit]`
    ///
    /// # Behavior
    ///
    /// Function return [BinaryConversionError] when
    /// array is not padded to byte-size boundary i.e. length to divisible by 8.
    ///
    /// # Examples
    ///
    /// ## Convert 16 bits to 2 bytes
    /// ```
    /// use ptero::binary::{Bit, BitVec, BinaryConversionError};
    /// use std::convert::TryFrom;
    ///
    /// let array: BitVec = vec![0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1]
    ///                             .iter()
    ///                             .map(|v| Bit(*v))
    ///                             .collect::<Vec<Bit>>()
    ///                             .into();
    /// let result: Result<Vec<u8>, BinaryConversionError> = TryFrom::try_from(array);
    /// assert!(result.is_ok());
    /// assert_eq!(result.unwrap(), vec![42, 129]);
    /// ```      
    ///
    /// ## Return error if array is not in byte-size boundary
    /// ```
    /// use ptero::binary::{Bit, BinaryConversionError, BitVec};
    /// use std::convert::TryFrom;
    ///
    /// let array: BitVec = vec![0, 0, 1]
    ///                             .iter()
    ///                             .map(|v| Bit(*v))
    ///                             .collect::<Vec<Bit>>()
    ///                             .into();
    /// let result: Result<Vec<u8>, BinaryConversionError> = TryFrom::try_from(array);
    /// assert!(!result.is_ok());
    /// ```
    fn try_from(bit_vec: BitVec) -> Result<Vec<u8>, Self::Error> {
        let mut bytes = Vec::<u8>::default();
        let mut index = 0;
        if bit_vec.0.len() % 8 != 0 {
            return Err(BinaryConversionError::new(
                "Bit array length is not divisible by 8".to_string(),
            ));
        }
        while index < bit_vec.0.len() {
            let mut byte = 0;
            for _ in 0..8 {
                byte *= 2;
                byte += bit_vec.0.get(index).unwrap().0;
                index += 1;
            }
            bytes.push(byte);
        }
        Ok(bytes)
    }
}

#[cfg(not(tarpaulin_include))]
impl fmt::Display for Bit {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}
/// Error signaling binary conversion issues. Used in `From` trait implementation.
#[derive(Debug, Clone)]
pub struct BinaryConversionError {
    message: String,
}

impl BinaryConversionError {
    fn new(message: String) -> Self {
        BinaryConversionError { message }
    }
}

#[cfg(not(tarpaulin_include))]
impl fmt::Display for BinaryConversionError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Binary conversion error")
    }
}

impl Error for BinaryConversionError {}

#[derive(Debug, Copy, Clone)]
struct BinaryPattern(u8);

impl BinaryPattern {
    fn new() -> BinaryPattern {
        BinaryPattern(MOST_SIGNIFICANT_BIT_PATTERN)
    }

    fn start(&mut self) {
        self.0 = MOST_SIGNIFICANT_BIT_PATTERN;
    }

    fn is_cleared(&self) -> bool {
        self.0 == CLEARED_PATTERN
    }

    fn shift(&mut self) {
        self.0 >>= 1;
    }

    fn get(&self, byte: u8) -> Bit {
        match byte & self.0 {
            0 => Bit(0),
            _ => Bit(1),
        }
    }
}

/// [Bit] sequence iterator.
/// It enables user to read [Bits](Bit) from any iterator that provides bytes as `u8`.
#[derive(Debug, Copy, Clone)]
pub struct BitIterator<'a> {
    bytes: &'a [u8],
    index: usize,
    fetch_pattern: BinaryPattern,
}

impl<'a> BitIterator<'a> {
    /// Creates a [Bit] iterator for specified byte array.
    ///
    /// **Please note that it begins iteration from the MSB.**
    ///
    /// # Arguments
    ///
    /// * `array` - reference to array of bytes `&[u8]`
    ///
    /// # Examples
    ///
    /// ## Returns optional value
    /// ```
    /// use ptero::binary::{Bit, BitIterator};
    ///
    /// let array: Vec<u8> = vec!(1, 0, 2, 3);
    /// let mut iterator = BitIterator::new(&array);
    ///
    /// let bit = iterator.next().unwrap();
    /// let Bit(value) = bit;
    /// assert_eq!(value, 0);
    /// ```    
    ///
    /// ## Repeats itself after reaching the end
    /// ```
    /// use ptero::binary::{Bit, BitIterator};
    ///
    /// let array: Vec<u8> = vec!(0);
    /// let mut iterator = BitIterator::new(&array);
    ///
    /// for v in &mut iterator {
    ///     assert_eq!(v, Bit(0));
    /// }
    /// iterator.next();
    /// for v in &mut iterator {
    ///     assert_eq!(v, Bit(0));
    /// }
    /// ```    
    ///
    ///
    pub fn new(array: &'a [u8]) -> Self {
        // At the first execution we'll fetch the first value and then process it
        BitIterator {
            bytes: array,
            index: 0,
            fetch_pattern: BinaryPattern::new(),
        }
    }
}

impl<'a> Iterator for BitIterator<'a> {
    type Item = Bit;

    fn next(&mut self) -> Option<Self::Item> {
        if self.fetch_pattern.is_cleared() {
            self.fetch_pattern.start();
            self.index += 1;
        };
        let byte = *self.bytes.get(self.index)?;
        let bit = self.fetch_pattern.get(byte);
        self.fetch_pattern.shift();
        Some(bit)
    }
}