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
// =============================================================================
// Copyright (c) 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Owned adapter state for a borrowed Display value and policy.
use std::fmt::Display;
use crate::RedactionPolicy;
use crate::Sensitivity;
/// Keeps the textual adapter alive for the complete field serialization.
#[doc(hidden)]
pub struct RedactedDisplaySerializeRef<'value, 'policy, T: ?Sized> {
/// Borrowed raw field, never formatted at construction.
value: &'value T,
/// Parent serialization policy.
policy: &'policy RedactionPolicy,
/// Final level declared on the field.
level: Sensitivity,
}
impl<'value, 'policy, T: ?Sized> RedactedDisplaySerializeRef<'value, 'policy, T> {
/// Captures the raw reference and field decision without invoking Display.
///
/// # Parameters
///
/// - `value`: Borrowed source accessed only during serialization.
/// - `policy`: Immutable policy borrowed independently of the source.
/// - `level`: Explicit sensitivity applied to the textual scalar.
///
/// # Returns
///
/// A lazy adapter retaining the supplied source and policy references.
#[must_use]
#[inline(always)]
pub fn new(value: &'value T, policy: &'policy RedactionPolicy, level: Sensitivity) -> Self {
Self { value, policy, level }
}
}
impl<T: Display + ?Sized> serde::Serialize for RedactedDisplaySerializeRef<'_, '_, T> {
/// Executes one bounded textual scalar serialization.
///
/// # Errors
///
/// Propagates payload admission or downstream serialization failures.
///
/// # Type Parameters
///
/// - `S`: Downstream serializer defining the result and error types.
///
/// # Parameters
///
/// - `serializer`: Destination receiving the admitted representation.
///
/// # Returns
///
/// The downstream result after the admitted representation is serialized.
#[inline(always)]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
super::redact_level_serialize::serialize_display_text(self.value, serializer, self.policy, self.level)
}
}