llm_tool/types/truncation/outcome.rs
1//! Detailed truncation outcome capturing metadata and recall references.
2
3use alloc::borrow::Cow;
4
5use super::sink::SpillRef;
6
7/// Detailed outcome of a middle-out string truncation operation.
8///
9/// Preserves metadata including original length, omitted byte range, and
10/// an optional [`SpillRef`] handle if an [`crate::OutputSpillSink`] was configured.
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct TruncationOutcome<'a> {
13 /// The truncated or original text.
14 pub text: Cow<'a, str>,
15 /// Byte length of the original input before truncation.
16 pub original_len: usize,
17 /// Byte range of the ORIGINAL input that was omitted.
18 ///
19 /// `None` when untruncated.
20 pub omitted: Option<core::ops::Range<usize>>,
21 /// Pluggable spill reference handle if full output was retained by an [`crate::OutputSpillSink`].
22 pub spill_ref: Option<SpillRef>,
23}
24
25impl<'a> TruncationOutcome<'a> {
26 /// Returns `true` if content was omitted during truncation.
27 #[must_use]
28 pub const fn is_truncated(&self) -> bool {
29 self.omitted.is_some()
30 }
31
32 /// Number of bytes omitted from the original input.
33 #[must_use]
34 pub const fn omitted_bytes(&self) -> usize {
35 match &self.omitted {
36 Some(range) => range.end.saturating_sub(range.start),
37 None => 0,
38 }
39 }
40
41 /// Consume this outcome and return the resulting text.
42 #[must_use]
43 pub fn into_text(self) -> Cow<'a, str> {
44 self.text
45 }
46}