mrot_test_utils/
error.rs

1//! test-utils error
2
3use chrono::ParseError;
4use libmrot::Error as LibMrotError;
5use std::convert::From;
6use std::fmt;
7use std::num::ParseIntError;
8use two_timer::TimeError;
9
10/// Mrot error variants
11#[derive(Debug)]
12pub enum Error {
13    /// wraps [libmrot::Error]
14    LibMrot(LibMrotError),
15    /// Bug in the test code.
16    /// Examples
17    ///
18    /// - a value in the [`World`](crate::World) was supposed to be defined but wasn't
19    /// - a value was expected to be read from the feature file's Examples table, but it wasn't there
20    UndefinedValue(String),
21    /// wraps [chrono::ParseError]
22    Chrono(ParseError),
23    /// Unexpected Err Result
24    UnexpectedErrResult(String),
25    /// Wraps [two_timer::TimeError]
26    TwoTimer(TimeError),
27    /// Wraps [std::num::ParseIntError]
28    StdNum(ParseIntError),
29}
30
31impl fmt::Display for Error {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        match &self {
34            Error::LibMrot(libmrot_error) => fmt::Display::fmt(libmrot_error, f),
35            Error::UndefinedValue(field) => {
36                fmt::Display::fmt(&format!("world has no value for {}", field), f)
37            }
38            Error::Chrono(parse_error) => fmt::Display::fmt(parse_error, f),
39            Error::UnexpectedErrResult(error) => fmt::Display::fmt(&error, f),
40            Error::TwoTimer(time_error) => fmt::Display::fmt(time_error, f),
41            Error::StdNum(parse_int_error) => fmt::Display::fmt(parse_int_error, f),
42        }
43    }
44}
45
46impl std::error::Error for Error {
47    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
48        match *self {
49            Error::LibMrot(ref libmrot_error) => Some(libmrot_error),
50            Error::UndefinedValue(_) => None,
51            Error::Chrono(ref parse_error) => Some(parse_error),
52            Error::UnexpectedErrResult(_) => None,
53            Error::TwoTimer(ref time_error) => Some(time_error),
54            Error::StdNum(ref parse_int_error) => Some(parse_int_error),
55        }
56    }
57}
58
59impl From<LibMrotError> for Error {
60    fn from(value: LibMrotError) -> Self {
61        Error::LibMrot(value)
62    }
63}
64
65impl From<ParseError> for Error {
66    fn from(value: ParseError) -> Self {
67        Error::Chrono(value)
68    }
69}
70
71impl From<TimeError> for Error {
72    fn from(value: TimeError) -> Self {
73        Error::TwoTimer(value)
74    }
75}
76
77impl From<ParseIntError> for Error {
78    fn from(value: ParseIntError) -> Self {
79        Error::StdNum(value)
80    }
81}