consts: store colors as hex codes in struct

This commit is contained in:
seth 2024-03-30 04:11:04 -04:00
parent 1ff95de3bf
commit a3014f2694
No known key found for this signature in database
GPG key ID: D31BD0D494BBEE86
6 changed files with 52 additions and 28 deletions

View file

@ -1,4 +1,4 @@
use crate::{consts, Context}; use crate::{consts::Colors, Context};
use eyre::{eyre, Context as _, OptionExt, Result}; use eyre::{eyre, Context as _, OptionExt, Result};
use log::trace; use log::trace;
@ -29,7 +29,7 @@ pub async fn members(ctx: Context<'_>) -> Result<()> {
let embed = CreateEmbed::new() let embed = CreateEmbed::new()
.title(format!("{member_count} total members!",)) .title(format!("{member_count} total members!",))
.description(format!("{online_count} online members",)) .description(format!("{online_count} online members",))
.color(consts::colors()["blue"]); .color(Colors::BLUE);
let reply = CreateReply::default().embed(embed); let reply = CreateReply::default().embed(embed);
ctx.send(reply).await?; ctx.send(reply).await?;

View file

@ -1,4 +1,4 @@
use crate::{api, consts, Context}; use crate::{api, consts::Colors, Context};
use eyre::Result; use eyre::Result;
use log::trace; use log::trace;
@ -27,7 +27,7 @@ pub async fn stars(ctx: Context<'_>) -> Result<()> {
let embed = CreateEmbed::new() let embed = CreateEmbed::new()
.title(format!("{count} total stars!")) .title(format!("{count} total stars!"))
.color(consts::colors()["yellow"]); .color(Colors::YELLOW);
let reply = CreateReply::default().embed(embed); let reply = CreateReply::default().embed(embed);
ctx.send(reply).await?; ctx.send(reply).await?;

View file

@ -1,6 +1,7 @@
#![allow(non_camel_case_types, clippy::upper_case_acronyms)] #![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::env;
use std::str::FromStr;
use std::sync::OnceLock; use std::sync::OnceLock;
use eyre::{eyre, Result}; use eyre::{eyre, Result};
@ -40,9 +41,10 @@ pub async fn tag(
let mut e = CreateEmbed::new(); let mut e = CreateEmbed::new();
if let Some(color) = &frontmatter.color { if let Some(color) = &frontmatter.color {
let color = *consts::colors() let color = Colors::from_str(color.as_str())
.get(color.as_str()) .map(Color::from)
.unwrap_or(&Color::default()); .unwrap_or_default();
e = e.color(color); e = e.color(color);
} }

View file

@ -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> { #[derive(Clone, Copy, Debug, Default)]
static COLORS: OnceLock<HashMap<&str, Color>> = OnceLock::new(); pub struct Colors(i32);
COLORS.get_or_init(|| {
HashMap::from([ impl Colors {
("red", Color::from((239, 68, 68))), pub const RED: i32 = 0xEF4444;
("green", Color::from((34, 197, 94))), pub const GREEN: i32 = 0x22C55E;
("blue", Color::from((96, 165, 250))), pub const BLUE: i32 = 0x60A5FA;
("yellow", Color::from((253, 224, 71))), pub const YELLOW: i32 = 0xFDE047;
("orange", Color::from((251, 146, 60))), pub const ORANGE: i32 = 0xFB923C;
// TODO purple & pink :D
]) pub fn as_i32(self) -> i32 {
}) self.0
}
}
impl FromStr for Colors {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
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<Colors> for Colour {
fn from(value: Colors) -> Self {
Self::from(value.as_i32())
}
} }

View file

@ -1,5 +1,5 @@
use crate::consts; use crate::{consts::Colors, Data};
use crate::Data;
use std::fmt::Write; use std::fmt::Write;
use eyre::Report; use eyre::Report;
@ -34,7 +34,7 @@ pub async fn handle(error: FrameworkError<'_, Data, Report>) {
.title("Something went wrong!") .title("Something went wrong!")
.description("oopsie") .description("oopsie")
.timestamp(Timestamp::now()) .timestamp(Timestamp::now())
.color(consts::colors()["red"]); .color(Colors::RED);
let reply = CreateReply::default().embed(embed); let reply = CreateReply::default().embed(embed);

View file

@ -1,4 +1,4 @@
use crate::{consts, Data}; use crate::{consts::Colors, Data};
use eyre::Result; use eyre::Result;
use log::{debug, trace}; use log::{debug, trace};
@ -48,10 +48,10 @@ pub async fn handle(ctx: &Context, message: &Message, data: &Data) -> Result<()>
if issues.is_empty() { if issues.is_empty() {
e = e e = e
.color(consts::colors()["green"]) .color(Colors::GREEN)
.description("No issues found automatically"); .description("No issues found automatically");
} else { } else {
e = e.color(consts::colors()["red"]); e = e.color(Colors::RED);
for (title, description) in issues { for (title, description) in issues {
e = e.field(title, description, false); e = e.field(title, description, false);