Skip to main content

fop_types/
error.rs

1//! Error types for FOP
2
3use thiserror::Error;
4
5/// Result type alias for FOP operations
6pub type Result<T> = std::result::Result<T, FopError>;
7
8/// Location information for errors
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub struct Location {
11    pub line: usize,
12    pub column: usize,
13}
14
15impl Location {
16    pub fn new(line: usize, column: usize) -> Self {
17        Self { line, column }
18    }
19
20    pub fn unknown() -> Self {
21        Self { line: 0, column: 0 }
22    }
23}
24
25impl std::fmt::Display for Location {
26    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27        if self.line == 0 && self.column == 0 {
28            write!(f, "unknown location")
29        } else {
30            write!(f, "line {}, column {}", self.line, self.column)
31        }
32    }
33}
34
35/// Base error type for FOP operations
36#[derive(Error, Debug)]
37pub enum FopError {
38    /// XML parsing error with location
39    #[error("XML parsing error at {location}: {message}")]
40    XmlErrorWithLocation {
41        message: String,
42        location: Location,
43        suggestion: Option<String>,
44    },
45
46    /// XML parsing error (without location)
47    #[error("XML parsing error: {0}")]
48    XmlError(String),
49
50    /// Entity resolution error
51    #[error("Entity resolution error at {location}: {message}")]
52    EntityError { message: String, location: Location },
53
54    /// Invalid property value
55    #[error("Invalid property value for {property}: {value}")]
56    InvalidPropertyValue { property: String, value: String },
57
58    /// Unknown property
59    #[error("Unknown property: {0}")]
60    UnknownProperty(String),
61
62    /// Invalid element
63    #[error("Invalid element: {0}")]
64    InvalidElement(String),
65
66    /// Element nesting error
67    #[error("Invalid element nesting: {child} cannot be a child of {parent}")]
68    InvalidNesting { parent: String, child: String },
69
70    /// Missing required attribute
71    #[error("Missing required attribute {attribute} on element {element}")]
72    MissingAttribute { element: String, attribute: String },
73
74    /// Property validation error
75    #[error("Property validation error for {property}: {value} - {reason}")]
76    PropertyValidation {
77        property: String,
78        value: String,
79        reason: String,
80    },
81
82    /// I/O error
83    #[error("I/O error: {0}")]
84    IoError(#[from] std::io::Error),
85
86    /// Parse error
87    #[error("Parse error: {0}")]
88    ParseError(String),
89
90    /// Generic error
91    #[error("{0}")]
92    Generic(String),
93}