-
Notifications
You must be signed in to change notification settings - Fork 471
/
Copy pathexec.rs
111 lines (95 loc) · 2.53 KB
/
exec.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
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
use std::{collections::BTreeMap, fmt::{self, Debug, Display}};
use anyhow::bail;
use serde::{de::{self, Visitor}, Deserializer};
#[derive(Clone, Debug)]
pub struct Exec {
pub cmd: String,
pub args: Vec<String>,
pub named: BTreeMap<String, String>,
}
impl TryFrom<&str> for Exec {
type Error = anyhow::Error;
fn try_from(s: &str) -> Result<Self, Self::Error> {
let s = shell_words::split(s)?;
if s.is_empty() {
bail!("`exec` cannot be empty");
}
let mut exec = Self { cmd: s[0].clone(), args: Vec::new(), named: BTreeMap::new() };
for arg in s.into_iter().skip(1) {
if arg.starts_with("--") {
let mut arg = arg.splitn(2, '=');
let key = arg.next().unwrap().trim_start_matches('-');
let val = arg.next().unwrap_or("").to_string();
exec.named.insert(key.to_string(), val);
} else {
exec.args.push(arg);
}
}
Ok(exec)
}
}
impl Display for Exec {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.cmd)?;
if !self.args.is_empty() {
write!(f, " {}", self.args.join(" "))?;
}
for (k, v) in &self.named {
write!(f, " --{k}")?;
if !v.is_empty() {
write!(f, "={v}")?;
}
}
Ok(())
}
}
impl Exec {
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<Exec>, D::Error>
where
D: Deserializer<'de>,
{
struct ExecVisitor;
impl<'de> Visitor<'de> for ExecVisitor {
type Value = Vec<Exec>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a exec string, e.g. tab_switch 0")
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: de::SeqAccess<'de>,
{
let mut execs = Vec::new();
while let Some(value) = &seq.next_element::<String>()? {
execs.push(Exec::try_from(value.as_str()).map_err(de::Error::custom)?);
}
Ok(execs)
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(vec![Exec::try_from(value).map_err(de::Error::custom)?])
}
}
deserializer.deserialize_any(ExecVisitor)
}
}
impl Exec {
#[inline]
pub fn call(cwd: &str, args: Vec<String>) -> Self {
Exec { cmd: cwd.to_owned(), args, named: Default::default() }
}
#[inline]
pub fn call_named(cwd: &str, named: BTreeMap<String, String>) -> Self {
Exec { cmd: cwd.to_owned(), args: Default::default(), named }
}
#[inline]
pub fn vec(self) -> Vec<Self> { vec![self] }
#[inline]
pub fn with_bool(mut self, name: &str, state: bool) -> Self {
if state {
self.named.insert(name.to_string(), "".to_string());
}
self
}
}