keyvalues_serde/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//! Contains error information for `keyvalues-serde`

// TODO: figure this out before the next breaking release
#![allow(clippy::large_enum_variant)]

use keyvalues_parser::error::Error as ParserError;
use serde::{de, ser};

use std::{
    fmt, io,
    num::{ParseFloatError, ParseIntError},
};

/// Alias for the result with [`Error`] as the error type
pub type Result<T> = std::result::Result<T, Error>;

/// All the possible errors that can be encountered when (de)serializing VDF text
#[derive(Debug)]
pub enum Error {
    Message(String),
    Parse(ParserError),
    Io(io::Error),
    NonFiniteFloat(f32),
    EofWhileParsingAny,
    EofWhileParsingKey,
    EofWhileParsingValue,
    EofWhileParsingKeyOrValue,
    EofWhileParsingObject,
    EofWhileParsingSequence,
    ExpectedObjectStart,
    ExpectedSomeValue,
    ExpectedSomeNonSeqValue,
    ExpectedSomeIdent,
    InvalidBoolean,
    InvalidChar,
    InvalidNumber,
    TrailingTokens,
    UnexpectedEndOfObject,
    UnexpectedEndOfSequence,
    Unsupported(&'static str),
}

impl de::Error for Error {
    fn custom<T: fmt::Display>(msg: T) -> Self {
        Self::Message(msg.to_string())
    }
}

impl ser::Error for Error {
    fn custom<T: fmt::Display>(msg: T) -> Self {
        Self::Message(msg.to_string())
    }
}

impl From<ParseIntError> for Error {
    fn from(_: ParseIntError) -> Self {
        Self::InvalidNumber
    }
}

impl From<ParseFloatError> for Error {
    fn from(_: ParseFloatError) -> Self {
        Self::InvalidNumber
    }
}

impl From<ParserError> for Error {
    fn from(e: ParserError) -> Self {
        Self::Parse(e)
    }
}

impl From<io::Error> for Error {
    fn from(e: io::Error) -> Self {
        Self::Io(e)
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Message(msg) => f.write_str(msg),
            Self::Parse(_) => f.write_str("Failed parsing VDF text"),
            Self::Io(e) => write!(f, "Encountered I/O Error: {e}"),
            Self::NonFiniteFloat(non_finite) => {
                write!(
                    f,
                    "Only finite f32 values are allowed. Instead got: {non_finite}"
                )
            }
            Self::EofWhileParsingAny => f.write_str("EOF while parsing unknown type"),
            Self::EofWhileParsingKey => f.write_str("EOF while parsing key"),
            Self::EofWhileParsingValue => f.write_str("EOF while parsing a value"),
            Self::EofWhileParsingKeyOrValue => f.write_str("EOF while parsing key or value"),
            Self::EofWhileParsingObject => f.write_str("EOF while parsing an object"),
            Self::EofWhileParsingSequence => f.write_str("EOF while parsing a sequence"),
            Self::ExpectedObjectStart => f.write_str("Expected a valid token for object start"),
            Self::ExpectedSomeValue => f.write_str("Expected some valid value"),
            Self::ExpectedSomeNonSeqValue => f.write_str("Expected a non-sequence value"),
            Self::ExpectedSomeIdent => f.write_str("Expected some valid ident"),
            Self::InvalidBoolean => f.write_str("Tried parsing an invalid boolean"),
            Self::InvalidChar => f.write_str("Tried parsing an invalid char"),
            Self::InvalidNumber => f.write_str("Tried parsing an invalid number"),
            Self::TrailingTokens => f.write_str("Tokens remain after deserializing"),
            Self::UnexpectedEndOfObject => f.write_str("Unexpected end of object"),
            Self::UnexpectedEndOfSequence => f.write_str("Unexpected end of sequence"),
            Self::Unsupported(type_name) => write!(f, "Tried using unsupported type: {type_name}"),
        }
    }
}

impl std::error::Error for Error {}