bitsy-parser/src/exit.rs

41 lines
975 B
Rust

use crate::Position;
#[derive(Debug, Eq, PartialEq)]
pub struct Exit {
/// destination
pub(crate) room: String, /// id
pub(crate) position: Position,
}
impl From<String> for Exit {
fn from(string: String) -> Exit {
// e.g. "4 3,3"
let room_position: Vec<&str> = string.split(' ').collect();
let room = room_position[0].to_string();
let position = Position::from(room_position[1].to_string());
Exit { room, position }
}
}
impl ToString for Exit {
fn to_string(&self) -> String {
format!("{} {}", self.room, self.position.to_string())
}
}
#[test]
fn test_exit_from_string() {
assert_eq!(
Exit::from("a 12,13".to_string()),
Exit { room: "a".to_string(), position: Position { x: 12, y: 13 } }
);
}
#[test]
fn test_exit_to_string() {
assert_eq!(
Exit { room: "8".to_string(), position: Position { x: 5, y: 6 } }.to_string(),
"8 5,6".to_string()
);
}