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
//! Base64 writer support (constant-time).

use crate::{Result, Writer};

/// Inner constant-time Base64 reader type from the `base64ct` crate.
type Inner<'o> = base64ct::Encoder<'o, base64ct::Base64>;

/// Constant-time Base64 writer implementation.
pub struct Base64Writer<'o> {
    inner: Inner<'o>,
}

impl<'o> Base64Writer<'o> {
    /// Create a new Base64 writer which writes output to the given byte slice.
    ///
    /// Output constructed using this method is not line-wrapped.
    pub fn new(output: &'o mut [u8]) -> Result<Self> {
        Ok(Self {
            inner: Inner::new(output)?,
        })
    }

    /// Finish encoding data, returning the resulting Base64 as a `str`.
    pub fn finish(self) -> Result<&'o str> {
        Ok(self.inner.finish()?)
    }
}

impl Writer for Base64Writer<'_> {
    fn write(&mut self, bytes: &[u8]) -> Result<()> {
        Ok(self.inner.encode(bytes)?)
    }
}