1use std::fmt;
4
5pub type Result<T> = std::result::Result<T, ParseError>;
7
8#[derive(Debug, Clone, PartialEq)]
10pub enum ParseError {
11 InvalidVersion(String),
13
14 MissingHeader(String),
16
17 InvalidParameter {
19 name: String,
20 value: String,
21 reason: String,
22 },
23
24 InvalidEquation {
26 line: usize,
27 equation: String,
28 reason: String,
29 },
30
31 MissingSection(String),
33
34 ParseFailed(String),
36
37 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}