qubit-redact-derive 0.8.1

Derive macros for qubit-redact domain-object formatting
Documentation
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// =============================================================================
//    Copyright (c) 2025 - 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Borrowing `Redact` implementation generation.

use proc_macro2::TokenStream;
use quote::format_ident;
use quote::quote;
use quote::quote_spanned;
use syn::DeriveInput;
use syn::Field;
use syn::Ident;
use syn::Path;
use syn::Result;
use syn::parse_quote;
use syn::spanned::Spanned;

use super::assertions;
use super::format;
use crate::attributes::ContainerAttributes;
use crate::attributes::SerdeContainerAttributes;
use crate::model;
use crate::model::ContainerData;
use crate::model::FieldMode;
use crate::model::FieldsData;
use crate::model::VariantData;
use crate::serde;
/// Expands a struct or enum into its runtime `Redact` implementation.
///
/// # Parameters
///
/// * `input` - Parsed derive input whose generics and fields are preserved.
/// * `runtime` - Resolved path to the `qubit-redact` runtime crate.
///
/// # Returns
///
/// Generated borrowing redaction plus optional formatting and Serde tokens.
///
/// # Errors
///
/// Returns a targeted syntax error when container or field controls are
/// invalid or when Serde controls conflict with the input shape.
#[inline]
pub(crate) fn expand(input: &DeriveInput, runtime: &Path) -> Result<TokenStream> {
    let container_attributes = ContainerAttributes::parse(input)?;
    expand_with_container_attributes(input, runtime, container_attributes)
}

