aboutsummaryrefslogtreecommitdiffstats
path: root/src/teamspeak/bbcode.rs
blob: 28be08a2cc9dcc9797bf2b367b4c49d99ee00ecf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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)
}