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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use crate::{Arguments, Display, Restyle, Result, Style, Write};

#[doc(hidden)] // workaround https://github.com/rust-lang/rust/issues/85522
#[derive(Clone, Copy, Debug)]
pub enum Align {
    Left,
    Center,
    Right,
}

#[doc(hidden)] // workaround https://github.com/rust-lang/rust/issues/85522
#[derive(Clone, Copy, Debug)]
pub enum Sign {
    Plus,
    Minus,
}

#[doc(hidden)] // workaround https://github.com/rust-lang/rust/issues/85522
#[derive(Clone, Copy, Debug)]
pub enum DebugHex {
    Lower,
    Upper,
}

#[doc(hidden)] // workaround https://github.com/rust-lang/rust/issues/85522
#[derive(Clone, Copy, Debug, Default)]
pub struct FormatterArgs<'a> {
    pub align: Option<Align>,
    pub sign: Option<Sign>,
    pub alternate: bool,
    pub zero: bool,
    pub width: Option<&'a usize>,
    pub precision: Option<&'a usize>,
    pub debug_hex: Option<DebugHex>,
}

/// A configured output stream.
///
/// A `Formatter` wraps a target output stream with a set of configuration
/// options for formatting of data written to the stream. There is (currently)
/// no public constructors for `Formatter`, an instance is created and passed to
/// implementations of [`stylish::Display`] when they are used in
/// the [`stylish`] macros.
pub struct Formatter<'a> {
    style: Style,
    pub(crate) format: FormatterArgs<'a>,
    write: &'a mut (dyn Write + 'a),
}

impl core::fmt::Debug for Formatter<'_> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result {
        f.debug_struct("Formatter")
            .field("style", &self.style)
            .field("format", &self.format)
            .finish()
    }
}

impl<'a> Formatter<'a> {
    pub(crate) fn new(write: &'a mut (dyn Write + 'a)) -> Self {
        Self {
            style: Style::default(),
            format: FormatterArgs::default(),
            write,
        }
    }

    // TODO: All the rest of the std::fmt::Formatter methods

    /// Create a sub-`Formatter` with some styles changed. This may be useful in
    /// implementations of [`stylish::Display`] to dynamically configure how
    /// some parts are formatted.
    ///
    /// ```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 fn with(&mut self, restyle: impl Restyle) -> Formatter<'_> {
        Formatter {
            write: &mut *self.write,
            format: self.format,
            style: self.style.with(restyle),
        }
    }

    pub(crate) fn with_args<'b>(&'b mut self, format: &FormatterArgs<'b>) -> Formatter<'b> {
        Formatter {
            write: &mut *self.write,
            format: *format,
            style: self.style,
        }
    }

    /// Writes some data to the underlying output stream, using the current
    /// style.
    ///
    /// ```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 fn write_str(&mut self, s: &str) -> Result {
        self.write.write_str(s, self.style)?;
        Ok(())
    }

    /// Writes some formatted data into this instance, overriding the current
    /// style as appropriate.
    ///
    /// ```rust
    /// struct Name(&'static str);
    ///
    /// impl stylish::Display for Name {
    ///     fn fmt(&self, f: &mut stylish::Formatter<'_>) -> stylish::Result {
    ///         match self.0 {
    ///             "Ferris" => f.write_fmt(stylish::format_args!("{:(fg=red)}", self.0)),
    ///             "Gorris" => f.write_fmt(stylish::format_args!("{:(fg=cyan)}", self.0)),
    ///             _ => f.write_fmt(stylish::format_args!("{}", 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 fn write_fmt(&mut self, args: Arguments<'_>) -> Result {
        args.fmt(self)?;
        Ok(())
    }
}

impl<'a> Write for Formatter<'a> {
    fn write_str(&mut self, s: &str, style: Style) -> Result {
        self.with(style).write_str(s)
    }

    fn write_fmt(&mut self, args: Arguments<'_>) -> Result {
        self.write_fmt(args)
    }
}

impl<'a> core::fmt::Write for Formatter<'a> {
    fn write_str(&mut self, s: &str) -> Result {
        self.write_str(s)
    }
}