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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// =============================================================================
// Copyright (c) 2025 - 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Whitelisted Serde container attributes for redacted serialization.
use syn::{
DeriveInput,
LitStr,
spanned::Spanned,
};
use crate::{
internal::SerdeContainerAttributeParser,
serde_enum_representation::SerdeEnumRepresentation,
serde_rename_rule::SerdeRenameRule,
};
/// Validated names, rename rules, and enum representation.
#[must_use]
pub(crate) struct SerdeContainerAttributes {
/// Serialized container name.
name: String,
/// Struct-field or enum-variant rename rule.
rename_all: Option<SerdeRenameRule>,
/// Enum variant-field rename rule.
rename_all_fields: Option<SerdeRenameRule>,
/// Validated enum representation.
representation: SerdeEnumRepresentation,
}
impl SerdeContainerAttributes {
/// Parses the safe serialization-only Serde container allowlist.
///
/// # Parameters
///
/// * `input` - Complete derive input carrying container attributes.
/// * `enabled` - Whether `#[redact(serde)]` requested parsing.
///
/// # Returns
///
/// Validated names, rename rules, and representation.
///
/// # Errors
///
/// Returns a targeted error for unsupported attributes, duplicates,
/// invalid representation combinations, or enum-only controls on structs.
#[inline(always)]
pub(crate) fn parse(
input: &DeriveInput,
enabled: bool,
) -> syn::Result<Self> {
SerdeContainerAttributeParser::parse(input, enabled)
}
/// Builds validated attributes from parser-owned container controls.
///
/// # Parameters
///
/// * `input` - Complete derive input carrying the container identity.
/// * `name` - Optional explicit serialized container name.
/// * `rename_all` - Optional struct-field or enum-variant rename rule.
/// * `rename_all_fields` - Optional enum variant-field rename rule.
/// * `tag` - Optional internal or adjacent enum tag.
/// * `content` - Optional adjacent enum content key.
/// * `untagged` - Optional bare untagged attribute path.
///
/// # Returns
///
/// Attributes with a validated enum representation.
///
/// # Errors
///
/// Returns the targeted representation validation error for incompatible
/// tag, content, or untagged controls.
pub(super) fn from_parts(
input: &DeriveInput,
name: Option<String>,
rename_all: Option<SerdeRenameRule>,
rename_all_fields: Option<SerdeRenameRule>,
tag: Option<LitStr>,
content: Option<LitStr>,
untagged: Option<syn::Path>,
) -> syn::Result<Self> {
let representation = representation(input, tag, content, untagged)?;
Ok(Self {
name: name.unwrap_or_else(|| input.ident.to_string()),
rename_all,
rename_all_fields,
representation,
})
}
/// Returns the serialized container name.
///
/// # Returns
///
/// An explicit `rename` or the Rust type identifier.
#[inline(always)]
pub(crate) fn name(&self) -> &str {
&self.name
}
/// Applies the struct field rename rule.
///
/// # Parameters
///
/// * `field_name` - Rust field identifier without a raw prefix.
///
/// # Returns
///
/// The serialized struct field name.
pub(crate) fn rename_struct_field(&self, field_name: &str) -> String {
self.rename_all.as_ref().map_or_else(
|| field_name.to_owned(),
|rule| rule.apply_to_field(field_name),
)
}
/// Applies the enum variant rename rule.
///
/// # Parameters
///
/// * `variant_name` - Rust variant identifier.
///
/// # Returns
///
/// The serialized variant name.
pub(crate) fn rename_variant(&self, variant_name: &str) -> String {
self.rename_all.as_ref().map_or_else(
|| variant_name.to_owned(),
|rule| rule.apply_to_variant(variant_name),
)
}
/// Applies the container-wide enum field rename rule.
///
/// # Parameters
///
/// * `field_name` - Rust field identifier without a raw prefix.
///
/// # Returns
///
/// The serialized variant field name.
pub(crate) fn rename_variant_field(&self, field_name: &str) -> String {
self.rename_all_fields.as_ref().map_or_else(
|| field_name.to_owned(),
|rule| rule.apply_to_field(field_name),
)
}
/// Returns the validated enum representation.
///
/// # Returns
///
/// Externally tagged, internally tagged, adjacently tagged, or untagged.
#[inline(always)]
pub(crate) const fn representation(&self) -> &SerdeEnumRepresentation {
&self.representation
}
}
/// Validates and selects one enum representation.
fn representation(
input: &DeriveInput,
tag: Option<LitStr>,
content: Option<LitStr>,
untagged: Option<syn::Path>,
) -> syn::Result<SerdeEnumRepresentation> {
if let Some(path) = untagged {
if tag.is_some() || content.is_some() {
return Err(syn::Error::new(
path.span(),
format!(
"Redact serde for `{}` cannot combine `untagged` with `tag` or `content`",
input.ident,
),
));
}
return Ok(SerdeEnumRepresentation::Untagged);
}
match (tag, content) {
(None, None) => Ok(SerdeEnumRepresentation::ExternallyTagged),
(None, Some(content)) => Err(syn::Error::new_spanned(
content,
format!(
"Redact serde for `{}` requires `tag` when `content` is present",
input.ident,
),
)),
(Some(tag), None) => {
Ok(SerdeEnumRepresentation::InternallyTagged { tag: tag.value() })
}
(Some(tag), Some(content)) => {
if tag.value() == content.value() {
return Err(syn::Error::new_spanned(
content,
format!(
"Redact serde for `{}` requires distinct `tag` and `content` names",
input.ident,
),
));
}
Ok(SerdeEnumRepresentation::AdjacentlyTagged {
tag: tag.value(),
content: content.value(),
})
}
}
}