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
use std::fmt::{Binary, LowerHex, UpperHex, Octal};

use crate::UberByte;

impl Binary for UberByte {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!("{:b}", self.value))
    }
}

impl LowerHex for UberByte {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!("{:x}", self.value))
    }
}

impl UpperHex for UberByte {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!("{:X}", self.value))
    }
}

impl Octal for UberByte {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!("{:o}", self.value))
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn bit_formatter(){
        let my_bit = UberByte::from(0b_1001_1001);

        assert_eq!(String::from("10011001"), format!("{:b}", my_bit));
    }

    #[test]
    fn lowerhex_formatter(){
        let my_bit = UberByte::from(0b_1101_1001);

        assert_eq!(String::from("d9"), format!("{:x}", my_bit));
    }

    #[test]
    fn upperhex_formatter(){
        let my_bit = UberByte::from(0b_1101_1001);

        assert_eq!(String::from("D9"), format!("{:X}", my_bit));
    }

    #[test]
    fn octa_formatter(){
        let my_bit = UberByte::from(0b_1001_1001);

        assert_eq!(String::from("231"), format!("{:o}", my_bit));
    }
}