diff --git a/Cargo.toml b/Cargo.toml index caaa387..ba5009d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,4 @@ keywords = ["gamedev"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +radix_fmt = "1.0.0" diff --git a/src/lib.rs b/src/lib.rs index 9d8e143..177f5e4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +use radix_fmt::radix_36; + pub mod avatar; pub mod colour; pub mod dialogue; @@ -62,3 +64,23 @@ impl AnimationFrames for Vec { string } } + +fn from_base36(str: &str) -> u64 { + u64::from_str_radix(str, 36).unwrap() +} + +#[test] +fn test_from_base36() { + assert_eq!(from_base36("0"), 0); + assert_eq!(from_base36("0z"), 35); + assert_eq!(from_base36("11"), 37); +} + +fn to_base36(input: u64) -> String { + format!("{}", radix_36(input)) +} + +#[test] +fn test_to_base36() { + assert_eq!(to_base36(37), "11"); +} diff --git a/src/mock.rs b/src/mock.rs index ed589a0..b5928cb 100644 --- a/src/mock.rs +++ b/src/mock.rs @@ -165,7 +165,7 @@ pub fn game_default() -> Game { room_format: 1, palettes: vec![ Palette { - id: "0".to_string(), + id: 0, name: None, colours: vec![ Colour {red: 0, green: 82, blue: 204 }, diff --git a/src/palette.rs b/src/palette.rs index 1fbf9f4..0795be9 100644 --- a/src/palette.rs +++ b/src/palette.rs @@ -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, pub colours: Vec, } @@ -11,7 +12,7 @@ impl From 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 },