use std::path::PathBuf;
use clap::{
CommandFactory,
Parser,
ValueHint,
error::ErrorKind,
};
static QUERY_HELP: &str = r#"
A query is sequence of tokens used to make a selection on a JSON input.
A query must be enclosed by single quotation marks.
The following tokens are available to build up a query:
== Separators ==
Group separator ,
┬
╰→ query '"a","b","c"' will build up an array from sub-queries
== Selectors ==
-- Arrays --
Array index selector [0,2,1]
┬
╰→ indexes can be used in arbitrary order
Array range selector [2:0]
┬
╰→ range can be in natural order [0:2], reversed [2:0],
without lower [:2] or upper bound [0:]
Lens selector |={"a","b"=true,"c"=null,"d"=1,"e"="string"}
┬
╰→ lens can be a combination of one or more selectors with an optional value,
a value being any of boolean | null | number | string
-- Objects --
Key selector "a"
┬
╰→ any valid JSON key
Multi key selector {"a","c","b"}
┬
╰→ keys can be used in arbitrary order
Object index selector {0,2,1}
┬
╰→ indexes can be used in arbitrary order
Object range selector {2:0}
┬
╰→ range can be in natural order {0:2}, reversed {2:0},
without lower {:2} or upper bound {0:}
== Operators ==
Flatten operator ..
┬
╰→ flattens arrays and objects
Keys operator @
┬
╰→ returns the keys of an object or the indexes of an array,
other primitives are returned as is
Pipe in operator |>
┬
╰→ applies the next tokens in parallel on each element of an array
Pipe out operator <|
┬
╰→ stops the parallelization initiated by the pipe in operator
Truncate operator !
┬
╰→ maps the output into simple JSON primitives
boolean | null | number | string | [] | {}
"#;
#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, Parser)]
#[command(
about,
author,
long_about = None,
version
)]
pub(crate) struct Args {
#[arg(
help = "Query to apply to the JSON data",
index = 1,
long_help = QUERY_HELP,
required_unless_present_any = ["no-query"]
)]
pub(crate) query: Option<String>,
#[arg(
help = "JSON file to use",
index = 2,
value_hint = ValueHint::FilePath,
value_name = "OPTIONAL_FILE"
)]
pub(crate) json_file: Option<PathBuf>,
#[arg(
conflicts_with = "validate",
help = "Inline the JSON output",
long = "inline",
short = 'i'
)]
pub(crate) inline: bool,
#[arg(
group = "no-query",
help = "Read the query from file",
long = "query",
long_help = QUERY_HELP,
short = 'q',
value_hint = ValueHint::FilePath,
value_name = "FILE"
)]
pub(crate) query_from_file: Option<PathBuf>,
#[arg(
help = "Write to stdout without JSON double-quotes (string only)",
long = "raw-string",
short = 'r'
)]
pub(crate) raw_string: bool,
#[arg(
conflicts_with = "validate",
help = "Sort the keys of every object in the JSON output",
long = "sort-keys",
short = 'S'
)]
pub(crate) sort_keys: bool,
#[arg(
help = "Read a stream of JSON data line by line",
long = "stream",
short = 's'
)]
pub(crate) stream: bool,
#[arg(
group = "no-query",
help = "Validate the JSON data",
long = "validate",
short = 'v'
)]
pub(crate) validate: bool,
}
impl Args {
pub(crate) fn get() -> Self {
match Self::parse().with_positionals_in_place() {
Ok(args) => args,
Err(message) => Self::command()
.error(ErrorKind::TooManyValues, message)
.exit(),
}
}
fn with_positionals_in_place(mut self) -> Result<Self, String> {
if !self.validate && self.query_from_file.is_none() {
return Ok(self);
}
if let Some(first) = self.query.take() {
if let Some(second) = &self.json_file {
return Err(format!(
"'{first}' and '{}' were both provided, but only one file is read with --validate or --query",
second.display()
));
}
self.json_file = Some(PathBuf::from(first));
}
Ok(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn parse(arguments: &[&str]) -> Result<Args, String> {
Args::parse_from(arguments).with_positionals_in_place()
}
#[test]
fn check_args() {
Args::command().debug_assert();
}
#[test]
fn check_file_is_read_without_a_query() {
let args = parse(&["jql", "--validate", "input.json"]).unwrap();
assert_eq!(args.json_file, Some(PathBuf::from("input.json")));
assert_eq!(args.query, None);
let args = parse(&["jql", "--query", "query.jql", "input.json"]).unwrap();
assert_eq!(args.json_file, Some(PathBuf::from("input.json")));
assert_eq!(args.query, None);
}
#[test]
fn check_query_and_file_are_left_alone() {
let args = parse(&["jql", r#""a""#, "input.json"]).unwrap();
assert_eq!(args.query.as_deref(), Some(r#""a""#));
assert_eq!(args.json_file, Some(PathBuf::from("input.json")));
let args = parse(&["jql", r#""a""#]).unwrap();
assert_eq!(args.query.as_deref(), Some(r#""a""#));
assert_eq!(args.json_file, None);
}
#[test]
fn check_stdin_is_left_alone_without_a_query() {
let args = parse(&["jql", "--validate"]).unwrap();
assert_eq!(args.json_file, None);
assert_eq!(args.query, None);
}
#[test]
fn check_two_files_are_rejected_without_a_query() {
assert!(parse(&["jql", "--validate", "one.json", "two.json"]).is_err());
assert!(parse(&["jql", "--query", "query.jql", "one.json", "two.json"]).is_err());
}
}