1use std::num::{ParseFloatError, ParseIntError};
4use std::fmt;
5
6#[derive(Debug)]
8pub enum TryFromFloatCurrenciesError {
9 Fractional {
11 fract: f32,
13 },
14 OutOfBounds {
16 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#[derive(Debug, Clone, PartialEq, Eq)]
38pub enum ParseError {
39 NoCurrenciesDetected,
41 MissingCount,
43 MissingCurrencyName,
45 UnexpectedToken {
47 span: std::ops::Range<usize>,
49 },
50 InvalidCurrencyName {
52 span: std::ops::Range<usize>,
54 },
55 ParseInt(ParseIntError),
57 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}