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
use fmt::Debug;
use crate::{Context, Vector};
use std::{error::Error, fmt};
pub type Action = fn(Context) -> Result<ActionResult, ActionError>;
#[derive(Debug)]
pub enum ActionResult {
Done,
ShowHelpRequest(Context),
ShowOtherHelpReq(Context, usize, Vector<String>),
Result(Context, Action),
Custom(Box<dyn Debug>),
}
impl ActionResult {
pub fn is_done(&self) -> bool {
match self {
ActionResult::Done => true,
_ => false,
}
}
}
#[derive(Debug)]
pub struct ActionError {
pub value: String,
pub kind: ActionErrorKind,
pub context: Context,
pub related_error: Option<Box<dyn Error>>,
pub printed: bool,
}
#[derive(Debug)]
pub enum ActionErrorKind {
Custom,
NoActionRegistered,
None,
}
impl ActionError {
pub fn new<T: Into<String>>(
value: T,
kind: ActionErrorKind,
context: Context,
related_error: Option<Box<dyn Error>>,
) -> Self {
Self {
value: value.into(),
kind,
context,
related_error,
printed: false,
}
}
pub fn without_related_error(value: String, kind: ActionErrorKind, context: Context) -> Self {
Self {
value,
kind,
context,
related_error: None,
printed: false,
}
}
}
impl fmt::Display for ActionError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> fmt::Result {
match self.kind {
ActionErrorKind::Custom => write!(f, "{}", self.value),
ActionErrorKind::NoActionRegistered => {
write!(f, "{} does not have its own action.", self.value)
}
ActionErrorKind::None => write!(f, "no action error"),
}
}
}
impl Error for ActionError {}