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
use crate::internal::consts;

//===========================================================================//

/// The "color" of a directory entry (which can be used for maintaining a
/// red-black tree).
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Color {
    Red,
    Black,
}

impl Color {
    pub fn as_byte(&self) -> u8 {
        match self {
            Color::Red => consts::COLOR_RED,
            Color::Black => consts::COLOR_BLACK,
        }
    }

    pub fn from_byte(byte: u8) -> Option<Color> {
        if byte == consts::COLOR_RED {
            Some(Color::Red)
        } else if byte == consts::COLOR_BLACK {
            Some(Color::Black)
        } else {
            None
        }
    }
}

//===========================================================================//

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

    #[test]
    fn round_trip() {
        for &color in &[Color::Red, Color::Black] {
            assert_eq!(Color::from_byte(color.as_byte()), Some(color));
        }
    }
}

//===========================================================================//