stylish_plain/io.rs
1//! Traits and associated types for writing [`stylish`] attributed data to
2//! fallible IO sinks by discarding style attributes.
3
4use stylish_core::{
5 io::{Result, Write},
6 Style,
7};
8
9/// An adaptor to allow writing [`stylish`] attributed data to an output stream
10/// by discarding style attributes.
11///
12/// ```rust
13/// let mut writer = stylish::io::Plain::new(Vec::new());
14/// stylish::write!(writer, "Hello {:(fg=red)}", "Ferris")?;
15/// assert_eq!(writer.into_inner(), b"Hello Ferris");
16/// # Ok::<(), std::io::Error>(())
17/// ```
18#[derive(Clone, Debug, Default)]
19pub struct Plain<T> {
20 inner: T,
21}
22
23impl<T: std::io::Write> Plain<T> {
24 /// Wrap the given output stream in this adaptor.
25 pub fn new(inner: T) -> Self {
26 Self { inner }
27 }
28
29 /// Inherent delegation to
30 /// [`stylish::io::Write::write_fmt`](stylish_core::io::Write::write_fmt) to
31 /// not require a trait import.
32 pub fn write_fmt(&mut self, args: stylish_core::Arguments<'_>) -> Result<()> {
33 stylish_core::io::Write::write_fmt(self, args)
34 }
35
36 /// Get back the wrapped output stream.
37 pub fn into_inner(self) -> T {
38 self.inner
39 }
40}
41
42impl<T: std::io::Write> Write for Plain<T> {
43 fn write(&mut self, s: &[u8], _style: Style) -> Result<usize> {
44 self.inner.write(s)
45 }
46
47 fn flush(&mut self) -> Result<()> {
48 self.inner.flush()
49 }
50
51 fn write_all(&mut self, s: &[u8], _style: Style) -> Result<()> {
52 self.inner.write_all(s)
53 }
54}