diff --git a/src/main.rs b/src/main.rs index e6b9e69..01d087d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -484,19 +484,26 @@ fn test_image_to_string() { assert_eq!(output, expected); } -fn animation_frames_to_string(animation_frames: Vec) -> String { - let mut string = String::new(); - let last_frame = animation_frames.len() - 1; +pub trait AnimationFrames { + fn to_string(&self) -> String; +} - for (i, frame) in animation_frames.into_iter().enumerate() { - string.push_str(&frame.to_string()); +impl AnimationFrames for Vec { + #[inline] + fn to_string(&self) -> String { + let mut string = String::new(); + let last_frame = self.len() - 1; - if i < last_frame { - string.push_str(&"\n>\n".to_string()); + for (i, frame) in self.into_iter().enumerate() { + string.push_str(&frame.to_string()); + + if i < last_frame { + string.push_str(&"\n>\n".to_string()); + } } - } - string + string + } } fn tile_from_string(string: String) -> Tile { @@ -551,19 +558,22 @@ fn test_tile_from_string() { assert_eq!(output, expected); } -fn tile_to_string(tile: Tile) -> String { - format!( - "TIL {}\n{}{}{}", - tile.id, - animation_frames_to_string(tile.animation_frames), - if tile.name.is_some() {format!("\nNAME {}", tile.name.unwrap())} else {"".to_string()}, - if tile.wall {"\nWAL true"} else {""} - ) +impl ToString for Tile { + #[inline] + fn to_string(&self) -> String { + format!( + "TIL {}\n{}{}{}", + self.id, + self.animation_frames.to_string(), + if self.name.as_ref().is_some() { format!("\nNAME {}", self.name.as_ref().unwrap())} else {"".to_string() }, + if self.wall {"\nWAL true"} else {""} + ) + } } #[test] fn test_tile_to_string() { - let output = tile_to_string(Tile { + let output = Tile { id: "7a".to_string(), name: Some("chequers".to_string()), wall: false, @@ -571,7 +581,7 @@ fn test_tile_to_string() { example_image_chequers_1(), example_image_chequers_2(), ] - }); + }.to_string(); let expected = include_str!("../test/resources/tile-chequers").to_string(); @@ -756,20 +766,21 @@ fn test_avatar_from_string() { assert_eq!(output, expected); } -fn avatar_to_string(avatar: Avatar) -> String { - format!( - "SPR A\n{}\nPOS {} {}", - animation_frames_to_string(avatar.animation_frames), - avatar.room, - avatar.position.to_string() - ) +impl ToString for Avatar { + #[inline] + fn to_string(&self) -> String { + format!( + "SPR A\n{}\nPOS {} {}", + self.animation_frames.to_string(), + self.room, + self.position.to_string() + ) + } } #[test] fn test_avatar_to_string() { - let output = avatar_to_string(example_avatar()); - let expected = include_str!("../test/resources/avatar"); - assert_eq!(output, expected); + assert_eq!(example_avatar().to_string(), include_str!("../test/resources/avatar")); } fn sprite_from_string(string: String) -> Sprite { @@ -822,24 +833,24 @@ fn test_sprite_from_string() { assert_eq!(output, expected); } -fn sprite_to_string(sprite: Sprite) -> String { - format!( - "SPR {}\n{}{}{}\nPOS {} {}", - sprite.id, - animation_frames_to_string(sprite.animation_frames), - if sprite.name.is_some() {format!("\nNAME {}", sprite.name.unwrap())} else {"".to_string()}, - if sprite.dialogue.is_some() {format!("\nDLG {}", sprite.dialogue.unwrap())} else {"".to_string()}, - sprite.room, - sprite.position.to_string(), - ) +impl ToString for Sprite { + #[inline] + fn to_string(&self) -> String { + format!( + "SPR {}\n{}{}{}\nPOS {} {}", + self.id, + self.animation_frames.to_string(), + if self.name.as_ref().is_some() { format!("\nNAME {}", self.name.as_ref().unwrap()) } else { "".to_string() }, + if self.dialogue.as_ref().is_some() { format!("\nDLG {}", self.dialogue.as_ref().unwrap()) } else { "".to_string() }, + self.room, + self.position.to_string(), + ) + } } #[test] fn test_sprite_to_string() { - let output = sprite_to_string(example_sprite()); - let expected = include_str!("../test/resources/sprite").to_string(); - - assert_eq!(output, expected); + assert_eq!(example_sprite().to_string(), include_str!("../test/resources/sprite").to_string()); } fn item_from_string(string: String) -> Item { @@ -882,21 +893,23 @@ fn test_item_from_string() { assert_eq!(output, expected); } -fn item_to_string(item: Item) -> String { - format!( - "ITM {}\n{}{}{}", - item.id, - animation_frames_to_string(item.animation_frames), - if item.name.is_some() {format!("\nNAME {}", item.name.unwrap())} else {"".to_string()}, - if item.dialogue.is_some() {format!("\nDLG {}", item.dialogue.unwrap())} else {"".to_string()}, - ) +impl ToString for Item { + #[inline] + fn to_string(&self) -> String { + format!( + "ITM {}\n{}{}{}", + self.id, + self.animation_frames.to_string(), + if self.name.is_some() { format!("\nNAME {}", self.name.as_ref().unwrap()) } else { "".to_string() }, + if self.dialogue.is_some() { format!("\nDLG {}", self.dialogue.as_ref().unwrap()) } else { "".to_string() }, + ) + } } #[test] fn test_item_to_string() { - let output = item_to_string(example_item()); + let output = example_item().to_string(); let expected = include_str!("../test/resources/item").to_string(); - assert_eq!(output, expected); } @@ -917,14 +930,16 @@ fn test_exit_from_string() { ); } -fn exit_to_string(exit: Exit) -> String { - format!("{} {}", exit.room, exit.position.to_string()) +impl ToString for Exit { + fn to_string(&self) -> String { + format!("{} {}", self.room, self.position.to_string()) + } } #[test] fn test_exit_to_string() { assert_eq!( - exit_to_string(Exit { room: "8".to_string(), position: Position { x: 5, y: 6 } }), + Exit { room: "8".to_string(), position: Position { x: 5, y: 6 } }.to_string(), "8 5,6".to_string() ); } @@ -949,19 +964,20 @@ fn test_ending_from_string() { ); } -fn ending_to_string(ending: Ending) -> String { - format!("END {}\n{}", ending.id, ending.dialogue) +impl ToString for Ending { + #[inline] + fn to_string(&self) -> String { + format!("END {}\n{}", self.id, self.dialogue) + } } #[test] fn test_ending_to_string() { assert_eq!( - ending_to_string( - Ending { - id: "7".to_string(), - dialogue: "This is another long ending. So long, farewell, etc.".to_string() - } - ), + Ending { + id: "7".to_string(), + dialogue: "This is another long ending. So long, farewell, etc.".to_string() + }.to_string(), "END 7\nThis is another long ending. So long, farewell, etc.".to_string() ); } @@ -1016,17 +1032,17 @@ fn test_variable_from_string() { ); } -fn variable_to_string(variable: Variable) -> String { - format!("VAR {}\n{}", variable.id, variable.initial_value) +impl ToString for Variable { + #[inline] + fn to_string(&self) -> String { + format!("VAR {}\n{}", self.id, self.initial_value) + } } #[test] fn test_variable_to_string() { - let output = variable_to_string( - Variable { id: "c".to_string(), initial_value: "57".to_string() } - ); + let output = Variable { id: "c".to_string(), initial_value: "57".to_string() }.to_string(); let expected = "VAR c\n57".to_string(); - assert_eq!(output, expected); } @@ -1098,59 +1114,61 @@ fn test_room_from_string() { assert_eq!(output, expected); } -fn room_to_string(room: Room) -> String { - let mut tiles = String::new(); - let mut items = String::new(); - let mut exits = String::new(); - let mut endings = String::new(); +impl ToString for Room { + fn to_string(&self) -> String { + let mut tiles = String::new(); + let mut items = String::new(); + let mut exits = String::new(); + let mut endings = String::new(); - let sqrt = (room.tiles.len() as f64).sqrt() as usize; // 8 for SD, 16 for HD - for line in room.tiles.chunks(sqrt) { - for tile in line { - tiles.push_str(&format!("{},", tile)); + let sqrt = (self.tiles.len() as f64).sqrt() as usize; // 8 for SD, 16 for HD + for line in self.tiles.chunks(sqrt) { + for tile in line { + tiles.push_str(&format!("{},", tile)); + } + tiles.pop(); // remove trailing comma + tiles.push_str("\n"); } - tiles.pop(); // remove trailing comma - tiles.push_str("\n"); - } - tiles.pop(); // remove trailing newline + tiles.pop(); // remove trailing newline - for instance in room.items { - items.push_str( - &format!("\nITM {} {}", instance.id, instance.position.to_string()) - ); - } + for instance in &self.items { + items.push_str( + &format!("\nITM {} {}", instance.id, instance.position.to_string()) + ); + } - for instance in room.exits { - exits.push_str( - &format!( - "\nEXT {} {}", - instance.position.to_string(), - exit_to_string(instance.exit) - ) - ); - } + for instance in &self.exits { + exits.push_str( + &format!( + "\nEXT {} {}", + instance.position.to_string(), + instance.exit.to_string(), + ) + ); + } - for instance in room.endings { - endings.push_str( - &format!("\nEND {} {}", instance.id, instance.position.to_string()) - ); - } + for instance in &self.endings { + endings.push_str( + &format!("\nEND {} {}", instance.id, instance.position.to_string()) + ); + } - format!( - "ROOM {}\n{}{}{}{}{}\nPAL {}", - room.id, - tiles, - if room.name.is_some() {format!("\nNAME {}", room.name.unwrap())} else {"".to_string()}, - items, - exits, - endings, - room.palette - ) + format!( + "ROOM {}\n{}{}{}{}{}\nPAL {}", + self.id, + tiles, + if self.name.as_ref().is_some() { format!("\nNAME {}", self.name.as_ref().unwrap()) } else { "".to_string() }, + items, + exits, + endings, + self.palette + ) + } } #[test] fn test_room_to_string() { - assert_eq!(room_to_string(example_room()), example_room_string()); + assert_eq!(example_room().to_string(), example_room_string()); } fn game_from_string(string: String ) -> Game { @@ -1162,7 +1180,6 @@ fn game_from_string(string: String ) -> Game { let main_split: Vec<&str> = string.split("\n\nDLG").collect(); let main = main_split[0].to_string(); let mut dialogues_endings_variables: String = main_split[1..].join("\n\nDLG"); - // todo handle dialogues_endings_variables let variable_segments = dialogues_endings_variables.clone(); let variable_segments: Vec<&str> = variable_segments.split("\n\nVAR").collect(); @@ -1188,7 +1205,6 @@ fn game_from_string(string: String ) -> Game { } } - let dialogue_segments = format!("\n\nDLG {}", dialogues_endings_variables.trim()); let dialogue_segments: Vec<&str> = dialogue_segments.split("\n\nDLG").collect(); for segment in dialogue_segments[1..].to_owned() { @@ -1259,57 +1275,60 @@ fn test_game_from_string() { assert_eq!(output, expected); } -fn game_to_string(game: Game) -> String { - let mut segments : Vec = Vec::new(); +impl ToString for Game { + #[inline] + fn to_string(&self) -> String { + let mut segments: Vec = Vec::new(); - // todo refactor + // todo refactor - for palette in game.palettes { - segments.push(palette.to_string()); + for palette in &self.palettes { + segments.push(palette.to_string()); + } + + for room in &self.rooms { + segments.push(room.to_string()); + } + + for tile in &self.tiles { + segments.push(tile.to_string()); + } + + segments.push(self.avatar.to_string()); + + for sprite in &self.sprites { + segments.push(sprite.to_string()); + } + + for item in &self.items { + segments.push(item.to_string()); + } + + for dialogue in &self.dialogues { + segments.push(dialogue.to_string()); + } + + for ending in &self.endings { + segments.push(ending.to_string()); + } + + for variable in &self.variables { + segments.push(variable.to_string()); + } + + format!( + "{}\n\n# BITSY VERSION {}\n\n! ROOM_FORMAT {}\n\n{}\n\n", + &self.name, + &self.version, + &self.room_format, + segments.join("\n\n"), + ) } - - for room in game.rooms { - segments.push(room_to_string(room)); - } - - for tile in game.tiles { - segments.push(tile_to_string(tile)); - } - - segments.push(avatar_to_string(game.avatar)); - - for sprite in game.sprites { - segments.push(sprite_to_string(sprite)); - } - - for item in game.items { - segments.push(item_to_string(item)); - } - - for dialogue in game.dialogues { - segments.push(dialogue.to_string()); - } - - for ending in game.endings { - segments.push(ending_to_string(ending)); - } - - for variable in game.variables { - segments.push(variable_to_string(variable)); - } - - format!( - "{}\n\n# BITSY VERSION {}\n\n! ROOM_FORMAT {}\n\n{}\n\n", - game.name, - game.version, - game.room_format, - segments.join("\n\n"), - ) } #[test] fn test_game_to_string() { - let output = game_to_string(example_game_default()); + let output = example_game_default().to_string(); let expected = include_str!["../test/resources/default.bitsy"].to_string(); assert_eq!(output, expected); }