armv6_m_instruction_parser/
conditions.rs

1#[derive(Debug, PartialEq)]
2#[repr(u8)]
3pub enum Condition {
4    EQ = 0,
5    NE = 1,
6    CS = 2,
7    CC = 3,
8    MI = 4,
9    PL = 5,
10    VS = 6,
11    VC = 7,
12    HI = 8,
13    LS = 9,
14    GE = 10,
15    LT = 11,
16    GT = 12,
17    LE = 13,
18    None = 14,
19}
20
21impl TryFrom<u8> for Condition {
22    type Error = &'static str;
23
24    fn try_from(value: u8) -> Result<Self, Self::Error> {
25        match value {
26            0 => Ok(Condition::EQ),
27            1 => Ok(Condition::NE),
28            2 => Ok(Condition::CS),
29            3 => Ok(Condition::CC),
30            4 => Ok(Condition::MI),
31            5 => Ok(Condition::PL),
32            6 => Ok(Condition::VS),
33            7 => Ok(Condition::VC),
34            8 => Ok(Condition::HI),
35            9 => Ok(Condition::LS),
36            10 => Ok(Condition::GE),
37            11 => Ok(Condition::LT),
38            12 => Ok(Condition::GT),
39            13 => Ok(Condition::LE),
40            14 => Ok(Condition::None),
41            _ => Err("Invalid condition"),
42        }
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn from_u8_to_condition() {
52        for n in 0..15 {
53            let cond: Condition = n.try_into().unwrap();
54            assert_eq!(cond as u8, n)
55        }
56
57        assert_eq!(15.try_into(), Err::<Condition, &'static str>("Invalid condition"))
58    }
59}