cargo fmt + irc format messages

This commit is contained in:
wrk 2023-05-30 03:14:52 +02:00
parent c8d132767a
commit 2f2c3c67a6
4 changed files with 105 additions and 4 deletions

99
src/format.rs Normal file
View File

@ -0,0 +1,99 @@
#[derive(Clone, Copy)]
pub enum Format {
Bold,
Italics,
Underline,
Strikethrough,
Reverse,
Color,
Plain,
}
impl std::fmt::Display for Format {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Format::Bold => write!(f, "\x02"),
Format::Italics => write!(f, "\x1d"),
Format::Underline => write!(f, "\x1f"),
Format::Strikethrough => write!(f, "\x1e"),
Format::Reverse => write!(f, "\x12"),
Format::Color => write!(f, "\x03"),
Format::Plain => write!(f, "\x0f"),
}
}
}
#[derive(Clone, Copy)]
pub enum Color {
White,
Black,
Blue,
Green,
Red,
Brown,
Purple,
Orange,
Yellow,
LightGreen,
Teal,
Cyan,
LightBlue,
Magenta,
Gray,
LightGray,
}
impl std::fmt::Display for Color {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Color::White => write!(f, "00"),
Color::Black => write!(f, "01"),
Color::Blue => write!(f, "02"),
Color::Green => write!(f, "03"),
Color::Red => write!(f, "04"),
Color::Brown => write!(f, "05"),
Color::Purple => write!(f, "06"),
Color::Orange => write!(f, "07"),
Color::Yellow => write!(f, "08"),
Color::LightGreen => write!(f, "09"),
Color::Teal => write!(f, "10"),
Color::Cyan => write!(f, "11"),
Color::LightBlue => write!(f, "12"),
Color::Magenta => write!(f, "13"),
Color::Gray => write!(f, "14"),
Color::LightGray => write!(f, "15"),
}
}
}
pub struct Msg(String);
impl std::fmt::Display for Msg {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", &self.0)
}
}
impl Msg {
pub fn new() -> Self {
Self(String::new())
}
pub fn text(mut self, text: impl AsRef<str>) -> Self {
self.0 += text.as_ref();
self
}
pub fn color(mut self, color: Color) -> Self {
self.0 += &Format::Color.to_string();
self.0 += &color.to_string();
self
}
pub fn format(mut self, format: Format) -> Self {
self.0 += &format.to_string();
self
}
pub fn reset(mut self) -> Self {
self.0 += &Format::Plain.to_string();
self
}
}

View File

@ -1,5 +1,6 @@
pub mod events; pub mod events;
pub mod factory; pub mod factory;
pub mod format;
pub mod irc_command; pub mod irc_command;
pub mod system; pub mod system;
pub mod system_params; pub mod system_params;

View File

@ -1,6 +1,6 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use crate::{factory::Factory, IrcPrefix}; use crate::{factory::Factory, format::Msg, IrcPrefix};
pub struct FunctionSystem<Input, F> { pub struct FunctionSystem<Input, F> {
f: F, f: F,
@ -113,13 +113,13 @@ impl IntoResponse for &str {
} }
} }
impl IntoResponse for Vec<String> { impl IntoResponse for Msg {
fn response(self) -> Response { fn response(self) -> Response {
Response(Some(self)) Response(Some(vec![self.to_string()]))
} }
} }
impl IntoResponse for Vec<&str> { impl<T: std::fmt::Display> IntoResponse for Vec<T> {
fn response(self) -> Response { fn response(self) -> Response {
Response(Some( Response(Some(
self.iter().map(|elem| elem.to_string()).collect::<Vec<_>>(), self.iter().map(|elem| elem.to_string()).collect::<Vec<_>>(),

View File

@ -0,0 +1 @@