vmf_forge/
errors.rs

1//! This module defines the error types used in the VMF parser.
2
3use pest::error::Error as PestError;
4use std::io;
5
6/// Represents an error that occurred during VMF parsing or serialization.
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum VmfError {
9    /// An I/O error occurred.
10    Io(io::ErrorKind),
11    /// A parsing error occurred.
12    Parse(Box<PestError<crate::parser::Rule>>),
13    /// The VMF file has an invalid format.
14    InvalidFormat(String),
15    /// An error occurred while parsing an integer.
16    ParseInt(std::num::ParseIntError, String),
17    /// An error occurred while parsing a float.
18    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
63/// A type alias for `Result` that uses `VmfError` as the error type.
64pub type VmfResult<T> = Result<T, VmfError>;