Skip to main content

qubit_redact/policy/redaction_policy_builder/
http_policy_builder_view.rs

1// =============================================================================
2//    Copyright (c) 2025 - 2026 Haixing Hu.
3//
4//    SPDX-License-Identifier: Apache-2.0
5//
6//    Licensed under the Apache License, Version 2.0.
7// =============================================================================
8//! Transactional view over HTTP policy contexts.
9
10use super::HttpContextBuilderView;
11use super::PolicyError;
12use super::RedactionFloor;
13use super::RedactionPolicyBuilder;
14
15/// Mutable view over all HTTP context differences.
16///
17/// # Examples
18///
19/// ```
20/// use qubit_redact::RedactionPolicy;
21/// use qubit_redact::formats::http::UrlPathPolicy;
22///
23/// let policy = RedactionPolicy::builder().http(|http| {
24///     http.url_path(UrlPathPolicy::Redact);
25/// })?.build()?;
26/// assert!(!policy.is_disabled());
27/// # Ok::<(), qubit_redact::PolicyError>(())
28/// ```
29#[cfg(feature = "http")]
30pub struct HttpPolicyBuilderView<'a> {
31    /// Root builder receiving HTTP policy changes.
32    pub(super) builder: &'a mut RedactionPolicyBuilder,
33    /// First validation error recorded by the transactional view.
34    pub(super) error: Option<PolicyError>,
35}
36
37#[cfg(feature = "http")]
38impl HttpPolicyBuilderView<'_> {
39    /// Returns the header context view.
40    #[must_use]
41    #[inline(always)]
42    pub fn header(&mut self) -> HttpContextBuilderView<'_> {
43        HttpContextBuilderView {
44            builder: &mut self.builder.http,
45            error: &mut self.error,
46            context: crate::formats::http::HttpFieldContext::Header,
47        }
48    }
49
50    /// Returns the query/form context view.
51    #[must_use]
52    #[inline(always)]
53    pub fn query(&mut self) -> HttpContextBuilderView<'_> {
54        HttpContextBuilderView {
55            builder: &mut self.builder.http,
56            error: &mut self.error,
57            context: crate::formats::http::HttpFieldContext::Query,
58        }
59    }
60
61    /// Returns the structured-body context view.
62    #[must_use]
63    #[inline(always)]
64    pub fn body(&mut self) -> HttpContextBuilderView<'_> {
65        HttpContextBuilderView {
66            builder: &mut self.builder.http,
67            error: &mut self.error,
68            context: crate::formats::http::HttpFieldContext::Body,
69        }
70    }
71
72    /// Sets URL path visibility for HTTP diagnostics.
73    #[inline(always)]
74    pub fn url_path(&mut self, policy: crate::formats::http::UrlPathPolicy) -> &mut Self {
75        self.builder.http.url_path_mut(policy);
76        self
77    }
78
79    /// Sets opaque text-body visibility for HTTP diagnostics.
80    #[inline(always)]
81    pub fn text_body(&mut self, policy: crate::formats::http::TextBodyPolicy) -> &mut Self {
82        self.builder.http.text_body_mut(policy);
83        self
84    }
85
86    /// Sets the same floor for every HTTP field context.
87    #[inline(always)]
88    pub fn floor_all(&mut self, floor: RedactionFloor) -> &mut Self {
89        self.builder.http.floor_all_mut(floor);
90        self
91    }
92
93    /// Disables every HTTP field-context floor explicitly.
94    #[inline(always)]
95    pub fn disable_all_floors(&mut self) -> &mut Self {
96        self.builder.http.disable_all_floors_mut();
97        self
98    }
99
100    /// Sets the handling of root and array JSON scalar values in HTTP
101    /// bodies.
102    #[inline(always)]
103    pub fn unkeyed_json(&mut self, policy: crate::UnkeyedJsonValuePolicy) -> &mut Self {
104        self.builder.unkeyed_json_value_policy = policy;
105        self
106    }
107}