mx-sanebot: factor out a helper when invoking processes

This commit is contained in:
Colin 2023-04-29 09:35:07 +00:00
parent 10a100c961
commit f300cb1202

View File

@ -186,27 +186,28 @@ impl From<tt::Request> for Request {
} }
} }
fn exec_stdout(program: &str, args: &[&str]) -> Option<String> {
process::Command::new(program)
.args(args)
.output()
.ok()
.and_then(|output|
str::from_utf8(&output.stdout).ok().map(ToOwned::to_owned)
)
}
impl Request { impl Request {
fn evaluate(self) -> Response { fn evaluate(self) -> Response {
match self { match self {
Request::Help => Response::Help, Request::Help => Response::Help,
Request::Bt => Response::Bt( Request::Bt => Response::Bt(
process::Command::new("sane-bt-show") exec_stdout("sane-bt-show", &[])
.output() .unwrap_or_else(||
.ok()
.and_then(|output|
str::from_utf8(&output.stdout).ok().map(ToOwned::to_owned)
).unwrap_or_else(||
"failed to retrieve torrent status".to_owned()) "failed to retrieve torrent status".to_owned())
), ),
Request::BtSearch(phrase) => Response::BtSearch( Request::BtSearch(phrase) => Response::BtSearch(
process::Command::new("sane-bt-search") exec_stdout("sane-bt-search", &[&*phrase])
.args([&*phrase]) .unwrap_or_else(||
.output()
.ok()
.and_then(|output|
str::from_utf8(&output.stdout).ok().map(ToOwned::to_owned)
).unwrap_or_else(||
"failed to complete torrent search".to_owned()) "failed to complete torrent search".to_owned())
), ),
} }