arr_rs/numeric/types/
binary.rs

1use crate::errors::prelude::*;
2
3/// the order of the bits packed/unpacked
4#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
5pub enum BitOrder {
6    /// standard binary representation
7    Big,
8    /// reversed order
9    Little,
10}
11
12/// `BitOrder` trait
13pub trait BitOrderType {
14
15    /// Parse input to `BitOrder` type
16    ///
17    /// # Errors
18    ///
19    /// may returns `ArrayError`
20    fn to_bit_order(self) -> Result<BitOrder, ArrayError>;
21}
22
23impl BitOrderType for BitOrder {
24
25    fn to_bit_order(self) -> Result<BitOrder, ArrayError> {
26        Ok(self)
27    }
28}
29
30impl BitOrderType for &str {
31
32    fn to_bit_order(self) -> Result<BitOrder, ArrayError> {
33        match self {
34            "big" => Ok(BitOrder::Big),
35            "little" => Ok(BitOrder::Little),
36            _ => Err(ArrayError::ParameterError { param: "`bit_order`", message: "must be one of {`big`, `little`}" })
37        }
38    }
39}
40
41impl BitOrderType for String {
42
43    fn to_bit_order(self) -> Result<BitOrder, ArrayError> {
44        match self.as_str() {
45            "big" => Ok(BitOrder::Big),
46            "little" => Ok(BitOrder::Little),
47            _ => Err(ArrayError::ParameterError { param: "`bit_order`", message: "must be one of {`big`, `little`}" })
48        }
49    }
50}