keyvalues_serde/
error.rs

1//! Contains error information for `keyvalues-serde`
2
3// TODO: figure this out before the next breaking release
4#![allow(clippy::large_enum_variant)]
5
6use keyvalues_parser::error::Error as ParserError;
7use serde_core::{de, ser};
8
9use std::{
10    fmt, io,
11    num::{ParseFloatError, ParseIntError},
12};
13
14/// Alias for the result with [`Error`] as the error type
15pub type Result<T> = std::result::Result<T, Error>;
16
17/// All the possible errors that can be encountered when (de)serializing VDF text
18#[derive(Debug)]
19pub enum Error {
20    Message(String),
21    Parse(ParserError),
22    Io(io::Error),
23    NonFiniteFloat(f32),
24    EofWhileParsingAny,
25    EofWhileParsingKey,
26    EofWhileParsingValue,
27    EofWhileParsingKeyOrValue,
28    EofWhileParsingObject,
29    EofWhileParsingSequence,
30    ExpectedObjectStart,
31    ExpectedSomeValue,
32    ExpectedSomeNonSeqValue,
33    ExpectedSomeIdent,
34    InvalidBoolean,
35    InvalidChar,
36    InvalidNumber,
37    TrailingTokens,
38    UnexpectedEndOfObject,
39    UnexpectedEndOfSequence,
40    Unsupported(&'static str),
41}
42
43impl de::Error for Error {
44    fn custom<T: fmt::Display>(msg: T) -> Self {
45        Self::Message(msg.to_string())
46    }
47}
48
49impl ser::Error for Error {
50    fn custom<T: fmt::Display>(msg: T) -> Self {
51        Self::Message(msg.to_string())
52    }
53}
54
55impl From<ParseIntError> for Error {
56    fn from(_: ParseIntError) -> Self {
57        Self::InvalidNumber
58    }
59}
60
61impl From<ParseFloatError> for Error {
62    fn from(_: ParseFloatError) -> Self {
63        Self::InvalidNumber
64    }
65}
66
67impl From<ParserError> for Error {
68    fn from(e: ParserError) -> Self {
69        Self::Parse(e)
70    }
71}
72
73impl From<io::Error> for Error {
74    fn from(e: io::Error) -> Self {
75        Self::Io(e)
76    }
77}
78
79impl fmt::Display for Error {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        match self {
82            Self::Message(msg) => f.write_str(msg),
83            Self::Parse(_) => f.write_str("Failed parsing VDF text"),
84            Self::Io(e) => write!(f, "Encountered I/O Error: {e}"),
85            Self::NonFiniteFloat(non_finite) => {
86                write!(
87                    f,
88                    "Only finite f32 values are allowed. Instead got: {non_finite}"
89                )
90            }
91            Self::EofWhileParsingAny => f.write_str("EOF while parsing unknown type"),
92            Self::EofWhileParsingKey => f.write_str("EOF while parsing key"),
93            Self::EofWhileParsingValue => f.write_str("EOF while parsing a value"),
94            Self::EofWhileParsingKeyOrValue => f.write_str("EOF while parsing key or value"),
95            Self::EofWhileParsingObject => f.write_str("EOF while parsing an object"),
96            Self::EofWhileParsingSequence => f.write_str("EOF while parsing a sequence"),
97            Self::ExpectedObjectStart => f.write_str("Expected a valid token for object start"),
98            Self::ExpectedSomeValue => f.write_str("Expected some valid value"),
99            Self::ExpectedSomeNonSeqValue => f.write_str("Expected a non-sequence value"),
100            Self::ExpectedSomeIdent => f.write_str("Expected some valid ident"),
101            Self::InvalidBoolean => f.write_str("Tried parsing an invalid boolean"),
102            Self::InvalidChar => f.write_str("Tried parsing an invalid char"),
103            Self::InvalidNumber => f.write_str("Tried parsing an invalid number"),
104            Self::TrailingTokens => f.write_str("Tokens remain after deserializing"),
105            Self::UnexpectedEndOfObject => f.write_str("Unexpected end of object"),
106            Self::UnexpectedEndOfSequence => f.write_str("Unexpected end of sequence"),
107            Self::Unsupported(type_name) => write!(f, "Tried using unsupported type: {type_name}"),
108        }
109    }
110}
111
112impl std::error::Error for Error {}