cron_lingo/
error.rs

1use std::error::Error as StdError;
2use std::fmt;
3
4/// A global error type that encapsulates all other, more specific
5/// error types.
6#[non_exhaustive]
7#[derive(Debug, PartialEq)]
8pub enum Error {
9    EmptyExpression,
10    Syntax(SyntaxError),
11    UnexpectedEndOfInput,
12    TimeParse(time::error::Parse),
13    IndeterminateOffset(time::error::IndeterminateOffset),
14}
15
16impl fmt::Display for Error {
17    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
18        match self {
19            Self::EmptyExpression => write!(f, "the expression string must not be empty"),
20            Self::Syntax(e) => e.fmt(f),
21            Self::UnexpectedEndOfInput => write!(
22                f,
23                "the parser reached the end of the expression but expected more characters"
24            ),
25            Self::TimeParse(e) => write!(f, "failed to parse time: {}", e),
26            Self::IndeterminateOffset(e) => e.fmt(f),
27        }
28    }
29}
30
31impl StdError for Error {}
32
33/// Generic syntax error. Gives the exact position of the erroneous character
34/// in an expression and points out the expected input.
35#[derive(Debug, Clone, PartialEq)]
36pub struct SyntaxError {
37    pub(crate) position: usize,
38    pub(crate) expected: String,
39    pub(crate) continues: String,
40}
41
42impl fmt::Display for SyntaxError {
43    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44        write!(
45            f,
46            "unexpected sequence of characters starting at position '{}', expected {}, got '{}'",
47            self.position, self.expected, self.continues
48        )
49    }
50}
51
52impl StdError for SyntaxError {}