ggplot_error/error/
mod.rs1mod error_std;
2use std::{
3 convert::Infallible,
4 error::Error,
5 fmt::{self, Debug, Display, Formatter},
6 ops::Range,
7};
8
9pub type Result<T> = std::result::Result<T, GGError>;
11pub type MaybeRanged = Option<Range<usize>>;
13
14#[derive(Debug)]
16pub enum GGError {
17 IOError(std::io::Error),
19 FormatError(std::fmt::Error),
22 SyntaxError(String),
24 TypeMismatch(String),
26 RuntimeError(String),
28 UndefinedVariable {
30 name: String,
32 },
33 Unreachable,
35 }
38
39macro_rules! error_msg {
40 ($name:ident => $t:ident) => {
41 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}