blob: 5cb8edefeed16b73475d54d596e74ee7d6c6f343 (
plain)
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
|
use std::ops::Deref;
#[derive(Ord, PartialOrd, PartialEq, Eq, Debug)]
pub struct Joltage {
inner: u8,
}
impl Joltage {
pub fn parse(value: u8) -> Result<Self, String> {
if value > 10 {
return Err(
format!(
"Joltage value {} is out of range (0-10)",
value
),
);
} else if value == 0 {
return Err("Joltage value cannot be zero".to_string());
}
Ok(
Joltage {
inner: value,
},
)
}
fn value(&self) -> u8 {
self.inner
}
}
impl Deref for Joltage {
type Target = u8;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl From<Joltage> for u8 {
fn from(value: Joltage) -> Self {
value.inner
}
}
|