From fb290f07f4b8ad3b02a7b4e4cde78f58e1731631 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Sun, 18 Oct 2020 15:59:56 +0100 Subject: [PATCH] make walls vec optional --- src/mock.rs | 4 ++-- src/room.rs | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/mock.rs b/src/mock.rs index 08d0125..d86a5e9 100644 --- a/src/mock.rs +++ b/src/mock.rs @@ -529,7 +529,7 @@ pub fn room() -> Room { position: Position { x: 8, y: 7 }, id: "undefined".to_string(), }], - walls: vec![], + walls: None, } } @@ -828,7 +828,7 @@ pub fn game_default() -> Game { items: vec![], exits: vec![], endings: vec![], - walls: vec![], + walls: None, }], tiles: vec![self::tile_default()], sprites: vec![ diff --git a/src/room.rs b/src/room.rs index 6afee5b..a2e1943 100644 --- a/src/room.rs +++ b/src/room.rs @@ -23,7 +23,7 @@ pub struct Room { pub exits: Vec, pub endings: Vec, /// old method of handling walls - a comma-separated list of tile IDs - pub walls: Vec, + pub walls: Option>, } impl Room { @@ -32,10 +32,10 @@ impl Room { } fn wall_line(&self) -> String { - if self.walls.is_empty() { - "".to_string() + if let Some(walls) = &self.walls { + optional_data_line("WAL", Some(walls.join(","))) } else { - optional_data_line("WAL", Some(self.walls.join(","))) + "".to_string() } } @@ -58,7 +58,7 @@ impl From for Room { let mut items: Vec = Vec::new(); let mut exits: Vec = Vec::new(); let mut endings: Vec = Vec::new(); - let mut walls: Vec = Vec::new(); + let mut walls = None; loop { let last_line = lines.pop().unwrap(); @@ -66,7 +66,7 @@ impl From for Room { if last_line.starts_with("WAL") { let last_line = last_line.replace("WAL ", ""); let ids: Vec<&str> = last_line.split(',').collect(); - walls = ids.iter().map(|&id| id.to_string()).collect(); + walls = Some(ids.iter().map(|&id| id.to_string()).collect()); } else if last_line.starts_with("NAME") { name = Some(last_line.replace("NAME ", "").to_string()); } else if last_line.starts_with("PAL") { @@ -258,6 +258,6 @@ mod test { fn room_walls_array() { let output = Room::from(include_str!("test-resources/room-with-walls").to_string()); - assert_eq!(output.walls, vec!["a".to_string(), "f".to_string()]); + assert_eq!(output.walls, Some(vec!["a".to_string(), "f".to_string()])); } }