aopt_help/
error.rs

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