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