onedrop_parser/
error.rs

1//! Error types for the onedrop-parser crate.
2
3use std::fmt;
4
5/// Result type alias for onedrop-parser operations.
6pub type Result<T> = std::result::Result<T, ParseError>;
7
8/// Errors that can occur during preset parsing.
9#[derive(Debug, Clone, PartialEq)]
10pub enum ParseError {
11    /// Invalid preset version number
12    InvalidVersion(String),
13
14    /// Missing required header
15    MissingHeader(String),
16
17    /// Invalid parameter value
18    InvalidParameter {
19        name: String,
20        value: String,
21        reason: String,
22    },
23
24    /// Invalid equation syntax
25    InvalidEquation {
26        line: usize,
27        equation: String,
28        reason: String,
29    },
30
31    /// Missing required section
32    MissingSection(String),
33
34    /// Generic parsing error
35    ParseFailed(String),
36
37    /// IO error
38    IoError(String),
39}
40
41impl fmt::Display for ParseError {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        match self {
44            ParseError::InvalidVersion(v) => {
45                write!(f, "Invalid preset version: {}", v)
46            }
47            ParseError::MissingHeader(h) => {
48                write!(f, "Missing required header: {}", h)
49            }
50            ParseError::InvalidParameter {
51                name,
52                value,
53                reason,
54            } => {
55                write!(
56                    f,
57                    "Invalid parameter '{}' with value '{}': {}",
58                    name, value, reason
59                )
60            }
61            ParseError::InvalidEquation {
62                line,
63                equation,
64                reason,
65            } => {
66                write!(
67                    f,
68                    "Invalid equation at line {}: '{}' - {}",
69                    line, equation, reason
70                )
71            }
72            ParseError::MissingSection(s) => {
73                write!(f, "Missing required section: {}", s)
74            }
75            ParseError::ParseFailed(msg) => {
76                write!(f, "Parse failed: {}", msg)
77            }
78            ParseError::IoError(msg) => {
79                write!(f, "IO error: {}", msg)
80            }
81        }
82    }
83}
84
85impl std::error::Error for ParseError {}
86
87impl From<std::io::Error> for ParseError {
88    fn from(err: std::io::Error) -> Self {
89        ParseError::IoError(err.to_string())
90    }
91}