Skip to main content

qubit_redact/http/
redacted_headers.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//! Safe rendered HTTP header values.
9
10use std::fmt::{
11    self,
12    Debug,
13    Display,
14    Formatter,
15};
16
17use crate::LogSafeText;
18
19/// Owns a deterministic log-safe rendering of an HTTP header map.
20#[must_use = "render or otherwise consume the redacted headers"]
21#[derive(Clone, PartialEq, Eq)]
22pub struct RedactedHeaders {
23    /// Sorted, escaped representation containing no unprocessed values.
24    text: LogSafeText<'static>,
25}
26
27impl RedactedHeaders {
28    /// Creates redacted headers from already escaped text.
29    ///
30    /// # Parameters
31    ///
32    /// * `text` - Complete log-safe rendering.
33    ///
34    /// # Returns
35    ///
36    /// An opaque safe header result.
37    #[inline(always)]
38    pub(super) const fn new(text: LogSafeText<'static>) -> Self {
39        Self { text }
40    }
41
42    /// Returns the complete safe rendering.
43    ///
44    /// # Returns
45    ///
46    /// A borrowed log-safe header representation.
47    #[inline]
48    pub const fn log_safe_text(&self) -> &LogSafeText<'static> {
49        &self.text
50    }
51
52    /// Consumes the result and returns its safe rendering.
53    ///
54    /// # Returns
55    ///
56    /// Owned log-safe header text.
57    #[inline]
58    pub fn into_log_safe_text(self) -> LogSafeText<'static> {
59        self.text
60    }
61}
62
63impl Display for RedactedHeaders {
64    /// Writes the safe header representation.
65    ///
66    /// # Parameters
67    ///
68    /// * `formatter` - Destination formatting context.
69    ///
70    /// # Returns
71    ///
72    /// The formatter result.
73    ///
74    /// # Errors
75    ///
76    /// Returns [`fmt::Error`] when the destination rejects a write.
77    #[inline]
78    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
79        Display::fmt(&self.text, formatter)
80    }
81}
82
83impl Debug for RedactedHeaders {
84    /// Writes only the safe header representation.
85    ///
86    /// # Parameters
87    ///
88    /// * `formatter` - Destination formatting context.
89    ///
90    /// # Returns
91    ///
92    /// The formatter result.
93    ///
94    /// # Errors
95    ///
96    /// Returns [`fmt::Error`] when the destination rejects a write.
97    #[inline]
98    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
99        formatter
100            .debug_tuple("RedactedHeaders")
101            .field(&self.text)
102            .finish()
103    }
104}