use std::io::Error;
use std::io::Result;
use std::io::Write;
pub(crate) struct PrefixErrorOutput {
bytes: Vec<u8>,
limit: usize,
}
impl PrefixErrorOutput {
#[must_use]
#[inline]
pub(crate) fn new(limit: usize) -> Self {
Self {
bytes: Vec::new(),
limit,
}
}
#[must_use]
#[inline]
pub(crate) fn bytes(&self) -> &[u8] {
&self.bytes
}
}
impl Write for PrefixErrorOutput {
fn write(&mut self, bytes: &[u8]) -> Result<usize> {
if bytes.is_empty() {
return Ok(0);
}
let remaining = self.limit.saturating_sub(self.bytes.len());
if remaining == 0 {
return Err(Error::other("scripted payload write failure"));
}
let accepted = remaining.min(bytes.len());
self.bytes.extend_from_slice(&bytes[..accepted]);
Ok(accepted)
}
#[inline]
fn flush(&mut self) -> Result<()> {
Ok(())
}
}