vmf_forge/
errors.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! This module defines the error types used in the VMF parser.

use pest::error::Error as PestError;
use std::io;

/// Represents an error that occurred during VMF parsing or serialization.
#[derive(Debug)]
pub enum VmfError {
    /// An I/O error occurred.
    Io(io::Error),
    /// A parsing error occurred.
    Parse(PestError<crate::parser::Rule>),
    /// The VMF file has an invalid format.
    InvalidFormat(String),
    /// An error occurred while parsing an integer.
    ParseInt(std::num::ParseIntError, String),
    /// An error occurred while parsing a float.
    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())
    }
}

/// A type alias for `Result` that uses `VmfError` as the error type.
pub type VmfResult<T> = Result<T, VmfError>;