implement From for Colour

This commit is contained in:
Max Bradbury 2020-04-12 11:31:18 +01:00
parent 7b941b5d99
commit b09b9dbece
1 changed files with 10 additions and 8 deletions

View File

@ -589,8 +589,9 @@ fn test_tile_to_string() {
assert_eq!(output, expected); assert_eq!(output, expected);
} }
fn colour_from_string(colour: String) -> Colour { impl From<String> for Colour {
let values: Vec<&str> = colour.split(',').collect(); fn from(string: String) -> Colour {
let values: Vec<&str> = string.split(',').collect();
let red: u8 = values[0].parse().unwrap_or(0); let red: u8 = values[0].parse().unwrap_or(0);
let green: u8 = values[1].parse().unwrap_or(0); let green: u8 = values[1].parse().unwrap_or(0);
@ -598,11 +599,12 @@ fn colour_from_string(colour: String) -> Colour {
Colour { red, green, blue } Colour { red, green, blue }
} }
}
#[test] #[test]
fn test_colour_from_string() { fn test_colour_from_string() {
assert_eq!( assert_eq!(
colour_from_string("0,255,0".to_string()), Colour::from("0,255,0".to_string()),
Colour { red: 0, green: 255, blue: 0 } Colour { red: 0, green: 255, blue: 0 }
); );
} }
@ -635,7 +637,7 @@ fn palette_from_string(palette: String) -> Palette {
let colour_start_index = if name.is_some() {2} else {1}; let colour_start_index = if name.is_some() {2} else {1};
let colours = lines[colour_start_index..].iter().map(|&line| { let colours = lines[colour_start_index..].iter().map(|&line| {
colour_from_string(line.to_string()) Colour::from(line.to_string())
}).collect(); }).collect();
Palette { id, name, colours } Palette { id, name, colours }