qubit-redact 0.8.1

Rule-driven redaction for fields, diagnostics, HTTP data, and Rust domain objects
Documentation
// =============================================================================
//    Copyright (c) 2025 - 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Borrowed adapter for generated structured redaction.

use serde::Serialize;
use serde::Serializer;

use super::redact_serialize::RedactSerialize;
use super::redact_serialize_scope::RedactSerializeScope;

/// Borrowed serializer adapter that carries one immutable redaction policy.
#[doc(hidden)]
pub struct RedactedSerializeRef<'value, 'policy, T: ?Sized> {
    /// Borrowed source value.
    value: &'value T,
    /// Policy used by generated serialization.
    policy: &'policy crate::RedactionPolicy,
}

impl<'value, 'policy, T: ?Sized> RedactedSerializeRef<'value, 'policy, T> {
    /// Creates a policy-carrying borrowed serializer adapter.
    #[must_use]
    #[inline(always)]
    pub fn new(value: &'value T, policy: &'policy crate::RedactionPolicy) -> Self {
        Self { value, policy }
    }
}

impl<T: ?Sized + RedactSerialize> Serialize for RedactedSerializeRef<'_, '_, T> {
    /// Runs this borrowed adapter under the shared policy and resource scope.
    ///
    /// # Errors
    ///
    /// Propagates admission failures and errors from the downstream serializer.
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let _scope = RedactSerializeScope::new(self.policy);
        self.value.serialize_redacted(serializer, self.policy)
    }
}