tui_markup/
error.rs

1use std::fmt::{Debug, Display};
2
3use crate::parser::Error as ParseError;
4
5/// Error with a location info.
6pub trait LocatedError {
7    /// get error happened location in source input.
8    fn location(&self) -> (usize, usize);
9}
10
11/// Error for markup source compile pipeline.
12///
13/// Display this error in `{}` formatter will show a error message with detailed reason and location.
14/// So usually you don't need check variants.
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub enum Error<'a, GE> {
17    /// Parsing stage failed, usually means there is invalid syntax in source string
18    Parse(ParseError<'a>),
19
20    /// Generating stage failed, see document of generator type for detail.
21    Gen(GE),
22}
23
24impl<'a, GE> Display for Error<'a, GE>
25where
26    GE: Display,
27{
28    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
29        match self {
30            Error::Parse(pe) => {
31                f.write_str("parse failed: ")?;
32                <ParseError as Display>::fmt(pe, f)?;
33            }
34            Error::Gen(ge) => {
35                f.write_str("generate failed: ")?;
36                <GE as Display>::fmt(ge, f)?;
37            }
38        }
39        Ok(())
40    }
41}
42
43impl<'a, GE> std::error::Error for Error<'a, GE> where Self: Debug + Display {}
44
45impl<'a, GE: LocatedError> LocatedError for Error<'a, GE> {
46    fn location(&self) -> (usize, usize) {
47        match self {
48            Self::Parse(e) => e.location(),
49            Self::Gen(e) => e.location(),
50        }
51    }
52}
53
54impl<'a, GE> From<ParseError<'a>> for Error<'a, GE> {
55    fn from(e: ParseError<'a>) -> Self {
56        Self::Parse(e)
57    }
58}
59
60#[cfg(test)]
61mod test {
62    use crate::generator::helper::GeneratorInfallible;
63
64    #[test]
65    fn error_must_impl_std_error() {
66        fn is_error<E: std::error::Error>() {}
67
68        is_error::<GeneratorInfallible>();
69        is_error::<crate::parser::Error>();
70        is_error::<super::Error<'static, GeneratorInfallible>>();
71    }
72}