re-implement _to_string functions as ToString

This commit is contained in:
Max Bradbury 2020-04-12 11:22:14 +01:00
parent 3d1beca268
commit 2ec01ebfce
1 changed files with 178 additions and 159 deletions

View File

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