tolerance/
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
use std::convert::Infallible;
use std::fmt::{Display, Formatter};
use std::num::{ParseFloatError, TryFromIntError};

#[derive(Debug, PartialEq)]
pub enum ToleranceError {
    ParseError(String),
    Overflow(String),
}

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

impl From<ParseFloatError> for ToleranceError {
    fn from(pfe: ParseFloatError) -> Self {
        Self::ParseError(pfe.to_string())
    }
}

impl From<TryFromIntError> for ToleranceError {
    fn from(t: TryFromIntError) -> Self {
        Self::Overflow(t.to_string())
    }
}

/// The error that never happens.
impl From<Infallible> for ToleranceError {
    fn from(_: Infallible) -> Self {
        Self::ParseError(String::new())
    }
}

impl Display for ToleranceError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        use ToleranceError::{Overflow, ParseError};
        let text = match self {
            ParseError(text) | Overflow(text) => text.as_str(),
        };
        write!(f, "{text}")
    }
}

impl ToleranceError {
    /// Helper-method to create a `ParseError`.
    #[inline]
    pub fn parse_err<R>(text: impl Into<String>) -> Result<R, ToleranceError> {
        Err(Self::ParseError(text.into()))
    }
}