diff --git a/src/colour.rs b/src/colour.rs index 6aa9487..1564d88 100644 --- a/src/colour.rs +++ b/src/colour.rs @@ -5,16 +5,23 @@ pub struct Colour { pub blue: u8, } -impl From for Colour { +#[derive(Debug)] +pub struct InvalidRgb; + +impl Colour { #[inline] - fn from(string: String) -> Colour { + pub(crate) fn from(string: String) -> Result { let values: Vec<&str> = string.split(',').collect(); + if values.len() != 3 { + return Err(InvalidRgb); + } + 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 } + Ok(Colour { red, green, blue }) } } @@ -32,7 +39,7 @@ mod test { #[test] fn test_colour_from_string() { assert_eq!( - Colour::from("0,255,0".to_string()), + Colour::from("0,255,0".to_string()).unwrap(), Colour { red: 0, green: 255, blue: 0 } ); } diff --git a/src/palette.rs b/src/palette.rs index f776910..c1abb3c 100644 --- a/src/palette.rs +++ b/src/palette.rs @@ -23,7 +23,7 @@ impl From for Palette { let colours = lines[colour_start_index..] .iter() - .map(|&line| Colour::from(line.to_string())) + .map(|&line| Colour::from(line.to_string()).unwrap()) .collect(); Palette { id, name, colours }