From 85ac16d5a06a615b113a6c89087a24bb38d53e8e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gr=C3=A9gory=20Burri?= Date: Fri, 20 Dec 2019 10:05:57 +0100 Subject: [PATCH] Use a common trait (TryFrom) --- src/day17.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/day17.rs b/src/day17.rs index c64ed0e..f120506 100644 --- a/src/day17.rs +++ b/src/day17.rs @@ -1,17 +1,20 @@ use super::intcode; use std::collections::HashSet; +use std::convert::TryFrom; #[derive(PartialEq, Eq, Copy, Clone, Debug)] enum Direction { Up, Left, Down, Right } -impl Direction { - fn from_char(c: char) -> Option { +impl TryFrom for Direction { + type Error = (); + + fn try_from(c: char) -> Result { match c { - '^' => Some(Direction::Up), - '<' => Some(Direction::Left), - 'v' => Some(Direction::Down), - '>' => Some(Direction::Right), - _ => None + '^' => Ok(Direction::Up), + '<' => Ok(Direction::Left), + 'v' => Ok(Direction::Down), + '>' => Ok(Direction::Right), + _ => Err(()) } } } @@ -66,7 +69,7 @@ impl RobotTrackingSystem { current_x = 0; } else { let c = (*c as u8) as char; - if let Some(dir) = Direction::from_char(c) { + if let Ok(dir) = Direction::try_from(c) { self.start_position = (current_x, self.board.len() as i32); self.start_dir = dir } -- 2.45.2