vmf_forge/
errors.rs

1//! This module defines the error types used in the VMF parser using `thiserror`.
2
3use pest::error::Error as PestError;
4use std::{io, num};
5use thiserror::Error;
6
7/// Represents an error that occurred during VMF parsing or processing.
8#[derive(Error, Debug)]
9pub enum VmfError {
10    /// An I/O error occurred.
11    #[error("IO error: {0}")]
12    Io(#[from] io::Error),
13
14    /// A parsing error occurred during the Pest parsing phase.
15    #[error("VMF parse error: {0}")]
16    Parse(#[from] Box<PestError<crate::parser::Rule>>),
17
18    /// The VMF structure or content is invalid or unexpected.
19    #[error("Invalid VMF format: {0}")]
20    InvalidFormat(String),
21
22    /// An error occurred while parsing an integer value for a specific key.
23    #[error("Integer parse error for key '{key}': {source}")]
24    ParseInt {
25        key: String,
26        #[source]
27        source: num::ParseIntError,
28    },
29
30    /// An error occurred while parsing a float value for a specific key.
31    #[error("Float parse error for key '{key}': {source}")]
32    ParseFloat {
33        key: String,
34        #[source]
35        source: num::ParseFloatError,
36    },
37}
38
39/// A type alias for `Result` that uses `VmfError` as the error type.
40pub type VmfResult<T> = Result<T, VmfError>;
41
42impl From<(std::num::ParseIntError, String)> for VmfError {
43    fn from(err: (std::num::ParseIntError, String)) -> Self {
44        VmfError::ParseInt {
45            source: err.0,
46            key: err.1,
47        }
48    }
49}
50
51impl From<(std::num::ParseFloatError, String)> for VmfError {
52    fn from(err: (std::num::ParseFloatError, String)) -> Self {
53        VmfError::ParseFloat {
54            source: err.0,
55            key: err.1,
56        }
57    }
58}