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
use crate::*;
use bitvec::prelude::*;

pub trait IeeeBinary {
    fn ieee_binary32() -> Self;
    fn ieee_binary64() -> Self;
}

pub type BitPattern = BitVec<usize, Msb0>;

pub trait BitPatternExt where Self: Sized {
    fn from_value<T>(val: T) -> Self
    where
        T: BitStore;

    fn from_str(s: &str) -> Result<Self, error::Error>;
    fn from_bin_str(s: &str) -> Result<Self, error::Error>;
    fn from_oct_str(s: &str) -> Result<Self, error::Error>;
    fn from_hex_str(s: &str) -> Result<Self, error::Error>;

    fn to_bin_string(&self) -> String;
    fn into_bin_string(self) -> String;
}

impl BitPatternExt for BitPattern {
    fn from_value<T>(val: T) -> Self
    where
        T: BitStore
    {
        val.view_bits::<Msb0>().iter().collect()
    }

    fn from_str(s: &str) -> Result<Self, error::Error> {
        if s.len() < 3 {
            return Err(error::Error::InvalidRadixPrefix);
        }

        match s[0..2].as_ref() {
            "0b" => Ok(Self::from_bin_str(&s[2..])?),
            "0o" => Ok(Self::from_oct_str(&s[2..])?),
            "0x" => Ok(Self::from_hex_str(&s[2..])?),
            _ => Err(error::Error::InvalidRadixPrefix),
        }
    }
    
    fn from_bin_str(s: &str) -> Result<Self, error::Error> {
        Ok(s
            .chars()
            .filter(|c| c.is_digit(2))
            .map(|c| c == '1')
            .collect()
        )
    }

    fn from_oct_str(s: &str) -> Result<Self, error::Error> {
        Ok(s
            .chars()
            .filter(|c| c.is_digit(8))
            .flat_map(|c| format!("{:3b}", c.to_digit(8).unwrap())
                .chars()
                .collect::<Vec<_>>()
            )
            .map(|c| c == '1')
            .collect()
        )
    }
    
    fn from_hex_str(s: &str) -> Result<Self, error::Error> {
        Ok(s
            .chars()
            .filter(|c| c.is_digit(16))
            .flat_map(|c| format!("{:4b}", c.to_digit(16).unwrap())
                .chars()
                .collect::<Vec<_>>()
            )
            .map(|c| c == '1')
            .collect()
        )
    }
    
    fn to_bin_string(&self) -> String {
        self
            .iter()
            .map(|b| if b == true { '1' } else { '0' })
            .collect()
    }
    
    fn into_bin_string(self) -> String {
        self
            .into_iter()
            .map(|b| if b == true { '1' } else { '0' })
            .collect()
    }
}