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
43
44
45
46
47
48
use super::{html, markdown_v2, Formattable, Nesting};
use std::fmt::{self, Formatter, Write};

/// Formats text in bold. Can be created with [`bold`].
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
#[must_use = "formatters need to be formatted with `markdown_v2` or `html`"]
pub struct Bold<T>(T);

/// Formats text in bold.
pub const fn bold<T: Formattable>(text: T) -> Bold<T> {
    Bold(text)
}

impl<T: Formattable> markdown_v2::Formattable for Bold<T> {
    fn format(
        &self,
        formatter: &mut Formatter,
        nesting: Nesting,
    ) -> fmt::Result {
        if !nesting.bold {
            formatter.write_char('*')?;
        }
        markdown_v2::Formattable::format(
            &self.0,
            formatter,
            Nesting {
                bold: true,
                ..nesting
            },
        )?;
        if !nesting.bold {
            formatter.write_char('*')?;
        }
        Ok(())
    }
}

impl<T: Formattable> html::Formattable for Bold<T> {
    fn format(
        &self,
        formatter: &mut Formatter,
        nesting: Nesting,
    ) -> fmt::Result {
        formatter.write_str("<b>")?;
        html::Formattable::format(&self.0, formatter, nesting)?;
        formatter.write_str("</b>")
    }
}