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
// =============================================================================
// Copyright (c) 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Borrowed redaction projections for vectors.
use serde::Serialize;
use serde::Serializer;
use serde::ser::Error as SerdeError;
use serde::ser::SerializeSeq;
use super::redact_serialize::RedactSerialize;
use super::redact_serialize_scope::admit_collection_items;
use super::redact_serialize_scope::current_policy;
use super::redact_serialize_scope::serialize_payload;
use super::redact_serialize_scope::serialize_structured;
use super::redact_serialize_source::RedactSerializeSource;
use super::redacted_serialize_ref::RedactedSerializeRef;
use crate::RedactionPolicy;
use crate::Sensitivity;
/// Borrows a vector for structured redacted field traversal.
///
/// # Type Parameters
///
/// * `'a`: The source borrow lifetime.
/// * `T`: The nested source type.
pub struct VecProjection<'a, T>(
/// The elements borrowed for this traversal.
&'a [T],
);
impl<T> RedactSerializeSource for Vec<T> {
/// A projection borrowing the source container.
type RedactedFields<'a>
= VecProjection<'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> {
VecProjection(self)
}
}
impl<'value, T> Serialize for VecProjection<'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 policy = policy_owner.as_ref();
let Self(values) = self;
serialize_structured(serializer, policy, |serializer| {
if !admit_collection_items(values.len()) {
return serialize_payload(serializer, policy.masking().mask_opaque(Sensitivity::Secret));
}
let mut sequence = serializer.serialize_seq(Some(values.len()))?;
for value in *values {
sequence.serialize_element(&RedactedSerializeRef::new(value, policy))?;
}
sequence.end()
})
}
}