aopt_help/
error.rs

1use std::fmt::Display;
2
3pub type Result<T> = std::result::Result<T, Error>;
4
5#[derive(Clone)]
6pub enum Error {
7    Null,
8
9    InvalidBlockName(String),
10
11    DuplicatedBlockName(String),
12
13    InvalidStoreName(String),
14
15    DuplicatedStoreName(String),
16
17    DuplicatedCommandName(String),
18
19    Error(String),
20}
21
22impl Default for Error {
23    fn default() -> Self {
24        Self::Null
25    }
26}
27
28impl std::fmt::Debug for Error {
29    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30        write!(f, "{}", self.display())
31    }
32}
33
34impl std::error::Error for Error {
35    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
36        None
37    }
38
39    fn cause(&self) -> Option<&dyn std::error::Error> {
40        self.source()
41    }
42}
43
44impl Display for Error {
45    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46        write!(f, "{}", self.display())
47    }
48}
49
50impl Error {
51    pub fn raise(msg: String) -> Self {
52        Self::Error(msg)
53    }
54
55    pub fn display(&self) -> String {
56        match self {
57            Error::Null => {
58                panic!("Can not use Error::Null")
59            }
60            Error::InvalidBlockName(name) => {
61                format!("Invalid block name {}", name)
62            }
63            Error::DuplicatedBlockName(name) => {
64                format!("Duplicated block name {}", name)
65            }
66            Error::InvalidStoreName(name) => {
67                format!("Invalid store name {}", name)
68            }
69            Error::DuplicatedStoreName(name) => {
70                format!("Duplicated store name {}", name)
71            }
72            Error::DuplicatedCommandName(name) => {
73                format!("Duplicated command name {}", name)
74            }
75            Error::Error(msg) => msg.clone(),
76        }
77    }
78}