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
//! Why an action declined to run, as the verb that refused it says.
use crate::ActionError;
/// Why an action declined to run, in the user's words, and, where a caller
/// acts on the reason rather than reads it, a stable code naming it.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Refusal {
code: Option<String>,
reason: String,
}
impl Refusal {
pub fn new(reason: impl Into<String>) -> Self {
Self {
code: None,
reason: reason.into(),
}
}
/// A refusal a caller tells apart by `code` (a thing the action would add
/// is there already, say), with `reason` in words beside it for whoever
/// reads.
pub fn coded(code: impl Into<String>, reason: impl Into<String>) -> Self {
Self {
code: Some(code.into()),
reason: reason.into(),
}
}
/// The code the app gave, for a caller that decides on it.
#[must_use]
pub fn code(&self) -> Option<&str> {
self.code.as_deref()
}
/// Why, in the user's words.
#[must_use]
pub fn reason(&self) -> &str {
&self.reason
}
/// Why, in the user's words, owned.
#[must_use]
pub fn into_reason(self) -> String {
self.reason
}
}
impl std::fmt::Display for Refusal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.reason)
}
}
impl From<&str> for Refusal {
fn from(reason: &str) -> Self {
Self::new(reason)
}
}
impl From<String> for Refusal {
fn from(reason: String) -> Self {
Self::new(reason)
}
}
/// A verb's refusal as the action's: one a caller decides on keeps its code.
impl From<Refusal> for ActionError {
fn from(refusal: Refusal) -> Self {
match refusal.code {
Some(code) => Self::Unavailable {
code,
message: refusal.reason,
},
None => Self::Refused(refusal.reason),
}
}
}
/// An action's refusal as a verb's, for a reader above the action that hands
/// it on: one a caller decides on keeps its code.
impl From<ActionError> for Refusal {
fn from(error: ActionError) -> Self {
match error {
ActionError::Unavailable { code, message } => Self::coded(code, message),
other => Self::new(other.to_string()),
}
}
}