Skip to content

Commit

Permalink
Implement Info.environment_names and Info.color_schemes
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinegb committed Aug 1, 2024
1 parent 7491e88 commit 60cf30c
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/hex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use std::fmt;

use serde::{
de::{Unexpected, Visitor},
Deserializer, Serializer,
};

const EXPECTING: &str = "an RGBA hex code string";

struct HexVisitor;

impl<'de> Visitor<'de> for HexVisitor {
type Value = u32;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(EXPECTING)
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
let without_hash = v.trim_start_matches('#');
let parsed = u32::from_str_radix(without_hash, 16)
.map_err(|_| E::invalid_value(Unexpected::Str(&v), &EXPECTING))?;

Ok(parsed)
}
}

pub(super) fn serialize<S: Serializer>(v: &u32, serializer: S) -> Result<S::Ok, S::Error> {
let with_0x = format!("{v:#08X}");
let with_hash = format!("#{}", with_0x.trim_start_matches("0x"));

serializer.serialize_str(&with_hash)
}

pub(super) fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<u32, D::Error> {
deserializer.deserialize_str(HexVisitor)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn serializes_correctly() {
let mut writer = Vec::new();
let mut serializer = serde_json::Serializer::new(&mut writer);

serialize(&0xC81414FF, &mut serializer).unwrap();
assert_eq!(String::from_utf8(writer).unwrap(), "\"#C81414FF\"");
}

#[test]
fn deserializes_correctly() {
let mut deserializer = serde_json::Deserializer::from_str("\"#288ED2FF\"");

assert_eq!(deserialize(&mut deserializer).unwrap(), 0x288ED2FF);
}
}
75 changes: 75 additions & 0 deletions src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ pub struct Info {
///
/// Refer to the [BSMG Wiki](https://bsmg.wiki/mapping/map-format/info.html#cover-image-filename) for language-agnostic documentation.
pub cover_image_filename: PathBuf,
/// Surrounding world that a player is within when playing a map.
///
/// Refer to the [BSMG Wiki](https://bsmg.wiki/mapping/map-format/info.html#environments) for language-agnostic documentation.
pub environment_names: Vec<String>,
/// Color palettes used across in-game objects.
///
/// Refer to the [BSMG Wiki](https://bsmg.wiki/mapping/map-format/info.html#color-schemes) for language-agnostic documentation.
pub color_schemes: Vec<ColorScheme>,
}

impl Default for Info {
Expand All @@ -35,6 +43,8 @@ impl Default for Info {
audio: Default::default(),
song_preview_filename: "song.ogg".into(),
cover_image_filename: "cover.png".into(),
environment_names: Default::default(),
color_schemes: Default::default(),
}
}
}
Expand Down Expand Up @@ -111,6 +121,56 @@ impl Default for Audio {
}
}

/// Color palette used across in-game objects.
///
/// Refer to the [BSMG Wiki](https://bsmg.wiki/mapping/map-format/info.html#color-schemes) for language-agnostic documentation.
#[derive(Debug, PartialEq, Default, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct ColorScheme {
/// Refer to the [BSMG Wiki](https://bsmg.wiki/mapping/map-format/info.html#color-schemes) for language-agnostic documentation.
pub use_override: bool,
/// Player-facing name of color scheme.
///
/// Refer to the [BSMG Wiki](https://bsmg.wiki/mapping/map-format/info.html#color-schemes) for language-agnostic documentation.
pub color_scheme_name: String,
/// Color of left saber.
///
/// Refer to the [BSMG Wiki](https://bsmg.wiki/mapping/map-format/info.html#color-schemes) for language-agnostic documentation.
#[serde(with = "super::hex")]
pub saber_a_color: u32,
/// Color of right saber.
///
/// Refer to the [BSMG Wiki](https://bsmg.wiki/mapping/map-format/info.html#color-schemes) for language-agnostic documentation.
#[serde(with = "super::hex")]
pub saber_b_color: u32,
/// Color of wall obstacles.
///
/// Refer to the [BSMG Wiki](https://bsmg.wiki/mapping/map-format/info.html#color-schemes) for language-agnostic documentation.
#[serde(with = "super::hex")]
pub obstacles_color: u32,
/// One of two environment colors.
///
/// Refer to the [BSMG Wiki](https://bsmg.wiki/mapping/map-format/info.html#color-schemes) for language-agnostic documentation.
#[serde(with = "super::hex")]
pub environment_color_0: u32,
/// One of two environment colors.
///
/// Refer to the [BSMG Wiki](https://bsmg.wiki/mapping/map-format/info.html#color-schemes) for language-agnostic documentation.
#[serde(with = "super::hex")]
pub environment_color_1: u32,
/// Boosted variant of one of two environment colors.
///
/// Refer to the [BSMG Wiki](https://bsmg.wiki/mapping/map-format/info.html#color-schemes) for language-agnostic documentation.
#[serde(with = "super::hex")]
pub environment_color_0_boost: u32,
/// Boosted variant of one of two environment colors.
///
/// Refer to the [BSMG Wiki](https://bsmg.wiki/mapping/map-format/info.html#color-schemes) for language-agnostic documentation.
#[serde(with = "super::hex")]
pub environment_color_1_boost: u32,
}

#[cfg(test)]
mod tests {
use std::fs;
Expand Down Expand Up @@ -142,6 +202,21 @@ mod tests {
},
song_preview_filename: "song.ogg".into(),
cover_image_filename: "cover.png".into(),
environment_names: vec![
"WeaveEnvironment".to_string(),
"GlassDesertEnvironment".to_string(),
],
color_schemes: vec![ColorScheme {
use_override: true,
color_scheme_name: "Weave".to_string(),
saber_a_color: 0xC81414FF,
saber_b_color: 0x288ED2FF,
obstacles_color: 0xFF3030FF,
environment_color_0: 0xD91616FF,
environment_color_1: 0x30ACFFFF,
environment_color_0_boost: 0xD216D9FF,
environment_color_1_boost: 0x00FFA5FF,
}],
},
);
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#![warn(missing_docs)]

mod hex;
pub mod info;

pub use self::info::Info;
Expand Down

0 comments on commit 60cf30c

Please sign in to comment.