another_html_builder/
prelude.rs

1//! Set of extension implementations allowing to write to [std::fmt::Write] or [std::io::Write].
2
3/// Abstraction layer allowing not only to write to [std::fmt::Write] but also to [std::io::Write].
4pub trait WriterExt {
5    type Error: std::error::Error;
6
7    /// Function that will write any element that implement [std::fmt::Display].
8    fn write<E: std::fmt::Display>(&mut self, input: E) -> Result<(), Self::Error>;
9    fn write_str(&mut self, input: &str) -> Result<(), Self::Error>;
10    fn write_char(&mut self, input: char) -> Result<(), Self::Error>;
11}
12
13/// Wrapper for writer implementing [std::fmt::Write].
14pub struct FmtWriter<W>(pub W);
15
16impl<W: std::fmt::Write> WriterExt for FmtWriter<W> {
17    type Error = std::fmt::Error;
18    fn write<E: std::fmt::Display>(&mut self, input: E) -> Result<(), Self::Error> {
19        write!(&mut self.0, "{input}")
20    }
21
22    fn write_str(&mut self, input: &str) -> Result<(), Self::Error> {
23        self.0.write_str(input)
24    }
25
26    fn write_char(&mut self, input: char) -> Result<(), Self::Error> {
27        self.0.write_char(input)
28    }
29}
30
31/// Wrapper for writer implementing [std::io::Write].
32pub struct IoWriter<W>(pub W);
33
34impl<W: std::io::Write> WriterExt for IoWriter<W> {
35    type Error = std::io::Error;
36
37    fn write<E: std::fmt::Display>(&mut self, input: E) -> std::io::Result<()> {
38        write!(self.0, "{input}")
39    }
40
41    fn write_str(&mut self, input: &str) -> std::io::Result<()> {
42        write!(self.0, "{input}")
43    }
44
45    fn write_char(&mut self, input: char) -> std::io::Result<()> {
46        write!(self.0, "{input}")
47    }
48}