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
use crate::{JcWrapper, CmdOutput, Error};
use std::process::{Stdio, Command};
use serde::Deserialize;

impl<T: for<'a> Deserialize<'a>> JcWrapper<T> for Command {

    /// Called on a Command.
    fn parse(&mut self, output_type: CmdOutput) -> Result<T, Box<dyn std::error::Error>> {

        let output = self.stdout(Stdio::piped())
            .spawn()?;
        
        if let Some(text) = output.stdout {
            let jc = Command::new("jc")
                .arg(output_type.get_flag())
                .stdin(Stdio::from(text))
                .output()?;

            Ok(serde_json::from_slice(&jc.stdout)?)
        } else {
            Err(Error::NoOutput.into())
        }
    }

}