command_error/
process_wrap.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
use std::fmt::Debug;
use std::fmt::Display;
use std::process::Output;

use process_wrap::std::StdChildWrapper;
use process_wrap::std::StdCommandWrap;

use crate::ChildContext;
use crate::CommandExt;
use crate::Error;
use crate::ExecError;
use crate::OutputContext;
use crate::OutputConversionError;
use crate::OutputLike;
use crate::Utf8ProgramAndArgs;

impl CommandExt for StdCommandWrap {
    type Error = Error;
    type Child = ChildContext<Box<dyn StdChildWrapper>>;

    fn log(&self) -> Result<(), Self::Error> {
        #[cfg(feature = "tracing")]
        {
            let command: Utf8ProgramAndArgs = self.command().into();
            tracing::debug!(%command, "Executing command");
        }
        Ok(())
    }

    fn output_checked_as<O, R, E>(
        &mut self,
        succeeded: impl Fn(OutputContext<O>) -> Result<R, E>,
    ) -> Result<R, E>
    where
        O: Debug + OutputLike + TryFrom<Output> + Send + Sync + 'static,
        <O as TryFrom<Output>>::Error: Display + Send + Sync,
        E: From<Self::Error> + Send + Sync,
    {
        self.log()?;
        let displayed: Utf8ProgramAndArgs = self.command().into();
        let child = match self.spawn() {
            Ok(child) => child,
            Err(inner) => {
                return Err(Error::from(ExecError::new(Box::new(displayed), inner)).into());
            }
        };

        match child.wait_with_output() {
            Ok(output) => match output.try_into() {
                Ok(output) => succeeded(OutputContext {
                    output,
                    command: Box::new(displayed),
                }),
                Err(error) => Err(Error::from(OutputConversionError {
                    command: Box::new(displayed),
                    inner: Box::new(error),
                })
                .into()),
            },
            Err(inner) => Err(Error::from(ExecError {
                command: Box::new(displayed),
                inner,
            })
            .into()),
        }
    }

    fn status_checked_as<R, E>(
        &mut self,
        succeeded: impl Fn(OutputContext<std::process::ExitStatus>) -> Result<R, E>,
    ) -> Result<R, E>
    where
        E: From<Self::Error>,
    {
        self.log()?;
        let displayed: Utf8ProgramAndArgs = self.command().into();
        let mut child = match self.spawn() {
            Ok(child) => child,
            Err(inner) => {
                return Err(Error::from(ExecError::new(Box::new(displayed), inner)).into());
            }
        };

        match child.wait() {
            Ok(status) => succeeded(OutputContext {
                output: status,
                command: Box::new(displayed),
            }),
            Err(inner) => Err(Error::from(ExecError {
                command: Box::new(displayed),
                inner,
            })
            .into()),
        }
    }

    fn spawn_checked(&mut self) -> Result<Self::Child, Self::Error> {
        let displayed: Utf8ProgramAndArgs = self.command().into();
        match self.spawn() {
            Ok(child) => Ok(ChildContext {
                child,
                command: Box::new(displayed),
            }),
            Err(inner) => Err(Error::from(ExecError {
                command: Box::new(displayed),
                inner,
            })),
        }
    }
}