Skip to main content

qubit_redact/domain/internal/
nested.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//! Container implementations for explicit nested redaction.
9
10use crate::domain::Redact;
11use crate::domain::RedactionWriter;
12
13impl<T: Redact> Redact for Option<T> {
14    /// Writes the contained domain values while sharing the enclosing
15    /// structural budget.
16    fn write_redacted(&self, writer: &mut RedactionWriter<'_>) {
17        match self {
18            None => writer.literal("None"),
19            Some(value) => writer.tuple("Some", |fields| {
20                fields.nested("", value);
21            }),
22        }
23    }
24}
25
26impl<T: Redact + ?Sized> Redact for Box<T> {
27    /// Writes the contained domain values while sharing the enclosing
28    /// structural budget.
29    fn write_redacted(&self, writer: &mut RedactionWriter<'_>) {
30        self.as_ref().write_redacted(writer)
31    }
32}
33
34impl<T: Redact> Redact for Vec<T> {
35    /// Writes the contained domain values while sharing the enclosing
36    /// structural budget.
37    fn write_redacted(&self, writer: &mut RedactionWriter<'_>) {
38        writer.sequence(|items| {
39            items.for_each(self, |items, value| {
40                items.nested_item(value);
41            });
42        });
43    }
44}
45
46impl<T: Redact, const N: usize> Redact for [T; N] {
47    /// Writes the contained domain values while sharing the enclosing
48    /// structural budget.
49    fn write_redacted(&self, writer: &mut RedactionWriter<'_>) {
50        writer.sequence(|items| {
51            items.for_each(self, |items, value| {
52                items.nested_item(value);
53            });
54        });
55    }
56}
57
58/// Generates nested tuple traversal for each supported arity.
59macro_rules! tuple_redact {
60    ($($name:ident),+ $(,)?) => {
61        impl<$($name: Redact),+> Redact for ($($name,)+) {
62            /// Traverses tuple fields through the active nested-value scope.
63            #[allow(non_snake_case)]
64            fn write_redacted(&self, writer: &mut RedactionWriter<'_>) {
65                let ($($name,)+) = self;
66                writer.tuple("Tuple", |fields| {
67                    $(fields.nested("", $name);)+
68                });
69            }
70        }
71    };
72}
73
74tuple_redact!(A);
75tuple_redact!(A, B);
76tuple_redact!(A, B, C);
77tuple_redact!(A, B, C, D);
78tuple_redact!(A, B, C, D, E);
79tuple_redact!(A, B, C, D, E, F);
80tuple_redact!(A, B, C, D, E, F, G);
81tuple_redact!(A, B, C, D, E, F, G, H);
82tuple_redact!(A, B, C, D, E, F, G, H, I);
83tuple_redact!(A, B, C, D, E, F, G, H, I, J);
84tuple_redact!(A, B, C, D, E, F, G, H, I, J, K);
85tuple_redact!(A, B, C, D, E, F, G, H, I, J, K, L);