Skip to content

Commit

Permalink
Add option: -c
Browse files Browse the repository at this point in the history
  • Loading branch information
fuyutarow committed Jun 7, 2021
1 parent 194763f commit eb9e95e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/bin/pq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ struct Opt {
/// sort keys of objects on output. it on works when --to option is json, currently
#[structopt(short = "S", long)]
sort_keys: bool,

/// compact instead of pretty-printed output, only when outputting in JSON
#[structopt(short, long)]
compact: bool,
}

fn main() -> anyhow::Result<()> {
Expand All @@ -48,6 +52,7 @@ fn main() -> anyhow::Result<()> {
from,
to,
sort_keys,
compact,
} = Opt::from_args();
let _ = {
let input = if let Some(file) = file_or_stdin {
Expand Down Expand Up @@ -81,7 +86,7 @@ fn main() -> anyhow::Result<()> {
lang.sort_keys();
}

lang.print();
lang.print(compact);
};

Ok(())
Expand Down
3 changes: 2 additions & 1 deletion src/lib/lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl Lang {
self.data = data;
}

pub fn print(&self) -> anyhow::Result<()> {
pub fn print(&self, compact: bool) -> anyhow::Result<()> {
let output = match (&self.to, &self.from == &self.to) {
(LangType::Csv, _) => {
// To pad missing values with null, serialize them to json, deserialize them with polars, and write them to csv from there.
Expand All @@ -159,6 +159,7 @@ impl Lang {
let s = String::from_utf8(v)?;
s
}
(LangType::Json, _) if compact => serde_json::to_string(&self.data).unwrap(),
(LangType::Json, _) => serde_json::to_string_pretty(&self.data).unwrap(),
(_, true) => self.text.to_owned(),
(LangType::Toml, _) => {
Expand Down
9 changes: 9 additions & 0 deletions tests-make/convert.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ tobe = '''
}
'''

[tests.to-json-compact]
script = '''
echo $INPUT | ./target/debug/pq -t json -c
'''
tobe = '''
{"name":"Mew","id":151,"fleeRate":0.1}
'''


[tests.to-toml]
script = '''
echo $INPUT | ./target/debug/pq -t toml
Expand Down

0 comments on commit eb9e95e

Please sign in to comment.