Skip to main content

source_vmf/
errors.rs

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