From 309899168a086de88acf97fd6683387a7af7078c Mon Sep 17 00:00:00 2001 From: Max Audron Date: Fri, 22 Oct 2021 19:08:59 +0200 Subject: write tons of documentation and reorganize some modules --- src/util/formatting/color.rs | 1 + src/util/formatting/mod.rs | 57 +++++++++++++++++++++++++++++++++++---- src/util/formatting/truncate.rs | 1 + src/util/mod.rs | 9 +++++-- src/util/web.rs | 60 ----------------------------------------- src/util/web/mod.rs | 42 +++++++++++++++++++++++++++++ src/util/web/url_shorteners.rs | 21 +++++++++++++++ 7 files changed, 124 insertions(+), 67 deletions(-) delete mode 100644 src/util/web.rs create mode 100644 src/util/web/mod.rs create mode 100644 src/util/web/url_shorteners.rs (limited to 'src/util') diff --git a/src/util/formatting/color.rs b/src/util/formatting/color.rs index 6907805..ac78b9c 100644 --- a/src/util/formatting/color.rs +++ b/src/util/formatting/color.rs @@ -1,3 +1,4 @@ +/// The basic 16 IRC color codes pub enum Color { White = 00, Black = 01, diff --git a/src/util/formatting/mod.rs b/src/util/formatting/mod.rs index 5cfdfa8..a20b48f 100644 --- a/src/util/formatting/mod.rs +++ b/src/util/formatting/mod.rs @@ -1,9 +1,31 @@ +//! Tools for formatting irc messages + mod truncate; mod color; pub use truncate::*; pub use color::*; +/// Turn strings bold, italic,underline, strikethrough, and monospace. +/// +/// The Formatting trait is implemented on `String` and `&str` for your convenience +/// +/// # Format a string +/// ``` +/// use catinator::util::Formatting; +/// +/// let text = "this should be bold"; +/// assert_eq!("\x02this should be bold\x02", text.bold()) +/// ``` +/// +/// # Use raw formatting code +/// ``` +/// use catinator::util::Formatting; +/// +/// let text = "this should be bold"; +/// let result = format!("{}{}", String::BOLD, text); +/// assert_eq!("\x02this should be bold", result) +/// ``` pub trait Formatting<'r> { const BOLD: &'r str = "\x02"; const ITALIC: &'r str = "\x1D"; @@ -12,11 +34,11 @@ pub trait Formatting<'r> { const MONOSPACE: &'r str = "\x11"; const COLOR: &'r str = "\x03"; - fn bold(self) -> Self; - fn italic(self) -> Self; - fn underline(self) -> Self; - fn strikethrough(self) -> Self; - fn monospace(self) -> Self; + fn bold(self) -> String; + fn italic(self) -> String; + fn underline(self) -> String; + fn strikethrough(self) -> String; + fn monospace(self) -> String; // fn color(self, color: Color) -> Self; } @@ -65,6 +87,31 @@ impl<'r> Formatting<'r> for String { // } } +impl<'r> Formatting<'r> for &str { + fn bold(self) -> String { + format!("{}{}{}", Self::BOLD, self, Self::BOLD) + } + + fn italic(self) -> String { + format!("{}{}{}", Self::ITALIC, self, Self::ITALIC) + } + + fn underline(self) -> String { + format!("{}{}{}", Self::UNDERLINE, self, Self::UNDERLINE) + } + + fn strikethrough(self) -> String { + format!("{}{}{}", Self::STRIKETHROUGH, self, Self::STRIKETHROUGH) + } + + fn monospace(self) -> String { + format!("{}{}{}", Self::MONOSPACE, self, Self::MONOSPACE) + } + + // TODO implement color codes + // fn color(mut self, foreground: Option, background: Option) -> Self { } +} + #[cfg(all(test, feature = "bench"))] mod bench { use super::*; diff --git a/src/util/formatting/truncate.rs b/src/util/formatting/truncate.rs index c1b6257..80caa23 100644 --- a/src/util/formatting/truncate.rs +++ b/src/util/formatting/truncate.rs @@ -1,4 +1,5 @@ /// Truncates a string after a certain number of characters. +/// /// Function always tries to truncate on a word boundary. /// Reimplemented from gonzobot. pub fn truncate(text: &str, len: usize) -> String { diff --git a/src/util/mod.rs b/src/util/mod.rs index a8988c3..23ef2ce 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,2 +1,7 @@ -pub mod formatting; -pub mod web; +//! Utilities for dealing with IRC and bot making + +mod formatting; +mod web; + +pub use formatting::*; +pub use web::*; diff --git a/src/util/web.rs b/src/util/web.rs deleted file mode 100644 index b9f44c1..0000000 --- a/src/util/web.rs +++ /dev/null @@ -1,60 +0,0 @@ -use anyhow::{Context, Error, Result}; -use async_trait::async_trait; -use reqwest::{get, Url}; -use urlparse::quote_plus as urlparse_quote_plus; - -#[async_trait] -pub trait UrlShortener { - fn new() -> Self; - async fn shorten(&self, url: &str) -> Result; -} - -pub struct IsgdUrlShortener {} - -#[async_trait] -impl UrlShortener for IsgdUrlShortener { - fn new() -> Self { - Self {} - } - - async fn shorten(&self, url: &str) -> Result { - Ok(get(Url::parse(&format!( - "https://is.gd/create.php?format=simple&url={}", - url - )) - .context("Failed to parse url")?) - .await - .context("Failed to make request")? - .text() - .await - .context("failed to get request response text")?) - } -} - -pub fn quote_plus(text: &str) -> Result { - Ok(urlparse_quote_plus(text, b"")?) -} - -#[cfg(test)] -mod tests { - use super::quote_plus; - use anyhow::{Error, Result}; - - #[test] - fn test_quote_plus_1() -> Result<(), Error> { - assert_eq!(quote_plus("5/10")?, "5%2F10"); - Ok(()) - } - - #[test] - fn test_quote_plus_2() -> Result<(), Error> { - assert_eq!(quote_plus("1 * 2")?, "1+%2A+2"); - Ok(()) - } - - #[test] - fn test_quote_plus_3() -> Result<(), Error> { - assert_eq!(quote_plus("e_plus("1 * 2")?)?, "1%2B%252A%2B2"); - Ok(()) - } -} diff --git a/src/util/web/mod.rs b/src/util/web/mod.rs new file mode 100644 index 0000000..4e886af --- /dev/null +++ b/src/util/web/mod.rs @@ -0,0 +1,42 @@ +use anyhow::{Error, Result}; +use async_trait::async_trait; +use urlparse::quote_plus as urlparse_quote_plus; + +pub mod url_shorteners; + +/// Shorten urls +#[async_trait] +pub trait UrlShortener { + /// Call this method with the url you want shortened. + /// Returns the shortened url. + async fn shorten(url: &str) -> Result; +} + +/// quote strings to be URL save +pub fn quote_plus(text: &str) -> Result { + Ok(urlparse_quote_plus(text, b"")?) +} + +#[cfg(test)] +mod tests { + use super::quote_plus; + use anyhow::{Error, Result}; + + #[test] + fn test_quote_plus_1() -> Result<(), Error> { + assert_eq!(quote_plus("5/10")?, "5%2F10"); + Ok(()) + } + + #[test] + fn test_quote_plus_2() -> Result<(), Error> { + assert_eq!(quote_plus("1 * 2")?, "1+%2A+2"); + Ok(()) + } + + #[test] + fn test_quote_plus_3() -> Result<(), Error> { + assert_eq!(quote_plus("e_plus("1 * 2")?)?, "1%2B%252A%2B2"); + Ok(()) + } +} diff --git a/src/util/web/url_shorteners.rs b/src/util/web/url_shorteners.rs new file mode 100644 index 0000000..74d62ce --- /dev/null +++ b/src/util/web/url_shorteners.rs @@ -0,0 +1,21 @@ +use anyhow::{Context, Error, Result}; +use async_trait::async_trait; +use reqwest::{get, Url}; + +pub struct Isgd; + +#[async_trait] +impl super::UrlShortener for Isgd { + async fn shorten(url: &str) -> Result { + Ok(get(Url::parse(&format!( + "https://is.gd/create.php?format=simple&url={}", + url + )) + .context("Failed to parse url")?) + .await + .context("Failed to make request")? + .text() + .await + .context("failed to get request response text")?) + } +} -- cgit v1.2.3