diff --git a/src/position.rs b/src/position.rs index 1e496a0..9db6dac 100644 --- a/src/position.rs +++ b/src/position.rs @@ -15,17 +15,14 @@ impl FromStr for Position { fn from_str(s: &str) -> Result { let mut parts = s.split(','); - let x = parts.next().unwrap().parse(); - let y = parts.next().unwrap().parse(); - if x.is_err() { - Err("bad x supplied for position".to_string()) - } else if y.is_err() { - Err("bad y supplied for position".to_string()) - } else { - let x = x.unwrap(); - let y = y.unwrap(); + let x = parts.next().unwrap(); + let y = parts.next().unwrap(); + + if let (Ok(x), Ok(y)) = (x.parse(), y.parse()) { Ok(Position { x, y }) + } else { + Err(format!("Malformed position string: {}", s)) } } } @@ -44,11 +41,16 @@ mod test { #[test] fn position_from_str() { assert_eq!( - Position::from_str(&"4,12").unwrap(), + Position::from_str("4,12").unwrap(), Position { x: 4, y: 12 } ); } + #[test] + fn position_from_malformed_str() { + assert!(Position::from_str("14,-1").is_err()) + } + #[test] fn position_to_string() { assert_eq!(Position { x: 4, y: 12 }.to_string(), "4,12".to_string())