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
use num_enum::{IntoPrimitive, TryFromPrimitive};

#[derive(Debug, Eq, PartialEq, TryFromPrimitive, IntoPrimitive)]
#[repr(u8)]
pub enum Table4_4 {
    Minute = 0,
    Hour,
    Day,
    Month,
    Year,
    Decade,
    Normal,
    Century,
    ThreeHours = 10,
    SixHours,
    TwelveHours,
    Second,
    Missing = 255,
}

impl Table4_4 {
    pub fn short_expr(&self) -> Option<&'static str> {
        match self {
            Self::Minute => Some("m"),
            Self::Hour => Some("h"),
            Self::Day => Some("D"),
            Self::Month => Some("M"),
            Self::Year => Some("Y"),
            Self::Decade => Some("10Y"),
            Self::Normal => Some("30Y"),
            Self::Century => Some("C"),
            Self::ThreeHours => Some("3h"),
            Self::SixHours => Some("6h"),
            Self::TwelveHours => Some("12h"),
            Self::Second => Some("s"),
            Self::Missing => None,
        }
    }
}

#[derive(Debug, Clone, Eq, PartialEq, TryFromPrimitive, IntoPrimitive)]
#[repr(u8)]
pub enum Table5_6 {
    FirstOrderSpatialDifferencing = 1,
    SecondOrderSpatialDifferencing,
    Missing = 255,
}

#[cfg(test)]
mod tests {
    use num_enum::TryFromPrimitiveError;

    use super::*;
    use crate::codetables::*;

    #[test]
    fn num_enum_conversion() {
        assert_eq!((Table4_4::try_from(1u8)), Ok(Table4_4::Hour));
        assert_eq!((Table4_4::try_from(10u8)), Ok(Table4_4::ThreeHours));
        assert_eq!(
            (Table4_4::try_from(254u8)),
            Err(TryFromPrimitiveError { number: 254 })
        );
    }

    #[test]
    fn num_lookup_result_conversion() {
        assert_eq!(Code::from(Table4_4::try_from(1u8)), Name(Table4_4::Hour));
        assert_eq!(Code::from(Table4_4::try_from(254u8)), Num(254));
    }
}