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

#[derive(Debug)]
pub struct BCDConversionError {
    description: String
}

impl BCDConversionError {
    pub fn new(description: String) -> Self {
        Self { description }
    }

    pub fn new_boxed(description: String) -> Box<Self> {
        Box::new(Self::new(description))
    }

    pub fn new_with_template_description<T>(t: &str, v: T, max: &str) -> Self 
        where T: std::fmt::Display
    {
        Self::new(format!("Error on {} to bcd, passed in value ({}) exceeds maximum of {}", t, v,max))
    }

    pub fn description<'self_lt>(&'self_lt self) -> &'self_lt str {
        &self.description
    }
}

impl std::fmt::Display for BCDConversionError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl std::error::Error for BCDConversionError {}

pub trait ToBCD : Sized {
    fn to_bcd(self) -> Result<Self, BCDConversionError>;
}

pub trait FromBCD : Sized {
    fn from_bcd(self) -> Self;
}

impl ToBCD for u8 {
    fn to_bcd(self) -> Result<Self, BCDConversionError> {
        if self > 99 { return Err(BCDConversionError::new_with_template_description("u8", self, "99")); }
        let d1 = self%10;
        let d2 = self/10;
        Ok((d2 << 4) | d1)
    }
}

impl FromBCD for u8 {
    fn from_bcd(self) -> Self {
        let d1 = self&0x0f;
        let d2 = (self&0xf0) >> 4;
        d2*10 + d1
    }
}

impl ToBCD for u16 {
    fn to_bcd(self) -> Result<Self, BCDConversionError> {
        if self > 9999 { return Err(BCDConversionError::new_with_template_description("u16", self, "9999")); }
        let mut value = self;
        let mut result = 0;
        let mut shift = 0;
        while value != 0 {
            let lower = (value%100) as u8;
            let lower = lower.to_bcd().unwrap() as Self;
            result |= lower << shift;
            value /= 100;
            shift += 8;
        }
        Ok(result)
    }
}

impl FromBCD for u16 {
    fn from_bcd(self) -> Self {
        let mut value = self;
        let mut result = 0;
        let mut digit_base = 1;
        while value != 0 {
            let lower = (value & 0xff) as u8;
            let lower = lower.from_bcd() as Self;
            result += lower*digit_base;
            value >>= 8;
            digit_base *= 100;
        }
        result
    }
}

impl ToBCD for u32 {
    fn to_bcd(self) -> Result<Self, BCDConversionError> {
        if self > 99999999 { return Err(BCDConversionError::new_with_template_description("u32", self, "99999999")); }
        let mut value = self;
        let mut result = 0;
        let mut shift = 0;
        while value != 0 {
            let lower = (value%100) as u8;
            let lower = lower.to_bcd().unwrap() as Self;
            result |= lower << shift;
            value /= 100;
            shift += 8;
        }
        Ok(result)
    }
}

impl FromBCD for u32 {
    fn from_bcd(self) -> Self {
        let mut value = self;
        let mut result = 0;
        let mut digit_base = 1;
        while value != 0 {
            let lower = (value & 0xff) as u8;
            let lower = lower.from_bcd() as Self;
            result += lower*digit_base;
            value >>= 8;
            digit_base *= 100;
        }
        result
    }
}

impl ToBCD for u64 {
    fn to_bcd(self) -> Result<Self, BCDConversionError> {
        if self > 9999999999999999 { return Err(BCDConversionError::new_with_template_description("u64", self, "9999999999999999")); }
        let mut value = self;
        let mut result = 0;
        let mut shift = 0;
        while value != 0 {
            let lower = (value%100) as u8;
            let lower = lower.to_bcd().unwrap() as Self;
            result |= lower << shift;
            value /= 100;
            shift += 8;
        }
        Ok(result)
    }
}

impl FromBCD for u64 {
    fn from_bcd(self) -> Self {
        let mut value = self;
        let mut result = 0;
        let mut digit_base = 1;
        while value != 0 {
            let lower = (value & 0xff) as u8;
            let lower = lower.from_bcd() as Self;
            result += lower*digit_base;
            value >>= 8;
            digit_base *= 100;
        }
        result
    }
}

impl ToBCD for u128 {
    fn to_bcd(self) -> Result<Self, BCDConversionError> {
        if self > 99999999999999999999999999999999 { return Err(BCDConversionError::new_with_template_description("u128", self, "99999999999999999999999999999999")); }
        let mut value = self;
        let mut result = 0;
        let mut shift = 0;
        while value != 0 {
            let lower = (value%100) as u8;
            let lower = lower.to_bcd().unwrap() as Self;
            result |= lower << shift;
            value /= 100;
            shift += 8;
        }
        Ok(result)
    }
}

impl FromBCD for u128 {
    fn from_bcd(self) -> Self {
        let mut value = self;
        let mut result = 0;
        let mut digit_base = 1;
        while value != 0 {
            let lower = (value & 0xff) as u8;
            let lower = lower.from_bcd() as Self;
            result += lower*digit_base;
            value >>= 8;
            digit_base *= 100;
        }
        result
    }
}