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: Sort out new MSRV
14// TODO: split the `Error` into a separate parse and render error
15
16/// All possible errors when parsing or rendering VDF text
17///
18/// Currently the two variants are parse errors which currently only occurs when `pest` encounters
19#[derive(Clone, Debug, PartialEq, Eq)]
20pub enum Error {
21    EscapedParseError(EscapedPestError),
22    RawParseError(RawPestError),
23    RenderError(fmt::Error),
24    RawRenderError { invalid_char: char },
25}
26
27impl From<EscapedPestError> for Error {
28    fn from(e: EscapedPestError) -> Self {
29        Self::EscapedParseError(e)
30    }
31}
32
33impl From<RawPestError> for Error {
34    fn from(e: RawPestError) -> Self {
35        Self::RawParseError(e)
36    }
37}
38
39impl From<std::fmt::Error> for Error {
40    fn from(e: std::fmt::Error) -> Self {
41        Self::RenderError(e)
42    }
43}
44
45impl fmt::Display for Error {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        match self {
48            Self::EscapedParseError(e) => write!(f, "Failed parsing input Error: {e}"),
49            Self::RawParseError(e) => write!(f, "Failed parsing input Error: {e}"),
50            Self::RenderError(e) => write!(f, "Failed rendering input Error: {e}"),
51            Self::RawRenderError { invalid_char } => write!(
52                f,
53                "Encountered invalid character in raw string: {invalid_char:?}"
54            ),
55        }
56    }
57}
58
59impl std::error::Error for Error {}