blob: 778fc6d0d3314d67501b0dc6d9f3c78edd86f05d (
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
45
46
47
48
49
50
51
52
|
use std::str::FromStr;
#[derive(Debug, PartialEq, Eq)]
enum Rotation {
Left(u16),
Right(u16),
}
struct ParseRotationError(String);
impl FromStr for Rotation {
type Err = ParseRotationError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut chars = s.chars();
let lr = chars.next().ok_or_else(|| ParseRotationError(s.into()))?;
let n: u16 = chars
.collect::<String>()
.parse()
.map_err(|_| ParseRotationError(s.into()))?;
match lr {
'L' => Ok(Self::Left(n)),
'R' => Ok(Self::Right(n)),
_ => Err(ParseRotationError(s.into())),
}
}
}
fn main() {
println!("Hello, world!");
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_rotation() {
assert!("L2"
.parse::<Rotation>()
.is_ok_and(|rot| rot == Rotation::Left(2)));
assert!("R929"
.parse::<Rotation>()
.is_ok_and(|rot| rot == Rotation::Right(929)));
assert!("M1".parse::<Rotation>().is_err());
assert!("L".parse::<Rotation>().is_err());
assert!("2".parse::<Rotation>().is_err());
}
}
|