use super::markers;
use crate::RedactionCompletion;
use crate::RedactionReason;
use crate::runtime::OperationSink;
pub(in crate::formats::http) struct BoundedLogWriter {
sink: OperationSink,
}
impl BoundedLogWriter {
pub(in crate::formats::http) fn new(max_bytes: usize, source_truncated: bool) -> Self {
Self {
sink: OperationSink::new(max_bytes, markers::TRUNCATED, source_truncated),
}
}
pub(in crate::formats::http) fn write_str(&mut self, value: &str) -> std::fmt::Result {
if self.sink.is_full() {
return Ok(());
}
let value = if self.sink.is_truncated() {
value.strip_suffix(markers::TRUNCATED).unwrap_or(value)
} else {
value
};
self.sink.write_log_safe(value)
}
#[must_use]
#[inline(always)]
pub(in crate::formats::http) fn is_full(&self) -> bool {
self.sink.is_full()
}
#[must_use]
#[inline(always)]
pub(in crate::formats::http) fn remaining_bytes(&self) -> usize {
self.sink.remaining_bytes()
}
pub(in crate::formats::http) fn mark_truncated(&mut self) {
self.sink.mark_truncated();
}
#[must_use]
#[inline(always)]
pub(in crate::formats::http) fn is_output_truncated(&self) -> bool {
self.sink.output_truncated()
}
pub(in crate::formats::http) fn finish(self) -> (String, bool) {
let (text, completion, _) = self
.sink
.finish_with_reason(RedactionReason::OutputLimitReached)
.into_parts();
(text, completion != RedactionCompletion::Complete)
}
}