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
use crate::{Child, Cmd};
use std::fmt;
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug)]
pub struct Error(String);
impl std::error::Error for Error {}
pub(crate) trait Context<T> {
fn cmd_context(self, cmd: &Cmd) -> Result<T>;
fn proc_context(self, proc: &Child) -> Result<T>;
}
impl<T, E: fmt::Display> Context<T> for Result<T, E> {
fn cmd_context(self, cmd: &Cmd) -> Result<T> {
self.map_err(|err| Error::cmd(cmd, &err))
}
fn proc_context(self, proc: &Child) -> Result<T> {
self.map_err(|err| Error::proc(proc, &err))
}
}
impl Error {
fn new(msg: String, log_level: Option<log::Level>) -> Self {
let me = Self(msg);
if let Some(level) = log_level {
log::log!(level, "[ERROR] {}", me.0);
}
me
}
fn cmd(cmd: &Cmd, message: &dyn fmt::Display) -> Self {
let msg = format!("{}\nCommand: {}", message, cmd);
Self::new(msg, cmd.0.log_err)
}
pub(crate) fn proc(proc: &Child, message: &dyn fmt::Display) -> Self {
let msg = format!("{}\nProcess: {}", message, proc);
Self::new(msg, proc.cmd.0.log_err)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}