cql2_cli/
lib.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use anyhow::{anyhow, Result};
use clap::{ArgAction, Parser, ValueEnum};
use cql2::{Expr, Validator};
use std::io::Read;

/// The CQL2 command-line interface.
#[derive(Debug, Parser)]
#[command(version, about, long_about = None)]
pub struct Cli {
    /// The input CQL2
    ///
    /// If not provided, or `-`, the CQL2 will be read from standard input. The
    /// type (json or text) will be auto-detected. To specify a format, use
    /// --input-format.
    input: Option<String>,

    /// The input format.
    ///
    /// If not provided, the format will be auto-detected from the input.
    #[arg(short, long)]
    input_format: Option<InputFormat>,

    /// The output format.
    ///
    /// If not provided, the format will be the same as the input.
    #[arg(short, long)]
    output_format: Option<OutputFormat>,

    /// Validate the CQL2
    #[arg(long, default_value_t = true, action = ArgAction::Set)]
    validate: bool,

    /// Verbosity.
    ///
    /// Provide this argument several times to turn up the chatter.
    #[arg(short, long, action = ArgAction::Count)]
    verbose: u8,
}

/// The input CQL2 format.
#[derive(Debug, ValueEnum, Clone)]
pub enum InputFormat {
    /// cql2-json
    Json,

    /// cql2-text
    Text,
}

/// The output CQL2 format.
#[derive(Debug, ValueEnum, Clone)]
enum OutputFormat {
    /// cql2-json, pretty-printed
    JsonPretty,

    /// cql2-json, compact
    Json,

    /// cql2-text
    Text,

    /// SQL
    Sql,
}

impl Cli {
    /// Runs the cli.
    ///
    /// # Examples
    ///
    /// ```
    /// use cql2_cli::Cli;
    /// use clap::Parser;
    ///
    /// let cli = Cli::try_parse_from(&["cql2", "landsat:scene_id = 'LC82030282019133LGN00'"]).unwrap();
    /// cli.run();
    /// ```
    pub fn run(self) {
        if let Err(err) = self.run_inner() {
            eprintln!("{}", err);
            std::process::exit(1)
        }
    }

    pub fn run_inner(self) -> Result<()> {
        let input = self
            .input
            .and_then(|input| if input == "-" { None } else { Some(input) })
            .map(Ok)
            .unwrap_or_else(read_stdin)?;
        let input_format = self.input_format.unwrap_or_else(|| {
            if input.starts_with('{') {
                InputFormat::Json
            } else {
                InputFormat::Text
            }
        });
        let expr: Expr = match input_format {
            InputFormat::Json => cql2::parse_json(&input)?,
            InputFormat::Text => match cql2::parse_text(&input) {
                Ok(expr) => expr,
                Err(err) => {
                    return Err(anyhow!("[ERROR] Parsing error: {input}\n{err}"));
                }
            },
        };
        if self.validate {
            let validator = Validator::new().unwrap();
            let value = serde_json::to_value(&expr).unwrap();
            if let Err(error) = validator.validate(&value) {
                return Err(anyhow!(
                    "[ERROR] Invalid CQL2: {input}\n{}",
                    match self.verbose {
                        0 => "For more detailed validation information, use -v".to_string(),
                        1 => format!("For more detailed validation information, use -vv\n{error}"),
                        2 =>
                            format!("For more detailed validation information, use -vvv\n{error:#}"),
                        _ => {
                            let detailed_output = error.detailed_output();
                            format!("{detailed_output:#}")
                        }
                    }
                ));
            }
        }
        let output_format = self.output_format.unwrap_or(match input_format {
            InputFormat::Json => OutputFormat::Json,
            InputFormat::Text => OutputFormat::Text,
        });
        match output_format {
            OutputFormat::JsonPretty => serde_json::to_writer_pretty(std::io::stdout(), &expr)?,
            OutputFormat::Json => serde_json::to_writer(std::io::stdout(), &expr)?,
            OutputFormat::Text => print!("{}", expr.to_text()?),
            OutputFormat::Sql => serde_json::to_writer_pretty(std::io::stdout(), &expr.to_sql()?)?,
        }
        println!();
        Ok(())
    }
}

fn read_stdin() -> Result<String> {
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf)?;
    Ok(buf)
}