-
Notifications
You must be signed in to change notification settings - Fork 7
/
action.rs
66 lines (61 loc) · 1.91 KB
/
action.rs
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
use clap::builder::Str;
use crate::{
command::application::{CLEAR, CREATE, DELETE, EXIT, HISTORY, LIST, LS, Q, QUEUE, TEST},
error::ParseError,
};
pub enum ActionType {
Create,
Queue,
Delete,
List,
Test,
Exit,
Clear,
History,
}
impl ActionType {
pub fn parse(s: &str) -> Result<Self, ParseError> {
match s.to_lowercase().as_str() {
CREATE => Ok(ActionType::Create),
Q | QUEUE => Ok(ActionType::Queue),
DELETE => Ok(ActionType::Delete),
LS | LIST => Ok(ActionType::List),
TEST => Ok(ActionType::Test),
EXIT => Ok(ActionType::Exit),
CLEAR => Ok(ActionType::Clear),
HISTORY => Ok(ActionType::History),
_ => Err(ParseError::new(format!(
"failed to parse str ({}) to ActionType",
s
))),
}
}
}
impl From<ActionType> for String {
fn from(action: ActionType) -> Self {
match action {
ActionType::Create => String::from(CREATE),
ActionType::Queue => String::from(QUEUE),
ActionType::Delete => String::from(DELETE),
ActionType::List => String::from(LIST),
ActionType::Test => String::from(TEST),
ActionType::Exit => String::from(EXIT),
ActionType::Clear => String::from(CLEAR),
ActionType::History => String::from(HISTORY),
}
}
}
impl From<ActionType> for Str {
fn from(action: ActionType) -> Self {
match action {
ActionType::Create => CREATE.into(),
ActionType::Queue => QUEUE.into(),
ActionType::Delete => DELETE.into(),
ActionType::List => LIST.into(),
ActionType::Test => TEST.into(),
ActionType::Exit => EXIT.into(),
ActionType::Clear => CLEAR.into(),
ActionType::History => HISTORY.into(),
}
}
}