Skip to content

Commit

Permalink
add support for json output
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptaliagy committed Jul 1, 2024
1 parent 5b02dc1 commit af2d413
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 7 deletions.
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ toml = "0.8"
clap = { version = "4.5", features = ["derive", "usage", "help"] }
thiserror = "1.0.61"
colored = { version = "2.1.0", optional = true }
serde_json = { version = "1.0.120", features = [
"indexmap",
"preserve_order",
"raw_value",
"unbounded_depth",
], optional = true }

[features]
default = []
default = ["json"]
color = ["colored"]
json = ["dep:serde_json"]


[lib]
Expand Down
8 changes: 6 additions & 2 deletions src/legacy.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clap::Parser;
use colored::Colorize;
use std::process::exit;
use tq::OutputType;

fn main() {
eprintln!(
Expand All @@ -9,7 +10,7 @@ fn main() {
);
eprintln!(
"{}",
"The \"tomlq\" binary will be removed from this package starting in version 0.2.0".yellow()
"The \"tomlq\" binary will be removed from this package starting in version 0.2.0, scheduled for January 1, 2025".yellow()
);

let app = tq::Cli::parse();
Expand All @@ -26,7 +27,10 @@ fn main() {

exit(match x {
Ok(needle) => {
println!("{}", format!("{}", needle).trim_matches('"'));
match app.output {
OutputType::Toml => println!("{}", format!("{}", needle).trim_matches('"')),
OutputType::Json => println!("{}", serde_json::to_string(&needle).unwrap()),
}
0
}
Err(e) => {
Expand Down
21 changes: 18 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ pub enum TqError {
PatternNotFoundError { pattern: String },
}

#[derive(Default, Debug, Clone, clap::ValueEnum)]
pub enum OutputType {
#[default]
Toml,
#[cfg(feature = "json")]
Json,
}

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct Cli {
Expand All @@ -28,12 +36,19 @@ pub struct Cli {

/// Field to read from the TOML file
pub pattern: String,
// /// The TOML File URL to read
// #[arg(short, long, value_name = "URL_PATH")]
// pub url: String,

/// The output type. Default is TOML, but supports outputting in different formats.
#[arg(short, long, value_name = "OUTPUT_TYPE", default_value = "toml")]
pub output: OutputType,
}

pub fn extract_pattern<'a>(toml_file: &'a Value, pattern: &str) -> Result<&'a Value> {
if pattern.is_empty() || pattern == "." {
return Ok(toml_file);
}

let pattern = pattern.trim_start_matches('.');

pattern
.split('.')
.fold(Some(toml_file), |acc, key| match acc {
Expand Down
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clap::Parser;
use std::process::exit;
use tq::OutputType;

fn main() {
let app = tq::Cli::parse();
Expand All @@ -16,7 +17,11 @@ fn main() {

exit(match x {
Ok(needle) => {
println!("{}", format!("{}", needle).trim_matches('"'));
match app.output {
OutputType::Toml => println!("{}", format!("{}", needle).trim_matches('"')),
OutputType::Json => println!("{}", serde_json::to_string(&needle).unwrap()),
}

0
}
Err(e) => {
Expand Down

0 comments on commit af2d413

Please sign in to comment.