implement ToString for Palette

This commit is contained in:
Max Bradbury 2020-04-12 10:15:51 +01:00
parent 1221df2634
commit a58b0ae877
1 changed files with 18 additions and 14 deletions

View File

@ -666,24 +666,28 @@ fn test_palette_from_string_no_name() {
assert_eq!(output, expected); assert_eq!(output, expected);
} }
fn palette_to_string(palette: Palette) -> String { impl ToString for Palette {
let name = if palette.name.is_some() { #[inline]
format!("NAME {}\n", palette.name.unwrap()) fn to_string(&self) -> String {
let name = if self.name.as_ref().is_some() {
format!("NAME {}\n", self.name.as_ref().unwrap())
} else { } else {
"".to_string() "".to_string()
}; };
let colours: Vec<String> = palette.colours.into_iter().map(|colour| let mut colours = String::new();
colour.to_string() for colour in &self.colours {
).collect(); colours.push_str(&format!("{}\n", colour.to_string()));
let colours = colours.join("\n"); }
colours.pop();
format!("PAL {}\n{}{}", palette.id, name, colours) format!("PAL {}\n{}{}", self.id, name, colours)
}
} }
#[test] #[test]
fn test_palette_to_string() { fn test_palette_to_string() {
let output = palette_to_string(Palette { let output = Palette {
id: "g".to_string(), id: "g".to_string(),
name: Some("moss".to_string()), name: Some("moss".to_string()),
colours: vec![ colours: vec![
@ -691,7 +695,7 @@ fn test_palette_to_string() {
Colour {red: 255, green: 254, blue: 253}, Colour {red: 255, green: 254, blue: 253},
Colour {red: 126, green: 127, blue: 128}, Colour {red: 126, green: 127, blue: 128},
] ]
}); }.to_string();
let expected = "PAL g\nNAME moss\n1,2,3\n255,254,253\n126,127,128".to_string(); let expected = "PAL g\nNAME moss\n1,2,3\n255,254,253\n126,127,128".to_string();
assert_eq!(output, expected); assert_eq!(output, expected);
} }
@ -1258,7 +1262,7 @@ fn game_to_string(game: Game) -> String {
// todo refactor // todo refactor
for palette in game.palettes { for palette in game.palettes {
segments.push(palette_to_string(palette)); segments.push(palette.to_string());
} }
for room in game.rooms { for room in game.rooms {