diff --git a/src/main.rs b/src/main.rs index 3babe5c..72d63a6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -589,20 +589,22 @@ fn test_tile_to_string() { assert_eq!(output, expected); } -fn colour_from_string(colour: String) -> Colour { - let values: Vec<&str> = colour.split(',').collect(); +impl From for Colour { + fn from(string: String) -> Colour { + let values: Vec<&str> = string.split(',').collect(); - let red: u8 = values[0].parse().unwrap_or(0); - let green: u8 = values[1].parse().unwrap_or(0); - let blue: u8 = values[2].parse().unwrap_or(0); + let red: u8 = values[0].parse().unwrap_or(0); + let green: u8 = values[1].parse().unwrap_or(0); + let blue: u8 = values[2].parse().unwrap_or(0); - Colour { red, green, blue } + Colour { red, green, blue } + } } #[test] fn test_colour_from_string() { assert_eq!( - colour_from_string("0,255,0".to_string()), + Colour::from("0,255,0".to_string()), 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 colours = lines[colour_start_index..].iter().map(|&line| { - colour_from_string(line.to_string()) + Colour::from(line.to_string()) }).collect(); Palette { id, name, colours }