error handling for position
This commit is contained in:
parent
78b2319d4f
commit
c954e56ea9
|
@ -42,7 +42,7 @@ impl From<String> 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") {
|
||||
|
|
|
@ -58,7 +58,7 @@ impl From<String> 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])
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -64,13 +64,13 @@ impl From<String> 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<String> 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 {
|
||||
|
|
|
@ -60,7 +60,7 @@ impl From<String> 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 {
|
||||
|
|
Loading…
Reference in New Issue