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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use {
crate::{
app::SelectionType,
keys,
command::Sequence,
errors::ConfError,
verb::*,
},
serde::Deserialize,
std::convert::TryFrom,
};
#[derive(Default, Debug, Clone, Deserialize)]
pub struct VerbConf {
invocation: Option<String>,
internal: Option<String>,
external: Option<ExecPattern>,
execution: Option<ExecPattern>,
cmd: Option<String>,
cmd_separator: Option<String>,
key: Option<String>,
shortcut: Option<String>,
leave_broot: Option<bool>,
from_shell: Option<bool>,
apply_to: Option<String>,
set_working_dir: Option<bool>,
description: Option<String>,
}
impl TryFrom<&VerbConf> for Verb {
type Error = ConfError;
fn try_from(vc: &VerbConf) -> Result<Self, Self::Error> {
if vc.leave_broot == Some(false) && vc.from_shell == Some(true) {
return Err(ConfError::InvalidVerbConf {
details: "You can't simultaneously have leave_broot=false and from_shell=true".to_string(),
});
}
let invocation = vc.invocation.clone().filter(|i| !i.is_empty());
let internal = vc.internal.as_ref().filter(|i| !i.is_empty());
let external = vc.external.as_ref().filter(|i| !i.is_empty());
let cmd = vc.cmd.as_ref().filter(|i| !i.is_empty());
let cmd_separator = vc.cmd_separator.as_ref().filter(|i| !i.is_empty());
let execution = vc.execution.as_ref().filter(|i| !i.is_empty());
let key = vc.key.clone().map(|s| keys::parse_key(&s)).transpose()?;
let make_external_execution = |s| {
ExternalExecution::new(
s,
ExternalExecutionMode::from_conf(vc.from_shell, vc.leave_broot),
)
.with_set_working_dir(vc.set_working_dir)
};
let execution = match (execution, internal, external, cmd) {
(Some(ep), None, None, None) => {
if let Some(internal_pattern) = ep.as_internal_pattern() {
VerbExecution::Internal(InternalExecution::try_from(internal_pattern)?)
} else {
VerbExecution::External(make_external_execution(ep.clone()))
}
}
(None, Some(s), None, None) => {
VerbExecution::Internal(if s.starts_with(':') || s.starts_with(' ') {
InternalExecution::try_from(&s[1..])?
} else {
InternalExecution::try_from(&s)?
})
}
(None, None, Some(ep), None) => {
VerbExecution::External(make_external_execution(ep.clone()))
}
(None, None, None, Some(s)) => VerbExecution::Sequence(SequenceExecution {
sequence: Sequence::new(s, cmd_separator),
}),
_ => {
return Err(ConfError::InvalidVerbConf {
details: "You must define either internal, external or cmd".to_string(),
});
}
};
let description = vc
.description
.clone()
.map(VerbDescription::from_text)
.unwrap_or_else(|| VerbDescription::from_code(execution.to_string()));
let mut verb = Verb::new(
invocation.as_deref(),
execution,
description,
)?;
if let Some(key) = key {
if keys::is_reserved(key) {
return Err(ConfError::ReservedKey {
key: keys::key_event_desc(key),
});
}
verb = verb.with_key(key);
}
if let Some(shortcut) = &vc.shortcut {
verb.names.push(shortcut.clone());
}
verb.selection_condition = match vc.apply_to.as_deref() {
Some("file") => SelectionType::File,
Some("directory") => SelectionType::Directory,
Some("any") => SelectionType::Any,
None => SelectionType::Any,
Some(s) => {
return Err(ConfError::InvalidVerbConf {
details: format!("{:?} isn't a valid value of apply_to", s),
});
}
};
Ok(verb)
}
}