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
// =============================================================================
// Copyright (c) 2025 - 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Whitelisted Serde variant attributes for redacted serialization.
use syn::{
LitStr,
Meta,
Token,
Variant,
};
use crate::serde_rename_rule::SerdeRenameRule;
/// Validated variant name, field rename rule, and skip state.
#[must_use]
pub(crate) struct SerdeVariantAttributes {
/// Explicit serialized variant name.
rename: Option<String>,
/// Variant-local named-field rename rule.
rename_all: Option<SerdeRenameRule>,
/// Whether serialization of this variant is forbidden.
skip: bool,
}
impl SerdeVariantAttributes {
/// Parses the safe serialization-only Serde variant allowlist.
///
/// # Parameters
///
/// * `variant` - Enum variant carrying helper attributes.
/// * `type_name` - Owning enum used in diagnostics.
/// * `enabled` - Whether `#[redact(serde)]` requested parsing.
///
/// # Returns
///
/// Validated rename and skip controls.
///
/// # Errors
///
/// Returns an error for malformed, repeated, or unsupported controls.
///
/// # Panics
///
/// Panics only if `syn` supplies a nested metadata path without any
/// segments, which violates the `ParseNestedMeta` path invariant.
pub(crate) fn parse(
variant: &Variant,
type_name: &syn::Ident,
enabled: bool,
) -> syn::Result<Self> {
let mut parsed = Self {
rename: None,
rename_all: None,
skip: false,
};
if !enabled {
return Ok(parsed);
}
for attribute in &variant.attrs {
if !attribute.path().is_ident("serde") {
continue;
}
let Meta::List(_) = &attribute.meta else {
return Err(syn::Error::new_spanned(
attribute,
format!(
"Redact serde for `{type_name}` variant `{}` expects `#[serde(...)]`",
variant.ident,
),
));
};
attribute.parse_nested_meta(|meta| {
if meta.path.is_ident("rename") {
if parsed.rename.is_some() {
return Err(meta.error(format!(
"Redact serde for `{type_name}` variant `{}` repeats `rename`",
variant.ident,
)));
}
let literal: LitStr = meta.value()?.parse()?;
parsed.rename = Some(literal.value());
} else if meta.path.is_ident("rename_all") {
if parsed.rename_all.is_some() {
return Err(meta.error(format!(
"Redact serde for `{type_name}` variant `{}` repeats `rename_all`",
variant.ident,
)));
}
let literal: LitStr = meta.value()?.parse()?;
parsed.rename_all = Some(SerdeRenameRule::parse(&literal)?);
} else if meta.path.is_ident("skip")
|| meta.path.is_ident("skip_serializing")
{
if meta.input.peek(Token![=])
|| meta.input.peek(syn::token::Paren)
{
return Err(meta.error(format!(
"Redact serde for `{type_name}` variant `{}` requires a bare skip attribute",
variant.ident,
)));
}
if parsed.skip {
return Err(meta.error(format!(
"Redact serde for `{type_name}` variant `{}` repeats a skip attribute",
variant.ident,
)));
}
parsed.skip = true;
} else {
let key = meta
.path
.segments
.last()
.expect("syn nested meta paths always contain a segment")
.ident
.to_string();
return Err(meta.error(format!(
"Redact serde for `{type_name}` variant `{}` does not support `{key}` because it can change value paths or bypass redaction; use only `rename`, `rename_all`, `skip`, or `skip_serializing`",
variant.ident,
)));
}
Ok(())
})?;
}
Ok(parsed)
}
/// Selects the serialized variant name.
///
/// # Parameters
///
/// * `default_name` - Name produced by the container rule.
///
/// # Returns
///
/// The explicit rename when present, otherwise `default_name`.
#[inline(always)]
pub(crate) fn rename_variant(&self, default_name: String) -> String {
self.rename.clone().unwrap_or(default_name)
}
/// Applies the variant-local field rule before a container fallback.
///
/// # Parameters
///
/// * `field_name` - Rust field identifier without a raw prefix.
/// * `container_name` - Name produced by `rename_all_fields`.
///
/// # Returns
///
/// The serialized field name.
#[inline]
pub(crate) fn rename_field(
&self,
field_name: &str,
container_name: String,
) -> String {
self.rename_all
.as_ref()
.map_or(container_name, |rule| rule.apply_to_field(field_name))
}
/// Returns whether selecting this variant must fail serialization.
///
/// # Returns
///
/// `true` for `skip` or `skip_serializing`.
#[must_use]
#[inline(always)]
pub(crate) const fn skip(&self) -> bool {
self.skip
}
}