bundle_sources/
utils.rs

1// Copyright 2020 Ian Jackson
2// SPDX-License-Identifier: GPL-3.0-or-later
3// There is NO WARRANTY.
4
5use crate::imports::*;
6
7type E = anyhow::Error;
8
9#[throws(E)]
10pub fn check_exit_status(st : ExitStatus) {
11  if !st.success() {
12    throw!(anyhow!("program failed: {:?}",st))
13  }
14}
15
16#[throws(E)]
17pub fn run_cmd(mut cmd : Command) {
18  let st = cmd.status()?;
19  check_exit_status(st)?;
20}
21
22pub fn tar_command() -> Command {
23  let mut cmd = Command::new("tar");
24  cmd.args(&["--owner=root:0","--group=root:0","--mode=ug=rwX,o=rX"]);
25  cmd
26}
27
28pub trait ErrorMap<T,E0> {
29  type R;
30  fn cxm<S,MF>(self, mf : MF) -> Self::R
31  where MF : FnOnce() -> S,
32        S : ToOwned<Owned=String>,
33        String : Borrow<S>;
34}
35
36impl<T,E0> ErrorMap<T,E0> for Result<T,E0>
37  where E0 : Into<E>
38{
39  type R = Result<T,E>;
40  fn cxm<S,MF>(self, mf : MF) -> Self::R
41  where MF : FnOnce() -> S,
42        S : ToOwned<Owned=String>,
43        String : Borrow<S>
44  {
45    match self {
46      Ok(y) => { Ok(y) }
47      Err(e0) => {
48        let e1 : E = e0.into();
49        let e2 = e1.context(mf().to_owned());
50        Err(e2)
51      }
52    }
53  }
54}