aboutsummaryrefslogtreecommitdiff
path: root/minecraft-chat/src
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2021-12-10 00:54:58 -0600
committermat <github@matdoes.dev>2021-12-10 00:54:58 -0600
commitbe762fc5d37ba48386996afb4c5ba0c94aaf5883 (patch)
treea7c96227c51e291e4e59eab7a66c6bc223973e06 /minecraft-chat/src
parentf64750afdd9b18379f706df66c32806b6d21bbe8 (diff)
downloadazalea-drasl-be762fc5d37ba48386996afb4c5ba0c94aaf5883.tar.xz
rust is driving me insane
Diffstat (limited to 'minecraft-chat/src')
-rw-r--r--minecraft-chat/src/lib.rs6
-rw-r--r--minecraft-chat/src/style.rs50
2 files changed, 29 insertions, 27 deletions
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<TextColor, String> {
+ if value.starts_with("#") {
+ let n = value.chars().skip(1).collect::<String>();
+ 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<String, TextColor> {
- let legacy_format_to_color = Self::calculate_legacy_format_to_color();
+ };
+ static ref NAMED_COLORS: HashMap<String, TextColor> = {
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<TextColor, String> {
- if value.starts_with("#") {
- let n = value.chars().skip(1).collect::<String>();
- 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