#[derive(Debug)]
#[derive(Clone, Copy)]
pub struct BCDValue {
    value: u8
}

impl BCDValue {
    pub const fn new(value: u8) -> BCDValue {
        BCDValue{value: (((value/10) << 4) | (value%10))}
    }

    pub fn get_decimal(&self) -> u8 {
        ((self.value >> 4)*10) + (self.value & 0b1111)
    }
}

impl std::convert::From<u8> for BCDValue {
    fn from(value: u8) -> Self {
        BCDValue::new(value)
    }
}

impl std::convert::From<BCDValue> for u8 {
    fn from(bcd: BCDValue) -> Self {
        bcd.get_decimal()
    }    
}

impl std::convert::From<&BCDValue> for u8 {
    fn from(bcd: &BCDValue) -> Self {
        bcd.get_decimal()
    }    
}

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

    #[test]
    fn from_one_digit() {
        assert_eq!(BCDValue::from(2).value, 0x2);
    }

    #[test]
    fn from_two_digit() {
        assert_eq!(BCDValue::from(22).value, 0x22);
    }

    #[test]
    fn one_digit_tou8() {
        assert_eq!(u8::from(BCDValue::from(2)), 2);
    }

    #[test]
    fn two_digit_tou8() {
        assert_eq!(u8::from(BCDValue::from(22)), 22);
    }
}