-
Notifications
You must be signed in to change notification settings - Fork 0
/
Resistor Color.rs
72 lines (68 loc) · 2.03 KB
/
Resistor Color.rs
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
72
#[derive(Debug, PartialEq, Eq)]
pub enum ResistorColor {
Black,
Blue,
Brown,
Green,
Grey,
Orange,
Red,
Violet,
White,
Yellow,
}
fn string_color_to_enum_color(color: &str) -> ResistorColor {
match color {
"Black" => ResistorColor::Black,
"Blue" => ResistorColor::Blue,
"Brown" => ResistorColor::Brown,
"Green" => ResistorColor::Green,
"Grey" => ResistorColor::Grey,
"Orange" => ResistorColor::Orange,
"Red" => ResistorColor::Red,
"Violet" => ResistorColor::Violet,
"White" => ResistorColor::White,
"Yellow" => ResistorColor::Yellow,
_ => panic!("Invalid value for resistor color."),
}
}
pub fn color_to_value(_color: ResistorColor) -> u32 {
let mut res = 0;
match _color {
ResistorColor::Black => res = 0,
ResistorColor::Brown => res = 1,
ResistorColor::Red => res = 2,
ResistorColor::Orange => res = 3,
ResistorColor::Yellow => res = 4,
ResistorColor::Green => res = 5,
ResistorColor::Blue => res = 6,
ResistorColor::Violet => res = 7,
ResistorColor::Grey => res = 8,
ResistorColor::White => res = 9,
};
return res;
}
pub fn value_to_color_string(value: u32) -> String {
let mut res = String::new();
match value {
0 => res = String::from("Black"),
1 => res = String::from("Brown"),
2 => res = String::from("Red"),
3 => res = String::from("Orange"),
4 => res = String::from("Yellow"),
5 => res = String::from("Green"),
6 => res = String::from("Blue"),
7 => res = String::from("Violet"),
8 => res = String::from("Grey"),
9 => res = String::from("White"),
_ => res = String::from("value out of range"),
};
return res;
}
pub fn colors() -> Vec<ResistorColor> {
let mut res: Vec<ResistorColor> = Vec::new();
for number in 0..=9 {
res.push(string_color_to_enum_color(value_to_color_string(number).as_str()));
}
return res;
}