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
use crate::Context;
use std::error::Error;
use std::fmt;
pub type Action = fn(Context) -> Result<ActionResult, ActionError>;
#[derive(Debug)]
pub enum ActionResult {
Done,
ShowHelpRequest(Context),
}
#[derive(Debug)]
pub struct ActionError {
pub description: String,
pub context: Context,
pub related_error: Option<Box<dyn Error>>,
}
impl ActionError {
pub fn new<T: Into<String>>(
description: T,
context: Context,
related_error: Option<Box<dyn Error>>,
) -> Self {
Self {
description: description.into(),
context,
related_error,
}
}
pub fn without_related_error(description: String, context: Context) -> Self {
Self {
description,
context,
related_error: None,
}
}
}
impl fmt::Display for ActionError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.description)
}
}
impl Error for ActionError {}