Skip to main content

qubit_case/
case_style_validation_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//! # Case Style Validation Errors
9//!
10//! Defines errors returned when validating values against case styles.
11
12use std::fmt;
13
14use crate::CaseStyle;
15
16/// Error returned when a value does not match an expected case style.
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub struct CaseStyleValidationError {
19    style: CaseStyle,
20    value: String,
21}
22
23impl CaseStyleValidationError {
24    /// Creates a case style validation error.
25    ///
26    /// # Parameters
27    ///
28    /// * `style` - Expected case style.
29    /// * `value` - Original value that failed validation.
30    ///
31    /// # Returns
32    ///
33    /// Returns a new error carrying the expected style and original value.
34    #[inline]
35    pub fn new(style: CaseStyle, value: impl Into<String>) -> Self {
36        Self {
37            style,
38            value: value.into(),
39        }
40    }
41
42    /// Returns the expected case style.
43    ///
44    /// # Returns
45    ///
46    /// Returns the style used to validate the value.
47    #[inline]
48    pub const fn style(&self) -> CaseStyle {
49        self.style
50    }
51
52    /// Returns the invalid value.
53    ///
54    /// # Returns
55    ///
56    /// Returns the original value passed to `CaseStyle::validate` or
57    /// `CaseStyle::checked_to`.
58    #[inline]
59    pub fn value(&self) -> &str {
60        &self.value
61    }
62}
63
64impl fmt::Display for CaseStyleValidationError {
65    /// Formats the case style validation error.
66    ///
67    /// # Parameters
68    ///
69    /// * `f` - Formatter receiving the message.
70    ///
71    /// # Returns
72    ///
73    /// Returns the formatter result.
74    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75        write!(
76            f,
77            "Value '{}' does not match case style '{}'.",
78            self.value,
79            self.style.name(),
80        )
81    }
82}
83
84impl std::error::Error for CaseStyleValidationError {}