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
// =============================================================================
// Copyright (c) 2025 - 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Strict parsing boundary for container-level `redact` attributes.
use syn::{
DeriveInput,
Meta,
Token,
};
/// Parsed container controls for optional redacted serde integration.
#[must_use]
pub(crate) struct ContainerAttributes {
/// Whether the original type should receive a redacted `Debug` impl.
debug: bool,
/// Whether the original type should receive a redacted `Display` impl.
display: bool,
/// Whether redacted serde integration was requested.
serde: bool,
/// Whether every field must declare an explicit redaction mode.
require_explicit: bool,
}
impl ContainerAttributes {
/// Parses and validates container-level attributes on `input`.
///
/// # Parameters
///
/// * `input` - Complete derive input whose container attributes are read.
///
/// # Returns
///
/// Validated serde enablement and optional rename rule.
///
/// # Errors
///
/// Returns a targeted error for malformed, repeated, or unsupported
/// container controls.
pub(crate) fn parse(input: &DeriveInput) -> syn::Result<Self> {
let mut debug = false;
let mut display = false;
let mut serde = false;
let mut require_explicit = false;
for attribute in &input.attrs {
if !attribute.path().is_ident("redact") {
continue;
}
let Meta::List(list) = &attribute.meta else {
return Err(syn::Error::new_spanned(
attribute,
format!(
"Redact derive for `{}` expects `#[redact(debug, display, serde, require_explicit)]` on the container",
input.ident,
),
));
};
if list.tokens.is_empty() {
return Err(syn::Error::new_spanned(
attribute,
format!(
"Redact derive for `{}` does not allow an empty container attribute; use \
`#[redact(debug)]`, `#[redact(display)]`, `#[redact(serde)]`, or \
`#[redact(require_explicit)]`",
input.ident,
),
));
}
attribute.parse_nested_meta(|meta| {
let option = if meta.path.is_ident("debug") {
&mut debug
} else if meta.path.is_ident("display") {
&mut display
} else if meta.path.is_ident("serde") {
&mut serde
} else if meta.path.is_ident("require_explicit") {
&mut require_explicit
} else {
return Err(meta.error(format!(
"Redact derive for `{}` has unknown container attribute; use \
`debug`, `display`, `serde`, or `require_explicit`",
input.ident,
)));
};
if meta.input.peek(Token![=]) || meta.input.peek(syn::token::Paren) {
let name = meta
.path
.segments
.last()
.map_or("option".to_owned(), |segment| segment.ident.to_string());
return Err(meta.error(format!(
"Redact derive for `{}` requires bare `{name}` without arguments",
input.ident
)));
}
if *option {
let name = meta
.path
.segments
.last()
.map_or("option".to_owned(), |segment| segment.ident.to_string());
return Err(meta.error(format!(
"Redact derive for `{}` repeats the `{name}` container option",
input.ident
)));
}
*option = true;
Ok(())
})?;
}
Ok(Self {
debug,
display,
serde,
require_explicit,
})
}
/// Returns whether this struct requested a redacted `Debug` impl.
///
/// # Returns
///
/// `true` when the `debug` container option was present.
#[must_use]
#[inline(always)]
pub(crate) const fn debug_enabled(&self) -> bool {
self.debug
}
/// Returns whether this struct requested a redacted `Display` impl.
///
/// # Returns
///
/// `true` when the `display` container option was present.
#[must_use]
#[inline(always)]
pub(crate) const fn display_enabled(&self) -> bool {
self.display
}
/// Returns whether this struct requested redacted serialization.
///
/// # Returns
///
/// `true` when the `serde` container option was present.
#[must_use]
#[inline(always)]
pub(crate) const fn serde_enabled(&self) -> bool {
self.serde
}
/// Returns whether every field must select an explicit redaction mode.
///
/// # Returns
///
/// `true` when the `require_explicit` container option was present.
#[must_use]
#[inline(always)]
pub(crate) const fn require_explicit(&self) -> bool {
self.require_explicit
}
}