diff --git a/src/colour.rs b/src/colour.rs index 1b7ec66..5690ca9 100644 --- a/src/colour.rs +++ b/src/colour.rs @@ -1,43 +1,42 @@ -pub mod colour { - #[derive(Debug, Eq, PartialEq)] - pub struct Colour { - pub(crate) red: u8, - pub(crate) green: u8, - pub(crate) blue: u8, - } +#[derive(Debug, Eq, PartialEq)] +pub struct Colour { + pub(crate) red: u8, + pub(crate) green: u8, + pub(crate) blue: u8, +} - impl From for Colour { - fn from(string: String) -> Colour { - let values: Vec<&str> = string.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 } - } - } - - impl ToString for Colour { - #[inline] - fn to_string(&self) -> String { - format!("{},{},{}", self.red, self.green, self.blue) - } + Colour { red, green, blue } } } +impl ToString for Colour { + #[inline] + fn to_string(&self) -> String { + format!("{},{},{}", self.red, self.green, self.blue) + } +} + + #[test] fn test_colour_from_string() { assert_eq!( - colour::Colour::from("0,255,0".to_string()), - colour::Colour { red: 0, green: 255, blue: 0 } + Colour::from("0,255,0".to_string()), + Colour { red: 0, green: 255, blue: 0 } ); } #[test] fn test_colour_to_string() { assert_eq!( - colour::Colour { red: 22, green: 33, blue: 44 }.to_string(), + Colour { red: 22, green: 33, blue: 44 }.to_string(), "22,33,44".to_string() ); } diff --git a/src/lib.rs b/src/lib.rs index 2e79a14..a3c47cc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,13 +1,8 @@ pub mod colour; +pub mod palette; -use colour::colour::Colour; - -#[derive(Debug, Eq, PartialEq)] -struct Palette { - id: String, // base36 string (why??) - name: Option, - colours: Vec, -} +use colour::Colour; +use palette::Palette; #[derive(Debug, Eq, PartialEq)] struct Image { @@ -586,99 +581,6 @@ fn test_tile_to_string() { assert_eq!(output, expected); } -impl From for Palette { - fn from(string: String) -> Palette { - let lines: Vec<&str> = string.lines().collect(); - - let id = lines[0].replace("PAL ", ""); - - let name = match lines[1].starts_with("NAME") { - true => Some(lines[1].replace("NAME ", "").to_string()), - false => None, - }; - - let colour_start_index = if name.is_some() { 2 } else { 1 }; - - let colours = lines[colour_start_index..].iter().map(|&line| { - Colour::from(line.to_string()) - }).collect(); - - Palette { id, name, colours } - } -} - -#[test] -fn test_palette_from_string() { - let output = Palette::from( - "PAL 1\nNAME lamplight\n45,45,59\n66,60,39\n140,94,1".to_string() - ); - - let expected = Palette { - id: "1".to_string(), - name: Some("lamplight".to_string()), - colours: vec![ - Colour {red: 45, green: 45, blue: 59}, - Colour {red: 66, green: 60, blue: 39}, - Colour {red: 140, green: 94, blue: 1 }, - ], - }; - - assert_eq!(output, expected); -} - -#[test] -fn test_palette_from_string_no_name() { - let output = Palette::from( - "PAL 9\n45,45,59\n66,60,39\n140,94,1".to_string() - ); - - let expected = Palette { - id: "9".to_string(), - name: None, - colours: vec![ - Colour {red: 45, green: 45, blue: 59}, - Colour {red: 66, green: 60, blue: 39}, - Colour {red: 140, green: 94, blue: 1 }, - ], - }; - - assert_eq!(output, expected); -} - -impl ToString for Palette { - #[inline] - fn to_string(&self) -> String { - let name = if self.name.as_ref().is_some() { - format!("NAME {}\n", self.name.as_ref().unwrap()) - } else { - "".to_string() - }; - - let mut colours = String::new(); - for colour in &self.colours { - colours.push_str(&format!("{}\n", colour.to_string())); - } - colours.pop(); - - format!("PAL {}\n{}{}", self.id, name, colours) - } -} - -#[test] -fn test_palette_to_string() { - let output = Palette { - id: "g".to_string(), - name: Some("moss".to_string()), - colours: vec![ - Colour {red: 1, green: 2, blue: 3 }, - Colour {red: 255, green: 254, blue: 253}, - 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(); - assert_eq!(output, expected); -} - impl From for Position { fn from(string: String) -> Position { // e.g. "2,5" diff --git a/src/palette.rs b/src/palette.rs new file mode 100644 index 0000000..f454d79 --- /dev/null +++ b/src/palette.rs @@ -0,0 +1,101 @@ +use crate::colour::Colour; + +#[derive(Debug, Eq, PartialEq)] +pub struct Palette { + pub(crate) id: String, // base36 string (why??) + pub(crate) name: Option, + pub(crate) colours: Vec, +} + +impl From for Palette { + fn from(string: String) -> Palette { + let lines: Vec<&str> = string.lines().collect(); + + let id = lines[0].replace("PAL ", ""); + + let name = match lines[1].starts_with("NAME") { + true => Some(lines[1].replace("NAME ", "").to_string()), + false => None, + }; + + let colour_start_index = if name.is_some() { 2 } else { 1 }; + + let colours = lines[colour_start_index..].iter().map(|&line| { + Colour::from(line.to_string()) + }).collect(); + + Palette { id, name, colours } + } +} + +impl ToString for Palette { + #[inline] + fn to_string(&self) -> String { + let name = if self.name.as_ref().is_some() { + format!("NAME {}\n", self.name.as_ref().unwrap()) + } else { + "".to_string() + }; + + let mut colours = String::new(); + for colour in &self.colours { + colours.push_str(&format!("{}\n", colour.to_string())); + } + colours.pop(); + + format!("PAL {}\n{}{}", self.id, name, colours) + } +} + +#[test] +fn test_palette_from_string() { + let output = Palette::from( + "PAL 1\nNAME lamplight\n45,45,59\n66,60,39\n140,94,1".to_string() + ); + + let expected = Palette { + id: "1".to_string(), + name: Some("lamplight".to_string()), + colours: vec![ + Colour {red: 45, green: 45, blue: 59}, + Colour {red: 66, green: 60, blue: 39}, + Colour {red: 140, green: 94, blue: 1 }, + ], + }; + + assert_eq!(output, expected); +} + +#[test] +fn test_palette_from_string_no_name() { + let output = Palette::from( + "PAL 9\n45,45,59\n66,60,39\n140,94,1".to_string() + ); + + let expected = Palette { + id: "9".to_string(), + name: None, + colours: vec![ + Colour {red: 45, green: 45, blue: 59}, + Colour {red: 66, green: 60, blue: 39}, + Colour {red: 140, green: 94, blue: 1 }, + ], + }; + + assert_eq!(output, expected); +} + +#[test] +fn test_palette_to_string() { + let output = Palette { + id: "g".to_string(), + name: Some("moss".to_string()), + colours: vec![ + Colour {red: 1, green: 2, blue: 3 }, + Colour {red: 255, green: 254, blue: 253}, + 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(); + assert_eq!(output, expected); +}