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
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum Color {
Black,
White,
Red,
}
#[cfg(feature = "graphics")]
extern crate embedded_graphics;
#[cfg(feature = "graphics")]
use self::embedded_graphics::pixelcolor::raw::RawU8;
#[cfg(feature = "graphics")]
use self::embedded_graphics::prelude::*;
#[cfg(feature = "graphics")]
impl PixelColor for Color {
type Raw = RawU8;
}
impl From<u8> for Color {
fn from(value: u8) -> Self {
match value {
0 => Color::Black,
1 => Color::White,
2 => Color::Red,
_ => panic!("invalid color value"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn from_u8() {
assert_eq!(Color::Black, Color::from(0u8));
assert_eq!(Color::White, Color::from(1u8));
}
#[test]
fn from_u8_panic() {
for val in 3..=u8::max_value() {
extern crate std;
let result = std::panic::catch_unwind(|| Color::from(val));
assert!(result.is_err());
}
}
}