use pest::error::Error as PestError;
use std::io;
#[derive(Debug)]
pub enum VmfError {
Io(io::Error),
Parse(PestError<crate::parser::Rule>),
InvalidFormat(String),
ParseInt(std::num::ParseIntError, String),
ParseFloat(std::num::ParseFloatError, String),
}
impl std::fmt::Display for VmfError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
VmfError::Io(err) => write!(f, "IO error: {}", err),
VmfError::Parse(err) => write!(f, "Parse error: {}", err),
VmfError::InvalidFormat(msg) => write!(f, "Invalid format: {}", msg),
VmfError::ParseInt(err, key) => {
write!(f, "Integer parse error in key '{}': {}", key, err)
}
VmfError::ParseFloat(err, key) => {
write!(f, "Float parse error in key '{}': {}", key, err)
}
}
}
}
impl std::error::Error for VmfError {}
impl From<io::Error> for VmfError {
fn from(err: io::Error) -> Self {
VmfError::Io(err)
}
}
impl From<PestError<crate::parser::Rule>> for VmfError {
fn from(err: PestError<crate::parser::Rule>) -> Self {
VmfError::Parse(err)
}
}
impl From<std::num::ParseIntError> for VmfError {
fn from(err: std::num::ParseIntError) -> Self {
VmfError::ParseInt(err, "".to_string())
}
}
impl From<std::num::ParseFloatError> for VmfError {
fn from(err: std::num::ParseFloatError) -> Self {
VmfError::ParseFloat(err, "".to_string())
}
}
pub type VmfResult<T> = Result<T, VmfError>;