Skip to main content

qubit_redact/policy/field/
allow_rule.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//! Read-only views of configured allow rules.
9
10use super::FieldNameMatching;
11
12/// A borrowed canonical field name and the breadth of its allow rule.
13///
14/// # Type Parameters
15///
16/// * `'a` - Lifetime of the borrowed canonical field name.
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub struct AllowRule<'a> {
19    /// Canonical field name.
20    field: &'a str,
21    /// Whether the rule applies exactly or at token suffix boundaries.
22    matching: FieldNameMatching,
23}
24
25impl<'a> AllowRule<'a> {
26    /// Creates a borrowed allow-rule view.
27    ///
28    /// # Parameters
29    ///
30    /// * `field` - Canonical field name.
31    /// * `matching` - Breadth of the allow rule.
32    ///
33    /// # Returns
34    ///
35    /// A read-only view over the supplied rule.
36    #[must_use]
37    pub(super) const fn new(field: &'a str, matching: FieldNameMatching) -> Self {
38        Self { field, matching }
39    }
40
41    /// Returns the canonical field name.
42    ///
43    /// # Returns
44    ///
45    /// The canonical field name borrowed from the policy.
46    #[must_use]
47    #[inline(always)]
48    pub const fn field(&self) -> &'a str {
49        self.field
50    }
51
52    /// Returns the breadth of the allow rule.
53    ///
54    /// # Returns
55    ///
56    /// [`FieldNameMatching::Exact`] for an exact-only allow rule or
57    /// [`FieldNameMatching::ExactOrTokenSuffix`] for a suffix allow rule.
58    #[must_use]
59    #[inline(always)]
60    pub const fn matching(&self) -> FieldNameMatching {
61        self.matching
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::AllowRule;
68    use crate::policy::FieldNameMatching;
69
70    #[test]
71    fn accessors_preserve_the_allow_rule_view() {
72        let rule = AllowRule::new("request_id", FieldNameMatching::Exact);
73        assert_eq!(rule.field(), "request_id");
74        assert_eq!(rule.matching(), FieldNameMatching::Exact);
75    }
76}