mirror of
https://github.com/Noratrieb/jsonformat.git
synced 2026-01-14 14:15:03 +01:00
binary cleanup
This commit is contained in:
parent
28c691da5b
commit
c5e63a743a
3 changed files with 158 additions and 61 deletions
134
Cargo.lock
generated
134
Cargo.lock
generated
|
|
@ -3,13 +3,10 @@
|
||||||
version = 3
|
version = 3
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ansi_term"
|
name = "anyhow"
|
||||||
version = "0.11.0"
|
version = "1.0.57"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
|
checksum = "08f9b8508dccb7687a1d6c4ce66b2b0ecef467c94667de27d8d7fe1f8d2a9cdc"
|
||||||
dependencies = [
|
|
||||||
"winapi",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "atty"
|
name = "atty"
|
||||||
|
|
@ -73,13 +70,48 @@ version = "2.33.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002"
|
checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ansi_term",
|
"bitflags",
|
||||||
|
"textwrap 0.11.0",
|
||||||
|
"unicode-width",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap"
|
||||||
|
version = "3.1.12"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7c167e37342afc5f33fd87bbc870cedd020d2a6dffa05d45ccd9241fbdd146db"
|
||||||
|
dependencies = [
|
||||||
"atty",
|
"atty",
|
||||||
"bitflags",
|
"bitflags",
|
||||||
|
"clap_derive",
|
||||||
|
"clap_lex",
|
||||||
|
"indexmap",
|
||||||
|
"lazy_static",
|
||||||
"strsim",
|
"strsim",
|
||||||
"textwrap",
|
"termcolor",
|
||||||
"unicode-width",
|
"textwrap 0.15.0",
|
||||||
"vec_map",
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap_derive"
|
||||||
|
version = "3.1.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a3aab4734e083b809aaf5794e14e756d1c798d2c69c7f7de7a09a2f5214993c1"
|
||||||
|
dependencies = [
|
||||||
|
"heck",
|
||||||
|
"proc-macro-error",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap_lex"
|
||||||
|
version = "0.1.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "189ddd3b5d32a70b35e7686054371742a937b0d99128e76dde6340210e966669"
|
||||||
|
dependencies = [
|
||||||
|
"os_str_bytes",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -90,7 +122,7 @@ checksum = "1604dafd25fba2fe2d5895a9da139f8dc9b319a5fe5354ca137cbbce4e178d10"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"atty",
|
"atty",
|
||||||
"cast",
|
"cast",
|
||||||
"clap",
|
"clap 2.33.3",
|
||||||
"criterion-plot",
|
"criterion-plot",
|
||||||
"csv",
|
"csv",
|
||||||
"itertools",
|
"itertools",
|
||||||
|
|
@ -197,6 +229,18 @@ version = "1.8.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7"
|
checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hashbrown"
|
||||||
|
version = "0.11.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "heck"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hermit-abi"
|
name = "hermit-abi"
|
||||||
version = "0.1.19"
|
version = "0.1.19"
|
||||||
|
|
@ -206,6 +250,16 @@ dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "indexmap"
|
||||||
|
version = "1.8.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0f647032dfaa1f8b6dc29bd3edb7bbef4861b8b8007ebb118d6db284fd59f6ee"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
"hashbrown",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "itertools"
|
name = "itertools"
|
||||||
version = "0.10.3"
|
version = "0.10.3"
|
||||||
|
|
@ -247,7 +301,8 @@ dependencies = [
|
||||||
name = "jsonformat-cli"
|
name = "jsonformat-cli"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"anyhow",
|
||||||
|
"clap 3.1.12",
|
||||||
"jsonformat",
|
"jsonformat",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -312,6 +367,12 @@ version = "11.1.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575"
|
checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "os_str_bytes"
|
||||||
|
version = "6.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "plotters"
|
name = "plotters"
|
||||||
version = "0.3.1"
|
version = "0.3.1"
|
||||||
|
|
@ -340,6 +401,30 @@ dependencies = [
|
||||||
"plotters-backend",
|
"plotters-backend",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro-error"
|
||||||
|
version = "1.0.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro-error-attr",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
"version_check",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro-error-attr"
|
||||||
|
version = "1.0.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"version_check",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.37"
|
version = "1.0.37"
|
||||||
|
|
@ -479,9 +564,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "strsim"
|
name = "strsim"
|
||||||
version = "0.8.0"
|
version = "0.10.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
|
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
|
|
@ -494,6 +579,15 @@ dependencies = [
|
||||||
"unicode-xid",
|
"unicode-xid",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "termcolor"
|
||||||
|
version = "1.1.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
|
||||||
|
dependencies = [
|
||||||
|
"winapi-util",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "textwrap"
|
name = "textwrap"
|
||||||
version = "0.11.0"
|
version = "0.11.0"
|
||||||
|
|
@ -503,6 +597,12 @@ dependencies = [
|
||||||
"unicode-width",
|
"unicode-width",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "textwrap"
|
||||||
|
version = "0.15.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tinytemplate"
|
name = "tinytemplate"
|
||||||
version = "1.2.1"
|
version = "1.2.1"
|
||||||
|
|
@ -526,10 +626,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
|
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "vec_map"
|
name = "version_check"
|
||||||
version = "0.8.2"
|
version = "0.9.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
|
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "walkdir"
|
name = "walkdir"
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,8 @@ categories = ["command-line-utilities"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
jsonformat = { path = "..", version = "2.0.0" }
|
jsonformat = { path = "..", version = "2.0.0" }
|
||||||
clap = "2.33.3"
|
clap = { version = "3.1.12", features = ["derive"] }
|
||||||
|
anyhow = "1.0.57"
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "jsonformat"
|
name = "jsonformat"
|
||||||
|
|
|
||||||
|
|
@ -1,45 +1,52 @@
|
||||||
use std::{
|
use std::{
|
||||||
error::Error,
|
|
||||||
fs::File,
|
fs::File,
|
||||||
|
io,
|
||||||
io::{BufReader, BufWriter, Read, Write},
|
io::{BufReader, BufWriter, Read, Write},
|
||||||
|
path::PathBuf,
|
||||||
};
|
};
|
||||||
|
|
||||||
use clap::clap_app;
|
use anyhow::Context;
|
||||||
|
use clap::Parser;
|
||||||
use jsonformat::{format_reader_writer, Indentation};
|
use jsonformat::{format_reader_writer, Indentation};
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn Error>> {
|
#[derive(Parser)]
|
||||||
let matches = clap_app!(jsonformat =>
|
#[clap(author, about, version)]
|
||||||
(version: "1.1")
|
struct Options {
|
||||||
(author: "nilstrieb <nilstrieb@gmail.com>")
|
#[clap(short, long)]
|
||||||
(about: "Formats json from stdin or from a file")
|
indentation: Option<String>,
|
||||||
(@arg stdout: -s --stdout "Output the result to stdout instead of the default output file. Windows only.")
|
#[clap(short, long)]
|
||||||
(@arg indentation: -i --indent +takes_value "Set the indentation used (\\s for space, \\t for tab)")
|
output: Option<PathBuf>,
|
||||||
(@arg output: -o --output +takes_value "The output file for the formatted json")
|
input: Option<PathBuf>,
|
||||||
(@arg input: "The input file to format")
|
}
|
||||||
)
|
|
||||||
.get_matches();
|
fn main() -> anyhow::Result<()> {
|
||||||
|
let options = Options::parse();
|
||||||
|
|
||||||
// Note: on-stack dynamic dispatch
|
// Note: on-stack dynamic dispatch
|
||||||
let (mut file, mut stdin);
|
// ugly af but works
|
||||||
let reader: &mut dyn Read = match matches.value_of("input") {
|
let (mut file, stdin, mut stdin_lock);
|
||||||
|
let reader: &mut dyn Read = match &options.input {
|
||||||
Some(path) => {
|
Some(path) => {
|
||||||
file = File::open(path)?;
|
file = File::open(path)
|
||||||
|
.context(format!("Name: {}", path.display()))
|
||||||
|
.context("Open input file")?;
|
||||||
&mut file
|
&mut file
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
stdin = std::io::stdin();
|
stdin = io::stdin();
|
||||||
&mut stdin
|
stdin_lock = stdin.lock();
|
||||||
|
&mut stdin_lock
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let replaced_indent = matches.value_of("indentation").map(|value| {
|
let replaced_indent = options.indentation.map(|value| {
|
||||||
value
|
value
|
||||||
.to_lowercase()
|
.to_lowercase()
|
||||||
.chars()
|
.chars()
|
||||||
.filter(|c| ['s', 't'].contains(c))
|
.filter(|c| ['s', 't'].contains(c))
|
||||||
.collect::<String>()
|
.collect::<String>()
|
||||||
.replace("s", " ")
|
.replace('s', " ")
|
||||||
.replace("t", "\t")
|
.replace('t', "\t")
|
||||||
});
|
});
|
||||||
|
|
||||||
let indent = match replaced_indent {
|
let indent = match replaced_indent {
|
||||||
|
|
@ -47,36 +54,25 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
None => Indentation::Default,
|
None => Indentation::Default,
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut output = matches.value_of("output");
|
|
||||||
let mut windows_output_default_file: Option<String> = None;
|
|
||||||
|
|
||||||
#[cfg(windows)]
|
|
||||||
if !matches.is_present("stdout") {
|
|
||||||
if let Some(file) = matches.value_of("input") {
|
|
||||||
// on windows, set the default output file if no stdout flag is provided
|
|
||||||
// this makes it work with drag and drop in windows explorer
|
|
||||||
windows_output_default_file = Some(file.replace(".json", "_f.json"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
output = windows_output_default_file.as_deref().or(output);
|
|
||||||
|
|
||||||
// Note: on-stack dynamic dispatch
|
// Note: on-stack dynamic dispatch
|
||||||
let (mut file, mut stdout);
|
let (mut file, stdout, mut stdout_lock);
|
||||||
let writer: &mut dyn Write = match output {
|
let writer: &mut dyn Write = match &options.output {
|
||||||
Some(filename) => {
|
Some(path) => {
|
||||||
file = File::create(filename)?;
|
file = File::create(path)
|
||||||
|
.context(path.display().to_string())
|
||||||
|
.context("Open output file")?;
|
||||||
&mut file
|
&mut file
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
stdout = std::io::stdout();
|
stdout = io::stdout();
|
||||||
&mut stdout
|
stdout_lock = stdout.lock();
|
||||||
|
&mut stdout_lock
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut reader = BufReader::new(reader);
|
let mut reader = BufReader::new(reader);
|
||||||
let mut writer = BufWriter::new(writer);
|
let mut writer = BufWriter::new(writer);
|
||||||
format_reader_writer(&mut reader, &mut writer, indent)?;
|
format_reader_writer(&mut reader, &mut writer, indent).context("failed to read or write")?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue