1use pest::error::Error as PestError;
4use std::io;
5
6#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum VmfError {
9 Io(io::ErrorKind),
11 Parse(Box<PestError<crate::parser::Rule>>),
13 InvalidFormat(String),
15 ParseInt(std::num::ParseIntError, String),
17 ParseFloat(std::num::ParseFloatError, String),
19}
20
21impl std::fmt::Display for VmfError {
22 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
23 match self {
24 VmfError::Io(err) => write!(f, "IO error: {}", err),
25 VmfError::Parse(err) => write!(f, "Parse error: {}", err),
26 VmfError::InvalidFormat(msg) => write!(f, "Invalid format: {}", msg),
27 VmfError::ParseInt(err, key) => {
28 write!(f, "Integer parse error in key '{}': {}", key, err)
29 }
30 VmfError::ParseFloat(err, key) => {
31 write!(f, "Float parse error in key '{}': {}", key, err)
32 }
33 }
34 }
35}
36
37impl std::error::Error for VmfError {}
38
39impl From<io::Error> for VmfError {
40 fn from(err: io::Error) -> Self {
41 VmfError::Io(err.kind())
42 }
43}
44
45impl From<PestError<crate::parser::Rule>> for VmfError {
46 fn from(err: PestError<crate::parser::Rule>) -> Self {
47 VmfError::Parse(Box::new(err))
48 }
49}
50
51impl From<std::num::ParseIntError> for VmfError {
52 fn from(err: std::num::ParseIntError) -> Self {
53 VmfError::ParseInt(err, "".to_string())
54 }
55}
56
57impl From<std::num::ParseFloatError> for VmfError {
58 fn from(err: std::num::ParseFloatError) -> Self {
59 VmfError::ParseFloat(err, "".to_string())
60 }
61}
62
63pub type VmfResult<T> = Result<T, VmfError>;