move colour to own module
This commit is contained in:
parent
ffeb16363f
commit
e057b1f63a
|
@ -0,0 +1,43 @@
|
|||
pub mod colour {
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub struct Colour {
|
||||
pub(crate) red: u8,
|
||||
pub(crate) green: u8,
|
||||
pub(crate) blue: u8,
|
||||
}
|
||||
|
||||
impl From<String> 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);
|
||||
|
||||
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 }
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_colour_to_string() {
|
||||
assert_eq!(
|
||||
colour::Colour { red: 22, green: 33, blue: 44 }.to_string(),
|
||||
"22,33,44".to_string()
|
||||
);
|
||||
}
|
44
src/lib.rs
44
src/lib.rs
|
@ -1,9 +1,6 @@
|
|||
#[derive(Debug, Eq, PartialEq)]
|
||||
struct Colour {
|
||||
red: u8,
|
||||
green: u8,
|
||||
blue: u8,
|
||||
}
|
||||
pub mod colour;
|
||||
|
||||
use colour::colour::Colour;
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
struct Palette {
|
||||
|
@ -589,41 +586,6 @@ fn test_tile_to_string() {
|
|||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
impl From<String> 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);
|
||||
|
||||
Colour { red, green, blue }
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_colour_from_string() {
|
||||
assert_eq!(
|
||||
Colour::from("0,255,0".to_string()),
|
||||
Colour { red: 0, green: 255, blue: 0 }
|
||||
);
|
||||
}
|
||||
|
||||
impl ToString for Colour {
|
||||
#[inline]
|
||||
fn to_string(&self) -> String {
|
||||
format!("{},{},{}", self.red, self.green, self.blue)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_colour_to_string() {
|
||||
assert_eq!(
|
||||
Colour { red: 22, green: 33, blue: 44 }.to_string(),
|
||||
"22,33,44".to_string()
|
||||
);
|
||||
}
|
||||
|
||||
impl From<String> for Palette {
|
||||
fn from(string: String) -> Palette {
|
||||
let lines: Vec<&str> = string.lines().collect();
|
||||
|
|
Loading…
Reference in New Issue