summaryrefslogtreecommitdiff
path: root/src/systems/cmd/errors.rs
blob: abde5de40517915e91956cb3e6c9d6b0e17c5c61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use just_enough_vcs::lib::data::{member::MemberId, sheet::SheetName};

#[derive(thiserror::Error, Debug)]
pub enum CmdPrepareError {
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    #[error("{0}")]
    Error(String),

    // Workspace Reader Errors
    #[error("LocalWorkspace not found")]
    LocalWorkspaceNotFound,

    #[error("LocalConfig not found")]
    LocalConfigNotFound,

    #[error("LatestInfo not found")]
    LatestInfoNotFound,

    #[error("LatestFileData of {0} not found")]
    LatestFileDataNotExist(MemberId),

    #[error("CachedSheet `{0}` not found")]
    CachedSheetNotFound(SheetName),

    #[error("LocalSheet `{0}/{1}` not found")]
    LocalSheetNotFound(MemberId, SheetName),

    #[error("LocalStatusAnalyzeFailed")]
    LocalStatusAnalyzeFailed,

    #[error("No sheet in use")]
    NoSheetInUse,
}

impl CmdPrepareError {
    pub fn new(msg: impl AsRef<str>) -> Self {
        CmdPrepareError::Error(msg.as_ref().to_string())
    }
}

#[derive(thiserror::Error, Debug)]
pub enum CmdExecuteError {
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    #[error("Content not prepared, cannot run")]
    Prepare(#[from] CmdPrepareError),

    #[error("{0}")]
    Error(String),
}

impl CmdExecuteError {
    pub fn new(msg: impl AsRef<str>) -> Self {
        CmdExecuteError::Error(msg.as_ref().to_string())
    }
}

#[derive(thiserror::Error, Debug)]
pub enum CmdRenderError {
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    #[error("Preparation failed, cannot render")]
    Prepare(#[from] CmdPrepareError),

    #[error("Execution failed, no output content obtained before rendering")]
    Execute(#[from] CmdExecuteError),

    #[error("{0}")]
    Error(String),

    #[error("Serialize failed, {0}")]
    SerializeFailed(String),

    #[error("Renderer `{0}` not found")]
    RendererNotFound(String),

    #[error("Type mismatch: expected `{expected:?}`, got `{actual:?}`")]
    TypeMismatch {
        expected: std::any::TypeId,
        actual: std::any::TypeId,
    },
}

impl CmdRenderError {
    pub fn new(msg: impl AsRef<str>) -> Self {
        CmdRenderError::Error(msg.as_ref().to_string())
    }
}

#[derive(thiserror::Error, Debug)]
pub enum CmdProcessError {
    #[error("Prepare error: {0}")]
    Prepare(#[from] CmdPrepareError),

    #[error("Execute error: {0}")]
    Execute(#[from] CmdExecuteError),

    #[error("Render error: {0}")]
    Render(#[from] CmdRenderError),

    #[error("{0}")]
    Error(String),

    #[error("Node `{0}` not found!")]
    NoNodeFound(String),

    #[error("No matching command found")]
    NoMatchingCommand,

    #[error("Parse error")]
    ParseError(String),

    #[error("Renderer override mode is active, but user requested help")]
    RendererOverrideButRequestHelp,

    #[error("Downcast failed")]
    DowncastFailed,
}

impl CmdProcessError {
    pub fn new(msg: impl AsRef<str>) -> Self {
        CmdProcessError::Error(msg.as_ref().to_string())
    }

    pub fn prepare_err(&self) -> Option<&CmdPrepareError> {
        match self {
            CmdProcessError::Prepare(e) => Some(e),
            _ => None,
        }
    }

    pub fn execute_err(&self) -> Option<&CmdExecuteError> {
        match self {
            CmdProcessError::Execute(e) => Some(e),
            _ => None,
        }
    }

    pub fn render_err(&self) -> Option<&CmdRenderError> {
        match self {
            CmdProcessError::Render(e) => Some(e),
            _ => None,
        }
    }
}