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
49
50
51
52
53
54
55
56
57
58
59
use crate::{Formatter, Result};

/// Format trait for the [`stylish` format,
/// `{:s}`](stylish#implementing-a-style-for-a-type).
///
/// `Display` is similar to [`core::fmt::Display`], but allows attaching
/// additional style attributes to the output.
///
/// ```rust
/// struct Name(&'static str);
///
/// impl stylish::Display for Name {
///     fn fmt(&self, f: &mut stylish::Formatter<'_>) -> stylish::Result {
///         let color = match self.0 {
///             "Ferris" => stylish::Color::Red,
///             "Gorris" => stylish::Color::Cyan,
///             _ => stylish::Color::Default,
///         };
///         f.with(stylish::Foreground(color)).write_str(self.0)
///     }
/// }
///
/// let formatted = stylish::html::format!("Hello {:s} and {:s}", Name("Ferris"), Name("Gorris"));
/// assert_eq!(
///     formatted,
///     "Hello <span style=color:red>Ferris</span> and <span style=color:cyan>Gorris</span>",
/// );
/// ```
pub trait Display {
    /// Formats the value using the given formatter.
    ///
    /// ```rust
    /// struct Name(&'static str);
    ///
    /// impl stylish::Display for Name {
    ///     fn fmt(&self, f: &mut stylish::Formatter<'_>) -> stylish::Result {
    ///         let color = match self.0 {
    ///             "Ferris" => stylish::Color::Red,
    ///             "Gorris" => stylish::Color::Cyan,
    ///             _ => stylish::Color::Default,
    ///         };
    ///         f.with(stylish::Foreground(color)).write_str(self.0)
    ///     }
    /// }
    ///
    /// let formatted = stylish::html::format!("Hello {:s} and {:s}", Name("Ferris"), Name("Gorris"));
    /// assert_eq!(
    ///     formatted,
    ///     "Hello <span style=color:red>Ferris</span> and <span style=color:cyan>Gorris</span>",
    /// );
    /// ```
    fn fmt(&self, f: &mut Formatter<'_>) -> Result;
}

impl<T: Display + ?Sized> Display for &T {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        (**self).fmt(f)
    }
}