From a3014f26944e364bacc4ad29a7bebb58f8c80180 Mon Sep 17 00:00:00 2001 From: seth Date: Sat, 30 Mar 2024 04:11:04 -0400 Subject: [PATCH] consts: store colors as hex codes in struct --- src/commands/general/members.rs | 4 +-- src/commands/general/stars.rs | 4 +-- src/commands/general/tag.rs | 10 +++--- src/consts.rs | 50 ++++++++++++++++++-------- src/handlers/error.rs | 6 ++-- src/handlers/event/analyze_logs/mod.rs | 6 ++-- 6 files changed, 52 insertions(+), 28 deletions(-) diff --git a/src/commands/general/members.rs b/src/commands/general/members.rs index 48525be..483b1a3 100644 --- a/src/commands/general/members.rs +++ b/src/commands/general/members.rs @@ -1,4 +1,4 @@ -use crate::{consts, Context}; +use crate::{consts::Colors, Context}; use eyre::{eyre, Context as _, OptionExt, Result}; use log::trace; @@ -29,7 +29,7 @@ pub async fn members(ctx: Context<'_>) -> Result<()> { let embed = CreateEmbed::new() .title(format!("{member_count} total members!",)) .description(format!("{online_count} online members",)) - .color(consts::colors()["blue"]); + .color(Colors::BLUE); let reply = CreateReply::default().embed(embed); ctx.send(reply).await?; diff --git a/src/commands/general/stars.rs b/src/commands/general/stars.rs index b669dbe..916c03f 100644 --- a/src/commands/general/stars.rs +++ b/src/commands/general/stars.rs @@ -1,4 +1,4 @@ -use crate::{api, consts, Context}; +use crate::{api, consts::Colors, Context}; use eyre::Result; use log::trace; @@ -27,7 +27,7 @@ pub async fn stars(ctx: Context<'_>) -> Result<()> { let embed = CreateEmbed::new() .title(format!("⭐ {count} total stars!")) - .color(consts::colors()["yellow"]); + .color(Colors::YELLOW); let reply = CreateReply::default().embed(embed); ctx.send(reply).await?; diff --git a/src/commands/general/tag.rs b/src/commands/general/tag.rs index b34a250..607ba8e 100644 --- a/src/commands/general/tag.rs +++ b/src/commands/general/tag.rs @@ -1,6 +1,7 @@ #![allow(non_camel_case_types, clippy::upper_case_acronyms)] -use crate::{consts, tags::Tag, Context}; +use crate::{consts::Colors, tags::Tag, Context}; use std::env; +use std::str::FromStr; use std::sync::OnceLock; use eyre::{eyre, Result}; @@ -40,9 +41,10 @@ pub async fn tag( let mut e = CreateEmbed::new(); if let Some(color) = &frontmatter.color { - let color = *consts::colors() - .get(color.as_str()) - .unwrap_or(&Color::default()); + let color = Colors::from_str(color.as_str()) + .map(Color::from) + .unwrap_or_default(); + e = e.color(color); } diff --git a/src/consts.rs b/src/consts.rs index f823470..e53ae36 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -1,17 +1,39 @@ -use std::{collections::HashMap, sync::OnceLock}; +#![allow(clippy::unreadable_literal)] +use std::str::FromStr; -use poise::serenity_prelude::Color; +use poise::serenity_prelude::Colour; -pub fn colors() -> &'static HashMap<&'static str, Color> { - static COLORS: OnceLock> = OnceLock::new(); - COLORS.get_or_init(|| { - HashMap::from([ - ("red", Color::from((239, 68, 68))), - ("green", Color::from((34, 197, 94))), - ("blue", Color::from((96, 165, 250))), - ("yellow", Color::from((253, 224, 71))), - ("orange", Color::from((251, 146, 60))), - // TODO purple & pink :D - ]) - }) +#[derive(Clone, Copy, Debug, Default)] +pub struct Colors(i32); + +impl Colors { + pub const RED: i32 = 0xEF4444; + pub const GREEN: i32 = 0x22C55E; + pub const BLUE: i32 = 0x60A5FA; + pub const YELLOW: i32 = 0xFDE047; + pub const ORANGE: i32 = 0xFB923C; + + pub fn as_i32(self) -> i32 { + self.0 + } +} + +impl FromStr for Colors { + type Err = (); + fn from_str(s: &str) -> Result { + match s.to_lowercase().as_str() { + "red" => Ok(Colors(Self::RED)), + "green" => Ok(Colors(Self::GREEN)), + "blue" => Ok(Colors(Self::BLUE)), + "yellow" => Ok(Colors(Self::YELLOW)), + "orange" => Ok(Colors(Self::ORANGE)), + _ => Err(()), + } + } +} + +impl From for Colour { + fn from(value: Colors) -> Self { + Self::from(value.as_i32()) + } } diff --git a/src/handlers/error.rs b/src/handlers/error.rs index 019bb3f..16fcda4 100644 --- a/src/handlers/error.rs +++ b/src/handlers/error.rs @@ -1,5 +1,5 @@ -use crate::consts; -use crate::Data; +use crate::{consts::Colors, Data}; + use std::fmt::Write; use eyre::Report; @@ -34,7 +34,7 @@ pub async fn handle(error: FrameworkError<'_, Data, Report>) { .title("Something went wrong!") .description("oopsie") .timestamp(Timestamp::now()) - .color(consts::colors()["red"]); + .color(Colors::RED); let reply = CreateReply::default().embed(embed); diff --git a/src/handlers/event/analyze_logs/mod.rs b/src/handlers/event/analyze_logs/mod.rs index 58c38cf..e490d09 100644 --- a/src/handlers/event/analyze_logs/mod.rs +++ b/src/handlers/event/analyze_logs/mod.rs @@ -1,4 +1,4 @@ -use crate::{consts, Data}; +use crate::{consts::Colors, Data}; use eyre::Result; use log::{debug, trace}; @@ -48,10 +48,10 @@ pub async fn handle(ctx: &Context, message: &Message, data: &Data) -> Result<()> if issues.is_empty() { e = e - .color(consts::colors()["green"]) + .color(Colors::GREEN) .description("No issues found automatically"); } else { - e = e.color(consts::colors()["red"]); + e = e.color(Colors::RED); for (title, description) in issues { e = e.field(title, description, false);