From c954e56ea98520fcb84cbc5c6497ae37606e6cb3 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Sat, 18 Apr 2020 16:12:06 +0100 Subject: [PATCH] error handling for position --- src/avatar.rs | 2 +- src/exit.rs | 2 +- src/position.rs | 10 ++++++---- src/room.rs | 6 +++--- src/sprite.rs | 2 +- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/avatar.rs b/src/avatar.rs index a2510a6..24ebfff 100644 --- a/src/avatar.rs +++ b/src/avatar.rs @@ -42,7 +42,7 @@ impl From for Avatar { panic!("Bad room/position for avatar: {}", string); } - position = Some(Position::from(room_pos[1].to_string())); + position = Some(Position::from(room_pos[1].to_string()).unwrap()); } else if last_line.starts_with("NAME") { name = Some(last_line.replace("NAME ", "")); } else if last_line.starts_with("COL") { diff --git a/src/exit.rs b/src/exit.rs index 3e4b7e1..e8f140f 100644 --- a/src/exit.rs +++ b/src/exit.rs @@ -58,7 +58,7 @@ impl From for Exit { // e.g. "EXT 6,4 0 10,12 FX fade_w" let room_position_effect: Vec<&str> = string.split_whitespace().collect(); let room_id = from_base36(room_position_effect[0]); - let position = Position::from(room_position_effect[1].to_string()); + let position = Position::from(room_position_effect[1].to_string()).unwrap(); let effect = if room_position_effect.len() == 4 { Transition::from(room_position_effect[3]) diff --git a/src/position.rs b/src/position.rs index f34d5de..7c253f5 100644 --- a/src/position.rs +++ b/src/position.rs @@ -1,11 +1,13 @@ +use std::error::Error; + #[derive(Debug, Eq, PartialEq)] pub struct Position { pub x: u8, pub y: u8, } -impl From for Position { - fn from(string: String) -> Position { +impl Position { + pub(crate) fn from(string: String) -> Result { // e.g. "2,5" let xy: Vec<&str> = string.split(',').collect(); let x = xy[0].parse().unwrap(); @@ -16,7 +18,7 @@ impl From 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] diff --git a/src/room.rs b/src/room.rs index cd48432..c6bce84 100644 --- a/src/room.rs +++ b/src/room.rs @@ -64,13 +64,13 @@ impl From for Room { let item_position: Vec<&str> = last_line.split(' ').collect(); let item_id = item_position[0]; let position = item_position[1]; - let position = Position::from(position.to_string()); + let position = Position::from(position.to_string()).unwrap(); items.push(Instance { position, id: item_id.to_string() }); } else if last_line.starts_with("EXT") { let last_line = last_line.replace("EXT ", ""); let parts: Vec<&str> = last_line.split(' ').collect(); - let position = Position::from(parts[0].to_string()); + let position = Position::from(parts[0].to_string()).unwrap(); let exit = Exit::from(format!("{} {}", parts[1], parts[2])); exits.push(ExitInstance { position, exit }); @@ -79,7 +79,7 @@ impl From for Room { let ending_position: Vec<&str> = last_line.split(' ').collect(); let ending = ending_position[0].to_string(); let position = ending_position[1].to_string(); - let position = Position::from(position); + let position = Position::from(position).unwrap(); endings.push(Instance { position, id: ending }); } else { diff --git a/src/sprite.rs b/src/sprite.rs index 53aa36f..f1f02c8 100644 --- a/src/sprite.rs +++ b/src/sprite.rs @@ -60,7 +60,7 @@ impl From for Sprite { panic!("Bad room/position for sprite: {}", string); } - position = Some(Position::from(room_position[1].to_string())); + position = Some(Position::from(room_position[1].to_string()).unwrap()); } else if last_line.starts_with("COL") { colour_id = Some(last_line.replace("COL ", "").parse().unwrap()); } else {