From be762fc5d37ba48386996afb4c5ba0c94aaf5883 Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 10 Dec 2021 00:54:58 -0600 Subject: rust is driving me insane --- minecraft-chat/src/lib.rs | 6 +++++- minecraft-chat/src/style.rs | 50 ++++++++++++++++++++++----------------------- 2 files changed, 29 insertions(+), 27 deletions(-) (limited to 'minecraft-chat/src') diff --git a/minecraft-chat/src/lib.rs b/minecraft-chat/src/lib.rs index 2fbad937..b7035e13 100644 --- a/minecraft-chat/src/lib.rs +++ b/minecraft-chat/src/lib.rs @@ -1,4 +1,8 @@ -//! Things for working with Minecraft chat messages, inspired by the Minecraft source code and prismarine-chat. +//! Things for working with Minecraft chat messages. +//! This was inspired by Minecraft and prismarine-chat. + +#[macro_use] +extern crate lazy_static; pub mod base_component; pub mod component; diff --git a/minecraft-chat/src/style.rs b/minecraft-chat/src/style.rs index c9d419a2..5d5cc8a5 100644 --- a/minecraft-chat/src/style.rs +++ b/minecraft-chat/src/style.rs @@ -9,8 +9,26 @@ pub struct TextColor { } impl TextColor { - // hopefully rust/llvm optimizes this so it's just calculated once - fn calculate_legacy_format_to_color() -> HashMap<&'static ChatFormatting<'static>, TextColor> { + pub fn parse(value: String) -> Result { + if value.starts_with("#") { + let n = value.chars().skip(1).collect::(); + let n = u32::from_str_radix(&n, 16).unwrap(); + return Ok(TextColor::from_rgb(n)); + } + let color = NAMED_COLORS.get(&value.to_ascii_uppercase()); + if color.is_some() { + return Ok(color.unwrap().clone()); + } + Err(format!("Invalid color {}", value)) + } + + fn from_rgb(value: u32) -> TextColor { + TextColor { value, name: None } + } +} + +lazy_static! { + static ref LEGACY_FORMAT_TO_COLOR: HashMap<&'static ChatFormatting<'static>, TextColor> = { let mut legacy_format_to_color = HashMap::new(); for formatter in &ChatFormatting::FORMATTERS { if !formatter.is_format && *formatter != ChatFormatting::RESET { @@ -24,34 +42,14 @@ impl TextColor { } } legacy_format_to_color - } - - fn calculate_named_colors() -> HashMap { - let legacy_format_to_color = Self::calculate_legacy_format_to_color(); + }; + static ref NAMED_COLORS: HashMap = { let mut named_colors = HashMap::new(); - for color in legacy_format_to_color.values() { + for color in LEGACY_FORMAT_TO_COLOR.values() { named_colors.insert(color.name.clone().unwrap(), color.clone()); } named_colors - } - - pub fn parse(value: String) -> Result { - if value.starts_with("#") { - let n = value.chars().skip(1).collect::(); - let n = u32::from_str_radix(&n, 16).unwrap(); - return Ok(TextColor::from_rgb(n)); - } - let named_colors = Self::calculate_named_colors(); - let color = named_colors.get(&value.to_ascii_uppercase()); - if color.is_some() { - return Ok(color.unwrap().clone()); - } - Err(format!("Invalid color {}", value)) - } - - fn from_rgb(value: u32) -> TextColor { - TextColor { value, name: None } - } + }; } /// The weird S character Minecraft used to use for chat formatting -- cgit v1.2.3