Skip to main content

formatparse_core/
error.rs

1//! Error types for formatparse-core
2//!
3//! Pure Rust error types (no PyO3 dependencies)
4
5use std::fmt;
6
7/// Errors that can occur during pattern parsing or matching
8#[derive(Debug, Clone)]
9pub enum FormatParseError {
10    /// Pattern parsing error
11    PatternError(String),
12    /// Regex compilation error
13    RegexError(String),
14    /// Type conversion error
15    ConversionError(String, String), // (value, target_type)
16    /// Repeated field name with mismatched types
17    RepeatedNameError(String),
18    /// Custom type validation error
19    CustomTypeError(String, String), // (type_name, message)
20    /// Regex group index error
21    RegexGroupIndexError(String, usize, i64), // (type_name, actual, expected)
22    /// Feature not implemented
23    NotImplementedError(String),
24    /// Missing required field
25    MissingFieldError(String),
26}
27
28impl fmt::Display for FormatParseError {
29    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30        match self {
31            FormatParseError::PatternError(msg) => write!(f, "Pattern error: {}", msg),
32            FormatParseError::RegexError(msg) => write!(f, "Invalid regex pattern: {}", msg),
33            FormatParseError::ConversionError(value, target_type) => {
34                write!(f, "Invalid {}: {}", target_type, value)
35            }
36            FormatParseError::RepeatedNameError(name) => {
37                write!(f, "Repeated name '{}' with mismatched types", name)
38            }
39            FormatParseError::CustomTypeError(type_name, msg) => {
40                write!(f, "Custom type '{}' error: {}", type_name, msg)
41            }
42            FormatParseError::RegexGroupIndexError(type_name, actual, expected) => {
43                write!(
44                    f,
45                    "Custom type '{}' pattern has {} capturing groups but regex_group_count is {}",
46                    type_name, actual, expected
47                )
48            }
49            FormatParseError::NotImplementedError(feature) => {
50                write!(f, "{} is not supported", feature)
51            }
52            FormatParseError::MissingFieldError(field) => {
53                write!(f, "Missing required field: {}", field)
54            }
55        }
56    }
57}
58
59impl std::error::Error for FormatParseError {}