Change Option<> in

parse_orientation()
parse_instruction()
for Result<Instruction, &'static str>
This commit is contained in:
mhart 2020-10-11 20:10:17 +02:00
parent c2d6e3422d
commit 6e1589ff7d

View File

@ -11,22 +11,22 @@ enum Instruction {
F, F,
} }
fn parse_orientation(c: char) -> Option<Orientation> { fn parse_orientation(c: char) -> Result<Orientation, &'static str> {
match c { match c {
'N' => Some(Orientation::N), 'N' => Ok(Orientation::N),
'E' => Some(Orientation::E), 'E' => Ok(Orientation::E),
'S' => Some(Orientation::S), 'S' => Ok(Orientation::S),
'W' => Some(Orientation::W), 'W' => Ok(Orientation::W),
_ => None, _ => Err("Invalid character, does not match any orientations"),
} }
} }
fn parse_instruction(c: char) -> Option<Instruction> { fn parse_instruction(c: char) -> Result<Instruction, &'static str> {
match c { match c {
'L' => Some(Instruction::L), 'L' => Ok(Instruction::L),
'R' => Some(Instruction::R), 'R' => Ok(Instruction::R),
'F' => Some(Instruction::F), 'F' => Ok(Instruction::F),
_ => None, _ => Err("Invalid character, does not match any instructions"),
} }
} }