summaryrefslogtreecommitdiff
path: root/nfa/src/error.rs
blob: ad75077c8bf48a5b8dd12be672a6c8adf41c9c6a (plain)
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
//! This file implements the error type for the crate.

use graph::error::Error as GError;

use core::fmt::{Display, Formatter};

/// The error type for NFA operations.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
#[non_exhaustive]
pub enum Error {
    /// An unknown node id is encountered.
    UnknownNode(usize),
    /// Some operations are not supported by the implementations.
    ///
    /// Everything is a trade-off, wink wink.
    UnsupportedOperation,
    /// There is no root in the underlying regular expression.
    NoRoot,
    /// This error comes from some underlying graph operation.
    Graph(GError),
    /// A cycle is found when constructing regular expressions.
    Cycle,
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::UnknownNode(id) => write!(f, "unknown node: {id}"),
            Error::UnsupportedOperation => write!(f, "unsupported operation"),
            Error::Graph(e) => write!(f, "graph error: {e}"),
            Error::NoRoot => write!(f, "no root found"),
            Error::Cycle => write!(f, "a cycle is found when constructing regular expressions"),
        }
    }
}

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

impl From<GError> for Error {
    fn from(e: GError) -> Self {
        Self::Graph(e)
    }
}