/// Generates the implementation from already-validated container controls.
///
/// # Parameters
///
/// * `input` - Complete derive input to expand.
/// * `runtime` - Resolved path to the `qubit-redact` runtime crate.
/// * `container_attributes` - Validated controls selected for the expansion.
///
/// # Returns
///
/// Generated implementations for the validated input.
///
/// # Errors
///
/// Returns a targeted syntax error when field, Serde, or capability controls
/// are incompatible with the input.
fn expand_with_container_attributes(
    input: &DeriveInput,
    runtime: &Path,
    container_attributes: ContainerAttributes,
) -> Result<TokenStream> {
    let mut normalized = input.clone();
    assertions::normalize_recursive_fields(&mut normalized);
    let input = &normalized;
    let model = model::parse(input, "Redact", true)?;
    let serde = Some(parse_quote!(#runtime::domain::internal::serde));
    let serde_container_attributes = SerdeContainerAttributes::parse(input, true)?;
    let serde_impl = serde::expand(
        input,
        runtime,
        serde.as_ref(),
        &serde_container_attributes,
        &model,
        container_attributes.serde_enabled(),
    )?;
    let mut redaction_generics = input.generics.clone();
    assertions::add_redact_bounds(&mut redaction_generics, &model, runtime);
    let original_count = input
        .generics
        .where_clause
        .as_ref()
        .map_or(0, |clause| clause.predicates.len());
    if let Some(clause) = &mut redaction_generics.where_clause {
        clause.predicates = clause
            .predicates
            .iter()
            .cloned()
            .enumerate()
            .flat_map(|(index, predicate)| {
                if index < original_count {
                    vec![predicate]
                } else {
                    assertions::non_recursive_predicates(input, predicate)
                }
            })
            .collect();
    }

    let write_body = match &model {
        ContainerData::Struct(fields) if container_attributes.transparent() => {
            writer_transparent_struct_body(fields, runtime)
        }
        ContainerData::Struct(fields) => writer_struct_body(&input.ident, fields, runtime),
        ContainerData::Enum(variants) => writer_enum_body(variants, runtime),
    };
    let format_impl = format::expand(input, runtime, &container_attributes, &redaction_generics);
    let name = &input.ident;
    let (impl_generics, type_generics, where_clause) = redaction_generics.split_for_impl();

    Ok(quote! {
        impl #impl_generics #runtime::Redact for #name #type_generics #where_clause {
            fn write_redacted(
                &self,
                writer: &mut #runtime::RedactionWriter<'_>,
            ) {
                #write_body
            }
        }
        #format_impl
        #serde_impl
    })
}

/// Generates one classified field without a nominal struct wrapper.
///
/// # Parameters
///
/// * `fields` - Validated single-field struct shape and field controls.
/// * `runtime` - Resolved runtime crate path.
///
/// # Returns
///
/// A transparent writer call containing the selected field operation.
///
/// # Panics
///
/// Panics if the input bypassed transparent single-field validation or carries
/// invalid map-level or keyed-field controls.
#[must_use]
fn writer_transparent_struct_body(fields: &FieldsData<'_>, runtime: &Path) -> TokenStream {
    let call = match fields {
        FieldsData::Named(fields) => {
            let field = fields.first().expect("transparent shape was validated");
            let identifier = field.identifier();
            let key_access = match field.attributes().mode() {
                FieldMode::KeyedBy(key) => Some(quote!(&self.#key)),
                _ => None,
            };
            let name = identifier.to_string();
            writer_field_call(
                field.field(),
                &name,
                field.attributes().mode(),
                quote!(&self.#identifier),
                key_access,
                runtime,
            )
        }
        FieldsData::Unnamed(fields) => {
            let field = fields.first().expect("transparent shape was validated");
            let index = field.index();
            let name = index.index.to_string();
            writer_field_call(
                field.field(),
                &name,
                field.attributes().mode(),
                quote!(&self.#index),
                None,
                runtime,
            )
        }
        FieldsData::Unit => {
            unreachable!("transparent unit structs are rejected")
        }
    };
    quote! {
        writer.transparent(|__fields| {
            #call
        });
    }
}

/// Generates a structured writer body for one struct.
///
/// # Parameters
///
/// * `type_name` - Rust type identifier used as the nominal label.
/// * `fields` - Validated named, tuple, or unit fields.
/// * `runtime` - Resolved runtime crate path.
///
/// # Returns
///
/// A record or tuple operation that writes each classified field.
///
/// # Panics
///
/// Panics if field controls bypassed map-level or keyed-field validation.
#[must_use]
fn writer_struct_body(type_name: &Ident, fields: &FieldsData<'_>, runtime: &Path) -> TokenStream {
    match fields {
        FieldsData::Named(fields) => {
            let calls = fields.iter().map(|field| {
                let identifier = field.identifier();
                let key_access = match field.attributes().mode() {
                    FieldMode::KeyedBy(key) => Some(quote!(&self.#key)),
                    _ => None,
                };
                writer_field_call(
                    field.field(),
                    &field.identifier().to_string(),
                    field.attributes().mode(),
                    quote!(&self.#identifier),
                    key_access,
                    runtime,
                )
            });
            quote! {
                writer.record(stringify!(#type_name), |__fields| {
                    #(#calls)*
                });
            }
        }
        FieldsData::Unnamed(fields) => {
            let calls = fields.iter().map(|field| {
                let index = field.index();
                writer_field_call(
                    field.field(),
                    &index.index.to_string(),
                    field.attributes().mode(),
                    quote!(&self.#index),
                    None,
                    runtime,
                )
            });
            quote! {
                writer.tuple(stringify!(#type_name), |__fields| {
                    #(#calls)*
                });
            }
        }
        FieldsData::Unit => {
            quote! { writer.record(stringify!(#type_name), |_| {}); }
        }
    }
}

/// Generates a structured writer match for one enum.
///
/// # Parameters
///
/// * `variants` - Validated variant shapes and their field controls.
/// * `runtime` - Resolved runtime crate path.
///
/// # Returns
///
/// A match expression that writes the selected variant and its fields.
///
/// # Panics
///
/// Panics if a keyed field has no validated sibling or a map-level control
/// lacks its required key level.
#[must_use]
fn writer_enum_body(variants: &[VariantData<'_>], runtime: &Path) -> TokenStream {
    if variants.is_empty() {
        return quote!(match *self {});
    }
    let arms = variants.iter().map(|variant| {
        let variant_name = &variant.variant().ident;
        match variant.fields() {
            FieldsData::Named(fields) => {
                let bindings = fields
                    .iter()
                    .enumerate()
                    .map(|(position, field)| {
                        format_ident!("__qubit_redact_field_{position}", span = field.field().span())
                    })
                    .collect::<Vec<_>>();
                let patterns = fields.iter().zip(&bindings).map(|(field, binding)| {
                    let identifier = field.identifier();
                    quote!(#identifier: #binding)
                });
                let calls = fields.iter().zip(&bindings).map(|(field, binding)| {
                    let identifier = field.identifier();
                    let field_name = identifier.to_string();
                    let key_access = match field.attributes().mode() {
                        FieldMode::KeyedBy(key) => {
                            let position = fields
                                .iter()
                                .position(|candidate| candidate.identifier() == key)
                                .expect("keyed_by sibling was validated");
                            let key_binding = &bindings[position];
                            Some(quote!(#key_binding))
                        }
                        _ => None,
                    };
                    writer_field_call(
                        field.field(),
                        &field_name,
                        field.attributes().mode(),
                        quote!(#binding),
                        key_access,
                        runtime,
                    )
                });
                quote! {
                    Self::#variant_name { #(#patterns),* } => {
                        writer.record(stringify!(#variant_name), |__fields| {
                            #(#calls)*
                        });
                    }
                }
            }
            FieldsData::Unnamed(fields) => {
                let bindings = fields
                    .iter()
                    .map(|field| {
                        format_ident!(
                            "__qubit_redact_field_{}",
                            field.index().index,
                            span = field.field().span(),
                        )
                    })
                    .collect::<Vec<_>>();
                let patterns = bindings.iter().map(|binding| quote!(#binding));
                let calls = fields.iter().zip(&bindings).map(|(field, binding)| {
                    let field_name = field.index().index.to_string();
                    writer_field_call(
                        field.field(),
                        &field_name,
                        field.attributes().mode(),
                        quote!(#binding),
                        None,
                        runtime,
                    )
                });
                quote! {
                    Self::#variant_name(#(#patterns),*) => {
                        writer.tuple(stringify!(#variant_name), |__fields| {
                            #(#calls)*
                        });
                    }
                }
            }
            FieldsData::Unit => {
                quote! { Self::#variant_name => writer.record(stringify!(#variant_name), |_| {}) }
            }
        }
    });
    quote! {
        match self {
            #(#arms),*
        }
    }
}

/// Generates one structured writer field call.
///
/// # Parameters
///
/// * `field` - Source field whose span is retained in generated diagnostics.
/// * `field_name` - Label presented to the runtime writer.
/// * `mode` - Validated field redaction mode.
/// * `value` - Borrowed field access expression.
/// * `key_access` - Sibling key access for keyed mode, or `None` for other
///   modes.
/// * `runtime` - Resolved runtime crate path.
///
/// # Returns
///
/// Span-preserving tokens for the selected field operation.
///
/// # Panics
///
/// Panics if map-level mode lacks its required key level or keyed mode lacks
/// its sibling access expression, both rejected during model validation.
#[must_use]
fn writer_field_call(
    field: &Field,
    field_name: &str,
    mode: &FieldMode,
    value: TokenStream,
    key_access: Option<TokenStream>,
    runtime: &Path,
) -> TokenStream {
    let call = match mode {
        FieldMode::Unmarked => {
            quote! { __fields.unmarked(#field_name, || #value); }
        }
        FieldMode::DisplayLevel(level) => {
            let level = level.runtime_tokens(runtime);
            quote! { __fields.sensitive_value_exact(#level, #field_name, &#runtime::domain::internal::DisplayValue::new(#value)); }
        }
        FieldMode::Level(level) => {
            let level = level.runtime_tokens(runtime);
            quote! { __fields.sensitive_value_exact(#level, #field_name, #value); }
        }
        FieldMode::Nested => quote! { __fields.nested(#field_name, #value); },
        FieldMode::Map => {
            quote! { __fields.map_value(#field_name, #value); }
        }
        FieldMode::MapLevels {
            key,
            value: value_level,
        } => {
            let key = key.as_ref().expect("map key level is required").runtime_tokens(runtime);
            let value_level = value_level
                .as_ref()
                .map(|level| {
                    let level = level.runtime_tokens(runtime);
                    quote!(Some(#level))
                })
                .unwrap_or_else(|| quote!(None));
            quote! { __fields.map_level_values(#field_name, #value, #key, #value_level); }
        }
        FieldMode::KeyedBy(_) => {
            let key = key_access.expect("keyed_by is available only for named fields");
            quote! { __fields.keyed_value(#field_name, #key, #value); }
        }
        FieldMode::Json => {
            quote! { __fields.json_text_value(#field_name, #value); }
        }
        FieldMode::Skip => quote! { __fields.skipped(#field_name, || #value); },
    };
    quote_spanned! {field.span()=> #call }
}