use std::fmt;
pub(in crate::policy) struct BoundedMaskWriter {
output: String,
max_bytes: usize,
}
impl BoundedMaskWriter {
pub(in crate::policy) fn new(max_bytes: usize) -> Self {
Self {
output: String::new(),
max_bytes,
}
}
pub(in crate::policy) fn finish(self) -> String {
self.output
}
}
impl fmt::Write for BoundedMaskWriter {
fn write_str(&mut self, value: &str) -> fmt::Result {
let remaining = self.max_bytes.saturating_sub(self.output.len());
let mut end = value.len().min(remaining);
while !value.is_char_boundary(end) {
end -= 1;
}
self.output.push_str(&value[..end]);
Ok(())
}
}