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 { let mut chars = s.chars(); let lr = chars.next().ok_or_else(|| ParseRotationError(s.into()))?; let n: u16 = chars .collect::() .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::() .is_ok_and(|rot| rot == Rotation::Left(2))); assert!("R929" .parse::() .is_ok_and(|rot| rot == Rotation::Right(929))); assert!("M1".parse::().is_err()); assert!("L".parse::().is_err()); assert!("2".parse::().is_err()); } }