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
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

#[cfg(feature = "std")]
use std::io::Write;

use crate::HTMLMinifierError;

/// Implement this trait to build a HTML writer.
pub trait HTMLWriter {
    fn push(&mut self, e: u8) -> Result<(), HTMLMinifierError>;
    fn push_bytes(&mut self, bytes: &[u8]) -> Result<(), HTMLMinifierError>;
}

#[cfg(not(feature = "std"))]
impl HTMLWriter for Vec<u8> {
    #[inline]
    fn push(&mut self, e: u8) -> Result<(), HTMLMinifierError> {
        self.push(e);

        Ok(())
    }

    #[inline]
    fn push_bytes(&mut self, bytes: &[u8]) -> Result<(), HTMLMinifierError> {
        self.extend_from_slice(bytes);

        Ok(())
    }
}

#[cfg(feature = "std")]
impl<W: Write> HTMLWriter for W {
    #[inline]
    fn push(&mut self, e: u8) -> Result<(), HTMLMinifierError> {
        Ok(self.write_all(&[e])?)
    }

    #[inline]
    fn push_bytes(&mut self, bytes: &[u8]) -> Result<(), HTMLMinifierError> {
        Ok(self.write_all(bytes)?)
    }
}