convert palette id to int

This commit is contained in:
2020-04-13 13:30:08 +01:00
parent 48e479c189
commit 1928334b27
4 changed files with 31 additions and 7 deletions

View File

@@ -1,8 +1,9 @@
use crate::colour::Colour;
use crate::{from_base36, to_base36};
#[derive(Debug, Eq, PartialEq)]
pub struct Palette {
pub id: String, // base36 string (why??)
pub id: u64,
pub name: Option<String>,
pub colours: Vec<Colour>,
}
@@ -11,7 +12,7 @@ impl From<String> for Palette {
fn from(string: String) -> Palette {
let lines: Vec<&str> = string.lines().collect();
let id = lines[0].replace("PAL ", "");
let id = from_base36(&lines[0].replace("PAL ", ""));
let name = match lines[1].starts_with("NAME") {
true => Some(lines[1].replace("NAME ", "").to_string()),
@@ -43,7 +44,7 @@ impl ToString for Palette {
}
colours.pop();
format!("PAL {}\n{}{}", self.id, name, colours)
format!("PAL {}\n{}{}", to_base36(self.id), name, colours)
}
}
@@ -54,7 +55,7 @@ fn test_palette_from_string() {
);
let expected = Palette {
id: "1".to_string(),
id: 1,
name: Some("lamplight".to_string()),
colours: vec![
Colour {red: 45, green: 45, blue: 59},
@@ -73,7 +74,7 @@ fn test_palette_from_string_no_name() {
);
let expected = Palette {
id: "9".to_string(),
id: 9,
name: None,
colours: vec![
Colour {red: 45, green: 45, blue: 59},
@@ -88,7 +89,7 @@ fn test_palette_from_string_no_name() {
#[test]
fn test_palette_to_string() {
let output = Palette {
id: "g".to_string(),
id: 16,
name: Some("moss".to_string()),
colours: vec![
Colour {red: 1, green: 2, blue: 3 },