qubit_redact/policy/policy_error.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//! Errors reported while building a redaction policy.
9
10use std::error::Error;
11use std::fmt;
12
13use super::PolicyLocation;
14use super::Sensitivity;
15
16/// Error returned when a redaction policy contains an invalid rule.
17#[non_exhaustive]
18#[derive(Debug, Clone, PartialEq, Eq)]
19pub enum PolicyError {
20 /// A supplied field name is empty after canonicalization.
21 EmptyFieldName {
22 /// Location where the invalid field was configured.
23 location: PolicyLocation,
24 },
25 /// A fixed mask has an empty replacement at the indicated level.
26 EmptyFixedReplacement {
27 /// Location where the fixed mask was configured.
28 location: PolicyLocation,
29 /// Sensitivity level containing the invalid fixed mask.
30 level: Sensitivity,
31 },
32 /// The configured output ceiling cannot be represented by Rust's
33 /// collection allocators on this platform.
34 OutputLimitTooLarge {
35 /// Rejected configured output limit.
36 maximum: usize,
37 },
38}
39
40impl fmt::Display for PolicyError {
41 /// Formats a concise description of the invalid policy configuration.
42 ///
43 /// # Parameters
44 ///
45 /// * `formatter` - Destination formatting context.
46 ///
47 /// # Returns
48 ///
49 /// The formatter result from writing the error description.
50 ///
51 /// # Errors
52 ///
53 /// Returns [`fmt::Error`] when the destination rejects a write.
54 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
55 match self {
56 Self::EmptyFieldName { location } => {
57 write!(formatter, "field name is empty after canonicalization in {location}",)
58 }
59 Self::EmptyFixedReplacement { location, level } => write!(
60 formatter,
61 "fixed mask replacement for {level:?} sensitivity is empty in {location}",
62 ),
63 Self::OutputLimitTooLarge { maximum } => write!(
64 formatter,
65 "output limit {maximum} exceeds this platform's addressable collection capacity",
66 ),
67 }
68 }
69}
70
71impl Error for PolicyError {}