From dbaaeb68bdf4143b9dca55b2baa86a2656a3249e Mon Sep 17 00:00:00 2001 From: Felix Kaaman Date: Sat, 22 Feb 2020 19:24:05 +0200 Subject: Add functions for BBCode formatting --- src/teamspeak/bbcode.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/teamspeak/bbcode.rs (limited to 'src/teamspeak/bbcode.rs') diff --git a/src/teamspeak/bbcode.rs b/src/teamspeak/bbcode.rs new file mode 100644 index 0000000..28be08a --- /dev/null +++ b/src/teamspeak/bbcode.rs @@ -0,0 +1,42 @@ +use std::fmt::{Formatter, Display, Error}; + +#[allow(dead_code)] +pub enum BbCode<'a> { + Bold(&'a dyn Display), + Italic(&'a dyn Display), + Underline(&'a dyn Display), + Link(&'a dyn Display, &'a str), +} + +impl<'a> Display for BbCode<'a> { + fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> { + match self { + BbCode::Bold(text) => fmt.write_fmt(format_args!("[B]{}[/B]", text))?, + BbCode::Italic(text) => fmt.write_fmt(format_args!("[I]{}[/I]", text))?, + BbCode::Underline(text) => fmt.write_fmt(format_args!("[U]{}[/U]", text))?, + BbCode::Link(text, url) => fmt.write_fmt(format_args!("[URL={}]{}[/URL]", url, text))?, + }; + + Ok(()) + } +} + +#[allow(dead_code)] +pub fn bold(text: &dyn Display) -> BbCode { + BbCode::Bold(text) +} + +#[allow(dead_code)] +pub fn italic(text: &dyn Display) -> BbCode { + BbCode::Italic(text) +} + +#[allow(dead_code)] +pub fn underline(text: &dyn Display) -> BbCode { + BbCode::Underline(text) +} + +#[allow(dead_code)] +pub fn link<'a>(text: &'a dyn Display, url: &'a str) -> BbCode<'a> { + BbCode::Link(text, url) +} -- cgit v1.2.3-70-g09d2