error handling for position
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
use std::error::Error;
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub struct Position {
|
||||
pub x: u8,
|
||||
pub y: u8,
|
||||
}
|
||||
|
||||
impl From<String> for Position {
|
||||
fn from(string: String) -> Position {
|
||||
impl Position {
|
||||
pub(crate) fn from(string: String) -> Result<Position, &'static dyn Error> {
|
||||
// e.g. "2,5"
|
||||
let xy: Vec<&str> = string.split(',').collect();
|
||||
let x = xy[0].parse().unwrap();
|
||||
@@ -16,7 +18,7 @@ impl From<String> for Position {
|
||||
|
||||
let y = xy[1].parse().unwrap();
|
||||
|
||||
Position { x, y }
|
||||
Ok(Position { x, y })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +31,7 @@ impl ToString for Position {
|
||||
|
||||
#[test]
|
||||
fn test_position_from_string() {
|
||||
assert_eq!(Position::from("4,12".to_string()), Position { x: 4, y: 12 });
|
||||
assert_eq!(Position::from("4,12".to_string()).unwrap(), Position { x: 4, y: 12 });
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user