Skip to main content

tf2_price/
error.rs

1//! Error types.
2
3use std::num::{ParseFloatError, ParseIntError};
4use std::fmt;
5
6/// Error converting float currencies to currencies.
7#[derive(Debug)]
8pub enum TryFromFloatCurrenciesError {
9    /// The `keys` part of the currencies contained a fractional value.
10    Fractional {
11        /// Fractional key values are invalid.
12        fract: f32,
13    },
14    /// A value overflowed or underflowed the integer bounds.
15    OutOfBounds {
16        /// The value that was out of bounds.
17        value: f32,
18    },
19}
20
21impl std::error::Error for TryFromFloatCurrenciesError {}
22
23impl fmt::Display for TryFromFloatCurrenciesError {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        match self {
26            TryFromFloatCurrenciesError::Fractional { fract } => {
27                write!(f, "Currencies contains fractional value: {fract}")
28            }
29            TryFromFloatCurrenciesError::OutOfBounds { value } => {
30                write!(f, "Conversion of {value} was out of integer bounds")
31            }
32        }
33    }
34}
35
36/// An error occurred parsing a currency from a string.
37#[derive(Debug, Clone, PartialEq, Eq)]
38pub enum ParseError {
39    /// String was invalid.
40    NoCurrenciesDetected,
41    /// A number was expected, but none was found.
42    MissingCount,
43    /// A currency name was expected, but none was found.
44    MissingCurrencyName,
45    /// An unexpected element was found.
46    UnexpectedToken {
47        /// The span of the unexpected token.
48        span: std::ops::Range<usize>,
49    },
50    /// An invalid currency name was found.
51    InvalidCurrencyName {
52        /// The span of the invalid currency name.
53        span: std::ops::Range<usize>,
54    },
55    /// A string failed to parse to an integer.
56    ParseInt(ParseIntError),
57    /// A string failed to parse to a float.
58    ParseFloat(ParseFloatError),
59}
60
61impl std::error::Error for ParseError {}
62
63impl fmt::Display for ParseError {
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        match self {
66            ParseError::NoCurrenciesDetected => write!(f, "No currencies could be parsed from string"),
67            ParseError::MissingCount => write!(f, "Expected a number, but none was found"),
68            ParseError::MissingCurrencyName => write!(f, "Expected a currency name, but none was found"),
69            ParseError::UnexpectedToken {
70                span
71            } => write!(f, "Unexpected token at {}", span.start),
72            ParseError::InvalidCurrencyName {
73                span
74            } => write!(f, "Invalid currency name at index {}", span.start),
75            ParseError::ParseInt(e) => write!(f, "{e}"),
76            ParseError::ParseFloat(e) => write!(f, "{e}"),
77        }
78    }
79}
80
81impl From<ParseIntError> for ParseError {
82    fn from(e: ParseIntError) -> Self {
83        ParseError::ParseInt(e)
84    }
85}
86
87impl From<ParseFloatError> for ParseError {
88    fn from(e: ParseFloatError) -> Self {
89        ParseError::ParseFloat(e)
90    }
91}