ggplot_error/error/
mod.rs

1mod error_std;
2use std::{
3    convert::Infallible,
4    error::Error,
5    fmt::{self, Debug, Display, Formatter},
6    ops::Range,
7};
8
9/// All result about tailwind
10pub type Result<T> = std::result::Result<T, GGError>;
11/// Maybe have ast position
12pub type MaybeRanged = Option<Range<usize>>;
13
14/// Error type for all tailwind operators
15#[derive(Debug)]
16pub enum GGError {
17    /// The error type for I/O operations
18    IOError(std::io::Error),
19    /// The error type which is returned from formatting a message into a
20    /// stream.
21    FormatError(std::fmt::Error),
22    /// The error type which is
23    SyntaxError(String),
24    /// The error type which is
25    TypeMismatch(String),
26    /// The error type which is occurred at runtime
27    RuntimeError(String),
28    /// Runtime error when variable is undefined
29    UndefinedVariable {
30        /// The name of the undefined variable
31        name: String,
32    },
33    /// A forbidden cst_node encountered
34    Unreachable,
35    // #[error(transparent)]
36    // UnknownError(#[from] anyhow::Error),
37}
38
39macro_rules! error_msg {
40    ($name:ident => $t:ident) => {
41        /// Constructor of [`NoteErrorKind::$t`]
42        pub fn $name(msg: impl Into<String>) -> Self {
43            GGError::$t(msg.into())
44        }
45    };
46    ($($name:ident => $t:ident),+ $(,)?) => (
47        impl GGError { $(error_msg!($name=>$t);)+ }
48    );
49}
50
51error_msg![
52    syntax_error => SyntaxError,
53    type_mismatch => TypeMismatch,
54    runtime_error => RuntimeError,
55];
56
57impl Error for GGError {}
58
59impl Display for GGError {
60    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
61        match self {
62            Self::IOError(e) => {
63                write!(f, "{}", e)
64            }
65            Self::FormatError(e) => {
66                write!(f, "{}", e)
67            }
68            Self::SyntaxError(msg) => {
69                f.write_str("SyntaxError: ")?;
70                f.write_str(msg)
71            }
72            Self::TypeMismatch(msg) => {
73                f.write_str("TypeError: ")?;
74                f.write_str(msg)
75            }
76            Self::RuntimeError(msg) => {
77                f.write_str("RuntimeError: ")?;
78                f.write_str(msg)
79            }
80            Self::UndefinedVariable { name } => {
81                write!(f, "RuntimeError: Variable {} not found in scope", name)
82            }
83            Self::Unreachable => {
84                f.write_str("InternalError: ")?;
85                f.write_str("Entered unreachable code!")
86            }
87        }
88    }
89}