keyvalues_parser/
error.rs

1//! All error information for parsing and rendering
2
3use std::fmt;
4
5use crate::text::parse::{EscapedPestError, RawPestError};
6
7/// Just a type alias for `Result` with a [`Error`]
8pub type Result<T> = std::result::Result<T, Error>;
9
10// TODO: Swap out the `EscapedParseError` and `RawParseError` for an opaque `Error::Parse` variant
11// that handles displaying the error
12// TODO: should this whole thing be overhauled (future me here: yes)
13// TODO: split the `Error` into a separate parse and render error
14
15/// All possible errors when parsing or rendering VDF text
16///
17/// Currently the two variants are parse errors which currently only occurs when `pest` encounters
18#[derive(Clone, Debug, PartialEq, Eq)]
19pub enum Error {
20    EscapedParseError(EscapedPestError),
21    RawParseError(RawPestError),
22    RenderError(fmt::Error),
23    RawRenderError { invalid_char: char },
24}
25
26impl From<EscapedPestError> for Error {
27    fn from(e: EscapedPestError) -> Self {
28        Self::EscapedParseError(e)
29    }
30}
31
32impl From<RawPestError> for Error {
33    fn from(e: RawPestError) -> Self {
34        Self::RawParseError(e)
35    }
36}
37
38impl From<std::fmt::Error> for Error {
39    fn from(e: std::fmt::Error) -> Self {
40        Self::RenderError(e)
41    }
42}
43
44impl fmt::Display for Error {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        match self {
47            Self::EscapedParseError(e) => write!(f, "Failed parsing input Error: {e}"),
48            Self::RawParseError(e) => write!(f, "Failed parsing input Error: {e}"),
49            Self::RenderError(e) => write!(f, "Failed rendering input Error: {e}"),
50            Self::RawRenderError { invalid_char } => write!(
51                f,
52                "Encountered invalid character in raw string: {invalid_char:?}"
53            ),
54        }
55    }
56}
57
58impl std::error::Error for Error {}