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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// =============================================================================
// Copyright (c) 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Runtime-owned bounded storage for one unpublished rendering operation.
use std::fmt;
use super::rendered_operation::RenderedOperation;
use crate::RedactionCompletion;
use crate::RedactionReason;
use crate::RedactionReasons;
/// Accumulates one log-safe rendering without exceeding its admitted allowance.
pub(crate) struct OperationSink {
/// Log-safe payload retained for the unpublished operation.
output: String,
/// Last byte boundary at which the truncation marker still fits.
marker_boundary: usize,
/// Maximum bytes the operation may retain, including its marker.
maximum: usize,
/// Static safe marker appended when payload output is omitted.
marker: &'static str,
/// Whether source metadata or rendering indicates omission.
truncated: bool,
/// Whether the output ceiling rejected any rendered text.
output_truncated: bool,
/// Strongest completion state accumulated by this operation.
completion: RedactionCompletion,
/// Machine-readable provenance accumulated by this operation.
reasons: RedactionReasons,
}
impl OperationSink {
/// Creates a complete operation through the runtime-owned result boundary.
///
/// # Parameters
///
/// - `text`: Already safe text bounded by the caller's format algorithm.
///
/// # Returns
///
/// A sink wrapping the supplied complete rendering.
#[must_use]
#[inline(always)]
pub(crate) fn complete(text: impl Into<String>) -> Self {
Self::from_rendered(text.into(), RedactionCompletion::Complete, RedactionReasons::empty())
}
/// Creates a complete operation with non-degrading provenance.
///
/// # Parameters
///
/// - `text`: Already safe text bounded by the caller's format algorithm.
/// - `reason`: Observed provenance for this rendering.
///
/// # Returns
///
/// A sink wrapping the supplied rendering and its completion facts.
#[cfg(any(feature = "json", feature = "http", feature = "uri"))]
#[must_use]
#[inline(always)]
pub(crate) fn complete_with_reason(text: impl Into<String>, reason: RedactionReason) -> Self {
Self::from_rendered(
text.into(),
RedactionCompletion::Complete,
RedactionReasons::empty().with(reason),
)
}
/// Creates a truncated operation through the runtime-owned result boundary.
///
/// # Parameters
///
/// - `text`: Already safe text bounded by the caller's format algorithm.
/// - `reason`: Observed provenance for this rendering.
///
/// # Returns
///
/// A truncated rendering, except empty text caused by `OutputLimitReached`
/// becomes exhausted. Other causes preserve truncated completion even
/// when their supplied text is empty.
#[must_use]
#[inline]
pub(crate) fn truncated(text: impl Into<String>, reason: RedactionReason) -> Self {
let text = text.into();
let completion = if text.is_empty() && reason == RedactionReason::OutputLimitReached {
RedactionCompletion::Exhausted
} else {
RedactionCompletion::Truncated
};
Self::from_rendered(text, completion, RedactionReasons::empty().with(reason))
}
/// Creates an exhausted operation through the runtime-owned result
/// boundary.
///
/// # Parameters
///
/// - `text`: Already safe text bounded by the caller's format algorithm.
///
/// # Returns
///
/// A sink wrapping the supplied exhausted rendering.
#[must_use]
#[inline(always)]
pub(crate) fn exhausted(text: impl Into<String>) -> Self {
Self::from_rendered(
text.into(),
RedactionCompletion::Exhausted,
RedactionReasons::empty().with(RedactionReason::OutputLimitReached),
)
}
/// Creates an empty sink with `maximum` final bytes and an optional marker.
///
/// # Parameters
///
/// - `maximum`: Final escaped byte limit, including a required marker.
/// - `marker`: Trusted static fallback marker.
/// - `source_truncated`: Whether the source already requires a final
/// marker.
///
/// # Returns
///
/// An empty bounded sink with independent source and output truncation
/// state.
#[must_use]
#[inline(always)]
pub(crate) fn new(maximum: usize, marker: &'static str, source_truncated: bool) -> Self {
Self {
output: String::new(),
marker_boundary: 0,
maximum,
marker,
truncated: source_truncated,
output_truncated: false,
completion: RedactionCompletion::Complete,
reasons: RedactionReasons::empty(),
}
}
/// Wraps text already bounded by a format algorithm for runtime
/// finalization.
///
/// # Parameters
///
/// - `text`: Already bounded, escaped payload.
/// - `completion`: Completion determined by the format algorithm.
/// - `reasons`: Observed rendering provenance.
///
/// # Returns
///
/// A sink retaining the existing payload without further marker insertion.
#[must_use]
#[inline]
fn from_rendered(text: String, completion: RedactionCompletion, reasons: RedactionReasons) -> Self {
let maximum = text.len();
Self {
output: text,
marker_boundary: maximum,
maximum,
marker: "",
truncated: false,
output_truncated: reasons.contains(RedactionReason::OutputLimitReached),
completion,
reasons,
}
}
/// Returns whether source or output omission requires a final marker.
///
/// # Returns
///
/// Whether source or output omission requires the configured marker.
#[must_use]
#[inline(always)]
#[cfg(feature = "http")]
pub(crate) const fn is_truncated(&self) -> bool {
self.truncated
}
/// Returns whether additional payload cannot affect the final output.
///
/// # Returns
///
/// Whether later payload can no longer change the retained output.
#[must_use]
#[inline(always)]
pub(crate) fn is_full(&self) -> bool {
self.output_truncated || (self.truncated && self.output.len() >= self.payload_limit())
}
/// Returns the remaining payload bytes before a required marker.
///
/// # Returns
///
/// Remaining escaped payload bytes after any marker reservation.
#[must_use]
#[inline(always)]
#[cfg(feature = "http")]
pub(crate) fn remaining_bytes(&self) -> usize {
self.payload_limit().saturating_sub(self.output.len())
}
/// Returns whether the output allowance, rather than source provenance,
/// truncated text.
///
/// # Returns
///
/// Whether the output limit rejected payload.
#[must_use]
#[inline(always)]
#[cfg(feature = "http")]
pub(crate) const fn output_truncated(&self) -> bool {
self.output_truncated
}
/// Writes a complete already-safe atom, retaining it only when it fits.
///
/// # Parameters
///
/// - `atom`: One complete, already escaped output unit.
///
/// # Returns
///
/// `true` if the complete atom was retained; `false` records output closure
/// and discards any suffix needed to reserve the marker.
pub(crate) fn write_atom(&mut self, atom: &str) -> bool {
if self.is_full() || self.output.len().saturating_add(atom.len()) > self.payload_limit() {
self.truncate_for_output();
return false;
}
self.output.push_str(atom);
if self.output.len() <= self.maximum.saturating_sub(self.marker.len()) {
self.marker_boundary = self.output.len();
}
true
}
/// Writes text after applying the common log-control escape rules.
///
/// # Parameters
///
/// - `value`: Raw text to escape one Unicode scalar at a time.
///
/// # Errors
///
/// Propagates a character-encoding error. Capacity rejection is recorded in
/// the sink and stops iteration; it does not itself return an error here.
pub(crate) fn write_log_safe(&mut self, value: &str) -> fmt::Result {
for character in value.chars() {
let mut encoded = [0_u8; 12];
let atom = crate::output::log_escape::encode_log_safe_character(character, &mut encoded)?;
if !self.write_atom(atom) {
break;
}
}
Ok(())
}
/// Marks input or a nested renderer as truncated while reserving the
/// marker.
#[cfg(any(feature = "http", feature = "uri"))]
#[inline]
pub(crate) fn mark_truncated(&mut self) {
self.truncated = true;
self.output.truncate(self.marker_boundary);
}
/// Adds provenance without weakening the current completion state.
///
/// # Parameters
///
/// - `reason`: Additional non-degrading provenance.
///
/// # Returns
///
/// This sink with the reason unioned into its existing set.
#[must_use]
#[cfg(any(feature = "json", feature = "http", feature = "uri"))]
#[inline(always)]
pub(crate) fn with_reason(mut self, reason: RedactionReason) -> Self {
self.reasons = self.reasons.with(reason);
self
}
/// Finalizes a pre-rendered operation through the runtime-owned boundary.
///
/// # Returns
///
/// The pre-rendered operation; use `finish_with_reason` for an active
/// bounded sink.
#[must_use]
#[inline(always)]
pub(crate) fn finish(self) -> RenderedOperation {
RenderedOperation::from_parts(self.output, self.completion, self.reasons, self.output_truncated)
}
/// Finalizes bounded text and turns sink-owned state into one operation.
///
/// # Parameters
///
/// - `reason`: Cause used when source or output omission requires a marker.
///
/// # Returns
///
/// The bounded operation, preserving the original cause and adding an
/// output-limit reason and closure when the sink or marker cannot fit.
#[must_use]
pub(crate) fn finish_with_reason(mut self, reason: RedactionReason) -> RenderedOperation {
if self.truncated {
self.reasons = self.reasons.with(reason);
if self.output_truncated || self.maximum < self.marker.len() {
self.reasons = self.reasons.with(RedactionReason::OutputLimitReached);
}
if self.maximum < self.marker.len() {
self.output.clear();
return RenderedOperation::from_parts(self.output, RedactionCompletion::Exhausted, self.reasons, true);
}
self.output.truncate(self.marker_boundary);
self.output.push_str(self.marker);
let output_closed = self.output_truncated || reason == RedactionReason::OutputLimitReached;
return RenderedOperation::from_parts(
self.output,
RedactionCompletion::Truncated,
self.reasons,
output_closed,
);
}
RenderedOperation::from_parts(self.output, self.completion, self.reasons, self.output_truncated)
}
/// Returns the payload maximum after reserving a marker when necessary.
///
/// # Returns
///
/// The escaped byte ceiling for payload, excluding a required marker.
#[must_use]
#[inline(always)]
fn payload_limit(&self) -> usize {
if self.truncated {
self.maximum.saturating_sub(self.marker.len())
} else {
self.maximum
}
}
/// Records output overflow and preserves the last marker-safe boundary.
#[inline]
fn truncate_for_output(&mut self) {
self.truncated = true;
self.output_truncated = true;
self.output.truncate(self.marker_boundary);
}
}
impl fmt::Write for OperationSink {
/// Stops a formatter after its first rejected escaped output atom.
///
/// # Parameters
///
/// - `value`: Raw formatter text to escape and retain under the allowance.
///
/// # Errors
///
/// Returns `fmt::Error` on any output rejection, including a previous
/// rejection, or propagates an escaping error.
#[inline]
fn write_str(&mut self, value: &str) -> fmt::Result {
self.write_log_safe(value)?;
if self.output_truncated { Err(fmt::Error) } else { Ok(()) }
}
}