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
// =============================================================================
// Copyright (c) 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Borrowed redaction projections for optional nested values.
use serde::Serialize;
use serde::Serializer;
use serde::ser::Error as SerdeError;
use super::redact_serialize::RedactSerialize;
use super::redact_serialize_scope::current_policy;
use super::redact_serialize_source::RedactSerializeSource;
use super::redacted_serialize_ref::RedactedSerializeRef;
use crate::RedactionPolicy;
/// Borrows an optional container for structured redacted field traversal.
///
/// # Type Parameters
///
/// * `'a`: The source borrow lifetime.
/// * `T`: The nested source type.
pub struct OptionProjection<'a, T>(
/// The optional source borrowed for this traversal.
&'a Option<T>,
);
impl<T> RedactSerializeSource for Option<T> {
/// A projection borrowing the source container.
type RedactedFields<'a>
= OptionProjection<'a, T>
where
Self: 'a;
/// Borrows the source; the caller must establish a serialization scope.
#[inline(always)]
fn redacted_fields<'a>(&'a self, _policy: &RedactionPolicy) -> Self::RedactedFields<'a> {
OptionProjection(self)
}
}
impl<'value, T> Serialize for OptionProjection<'value, T>
where
T: RedactSerialize,
{
/// Serializes the container within the current redaction scope.
///
/// # Type Parameters
///
/// * `S`: The destination serializer.
///
/// # Parameters
///
/// * `serializer`: The destination for the redacted representation.
///
/// # Returns
///
/// The serializer's output after applying the active policy and budgets.
///
/// # Errors
///
/// Returns a value-free error when no redaction scope is active, or
/// propagates errors from the destination serializer.
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let policy_owner =
current_policy().ok_or_else(|| S::Error::custom("serialization requires an active redaction scope"))?;
let Self(value) = self;
match *value {
Some(value) => RedactedSerializeRef::new(value, policy_owner.as_ref()).serialize(serializer),
None => serializer.serialize_none(),
}
}
}