1use std::fmt;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub enum ExitCode {
5 Success = 0,
6 RuntimeFailure = 1,
7 Usage = 2,
8 Internal = 3,
9}
10
11#[derive(Debug)]
12pub struct AppError {
13 code: ExitCode,
14 message: String,
15}
16
17impl AppError {
18 pub fn usage<T: Into<String>>(message: T) -> Self {
19 Self {
20 code: ExitCode::Usage,
21 message: message.into(),
22 }
23 }
24
25 pub fn runtime<T: Into<String>>(message: T) -> Self {
26 Self {
27 code: ExitCode::RuntimeFailure,
28 message: message.into(),
29 }
30 }
31
32 pub fn internal<T: Into<String>>(message: T) -> Self {
33 Self {
34 code: ExitCode::Internal,
35 message: message.into(),
36 }
37 }
38
39 pub fn code(&self) -> i32 {
40 self.code as i32
41 }
42}
43
44impl fmt::Display for AppError {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 write!(f, "{}", self.message)
47 }
48}
49
50impl std::error::Error for AppError {}