use crate::Error;
use minicbor::Encode;
use std::io;
#[derive(Debug)]
pub struct Writer<W> {
writer: W,
buffer: Vec<u8>,
max_len: usize
}
impl<W> Writer<W> {
pub fn new(writer: W) -> Self {
Self::with_buffer(writer, Vec::new())
}
pub fn with_buffer(writer: W, buffer: Vec<u8>) -> Self {
Self { writer, buffer, max_len: 512 * 1024 }
}
pub fn set_max_len(&mut self, val: u32) {
self.max_len = val as usize
}
pub fn writer(&self) -> &W {
&self.writer
}
pub fn writer_mut(&mut self) -> &mut W {
&mut self.writer
}
pub fn into_parts(self) -> (W, Vec<u8>) {
(self.writer, self.buffer)
}
}
impl<W: io::Write> Writer<W> {
pub fn write<T: Encode<()>>(&mut self, val: T) -> Result<usize, Error> {
self.write_with(val, &mut ())
}
pub fn write_with<C, T: Encode<C>>(&mut self, val: T, ctx: &mut C) -> Result<usize, Error> {
self.buffer.resize(4, 0u8);
minicbor::encode_with(val, &mut self.buffer, ctx)?;
if self.buffer.len() - 4 > self.max_len {
return Err(Error::InvalidLen)
}
let prefix = (self.buffer.len() as u32 - 4).to_be_bytes();
self.buffer[.. 4].copy_from_slice(&prefix);
self.writer.write_all(&self.buffer)?;
Ok(self.buffer.len() - 4)
}
pub fn flush(&mut self) -> Result<(), Error> {
self.writer.flush()?;
Ok(())
}
}