Skip to main content

api_testing_core/
cli_io.rs

1use std::io::Read;
2use std::path::PathBuf;
3
4use anyhow::anyhow;
5
6use crate::Result;
7
8pub fn read_response_bytes(response: &str, stdin: &mut dyn Read) -> Result<Vec<u8>> {
9    if response == "-" {
10        let mut buf = Vec::new();
11        stdin
12            .read_to_end(&mut buf)
13            .map_err(|_| anyhow!("error: failed to read response from stdin"))?;
14        return Ok(buf);
15    }
16
17    let resp_path = PathBuf::from(response);
18    if !resp_path.is_file() {
19        return Err(anyhow!("Response file not found: {}", resp_path.display()));
20    }
21
22    std::fs::read(&resp_path).map_err(|_| {
23        anyhow!(
24            "error: failed to read response file: {}",
25            resp_path.display()
26        )
27    })
28}