qubit_redact/argv/redacted_argv.rs
1// =============================================================================
2// Copyright (c) 2025 - 2026 Haixing Hu.
3//
4// SPDX-License-Identifier: Apache-2.0
5//
6// Licensed under the Apache License, Version 2.0.
7// =============================================================================
8//! Log-safe rendering of a redacted argument vector.
9
10use std::{
11 borrow::Cow,
12 fmt::{
13 self,
14 Display,
15 Formatter,
16 },
17};
18
19use crate::{
20 InputOutputLimit,
21 LogSafeText,
22};
23
24use super::redacted_argv_builder::RedactedArgvBuilder;
25
26/// A redacted argv rendering that is safe for a single-line text log.
27#[must_use = "render the redacted argv instead of the original arguments"]
28#[derive(Debug, Clone, PartialEq, Eq)]
29pub struct RedactedArgv {
30 /// Escaped debug-style rendering of all argument tokens.
31 rendered: LogSafeText<'static>,
32}
33
34impl RedactedArgv {
35 /// Creates a bounded argv rendering builder for one diagnostic budget.
36 ///
37 /// # Parameters
38 ///
39 /// * `budget` - Input and output limits for the diagnostic rendering.
40 ///
41 /// # Returns
42 ///
43 /// An empty byte-bounded argv rendering builder.
44 #[inline]
45 pub(super) fn builder(budget: InputOutputLimit) -> RedactedArgvBuilder {
46 RedactedArgvBuilder::new(budget)
47 }
48
49 /// Creates an argv value from already escaped bounded output.
50 ///
51 /// # Parameters
52 ///
53 /// * `rendered` - Escaped debug-style argv rendering.
54 ///
55 /// # Returns
56 ///
57 /// A displayable argv value.
58 #[inline(always)]
59 pub(super) fn from_rendered(rendered: String) -> Self {
60 Self {
61 rendered: LogSafeText::from_escaped(Cow::Owned(rendered)),
62 }
63 }
64
65 /// Borrows the already escaped diagnostic representation.
66 ///
67 /// The returned text is safe to append through
68 /// [`crate::DiagnosticLogBuilder::push_safe`]. Callers remain responsible
69 /// for applying any enclosing output budget.
70 #[inline(always)]
71 pub const fn as_log_safe_text(&self) -> &LogSafeText<'static> {
72 &self.rendered
73 }
74}
75
76impl Display for RedactedArgv {
77 /// Writes the complete escaped argv rendering.
78 ///
79 /// # Parameters
80 ///
81 /// * `formatter` - Destination formatting context.
82 ///
83 /// # Returns
84 ///
85 /// The formatter result from writing the complete rendering.
86 ///
87 /// # Errors
88 ///
89 /// Returns [`fmt::Error`] when the destination formatter rejects output.
90 #[inline(always)]
91 fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
92 Display::fmt(&self.rendered, formatter)
93 }
94}