Skip to main content

gq_cli/args/
input_data.rs

1use std::io::{BufReader, Read};
2
3use clap::{Args, ValueEnum};
4use clio::Input;
5use gq_core::data::Data;
6
7#[derive(Debug, Clone, ValueEnum)]
8pub enum InputType {
9    Json,
10    Yaml,
11}
12
13#[derive(Debug, Args)]
14pub struct InputData {
15    /// Input data file, use '-' for stdin
16    #[clap(long, short, value_parser, default_value = "-")]
17    pub input: Input,
18
19    /// Input data type
20    #[clap(long, short, default_value_t = InputType::Json)]
21    #[arg(value_enum)]
22    pub r#type: InputType,
23}
24
25impl TryFrom<InputData> for Data<'_> {
26    type Error = clio::Error;
27
28    fn try_from(input_data: InputData) -> Result<Self, Self::Error> {
29        let mut buf_reader = BufReader::new(input_data.input);
30        let mut buffer = String::new();
31        buf_reader.read_to_string(&mut buffer)?;
32
33        let result = match input_data.r#type {
34            InputType::Json => Data::json(buffer.into()),
35            InputType::Yaml => Data::yaml(buffer.into()),
36        };
37
38        Ok(result)
39    }
40}