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
use std::{io, process::Output};
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(
"unexpected process output: intent: `{intent}`, stdout: `{stdout}`, stderr: `{stderr}`"
)]
UnexpectedTmuxOutput {
intent: &'static str,
stdout: String,
stderr: String,
},
#[error("unexpected tmux config: `{0}`")]
TmuxConfig(&'static str),
#[error("failed parsing: `{intent}`")]
ParseError {
desc: &'static str,
intent: &'static str,
err: nom::Err<nom::error::Error<String>>,
},
#[error("failed parsing utf-8 string: `{source}`")]
Utf8 {
#[from]
source: std::string::FromUtf8Error,
},
#[error("failed with io: `{source}`")]
Io {
#[from]
source: io::Error,
},
}
#[must_use]
pub fn map_add_intent(
desc: &'static str,
intent: &'static str,
nom_err: nom::Err<nom::error::Error<&str>>,
) -> Error {
Error::ParseError {
desc,
intent,
err: nom_err.to_owned(),
}
}
pub fn check_empty_process_output(
output: &Output,
intent: &'static str,
) -> std::result::Result<(), Error> {
if !output.stdout.is_empty() || !output.stderr.is_empty() {
let stdout = String::from_utf8_lossy(&output.stdout[..]).to_string();
let stderr = String::from_utf8_lossy(&output.stderr[..]).to_string();
return Err(Error::UnexpectedTmuxOutput {
intent,
stdout,
stderr,
});
}
Ok(())
}