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