binary cleanup

This commit is contained in:
nora 2022-04-28 20:51:56 +02:00
parent 28c691da5b
commit c5e63a743a
3 changed files with 158 additions and 61 deletions

134
Cargo.lock generated
View file

@ -3,13 +3,10 @@
version = 3
[[package]]
name = "ansi_term"
version = "0.11.0"
name = "anyhow"
version = "1.0.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
dependencies = [
"winapi",
]
checksum = "08f9b8508dccb7687a1d6c4ce66b2b0ecef467c94667de27d8d7fe1f8d2a9cdc"
[[package]]
name = "atty"
@ -73,13 +70,48 @@ version = "2.33.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002"
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",
"bitflags",
"clap_derive",
"clap_lex",
"indexmap",
"lazy_static",
"strsim",
"textwrap",
"unicode-width",
"vec_map",
"termcolor",
"textwrap 0.15.0",
]
[[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]]
@ -90,7 +122,7 @@ checksum = "1604dafd25fba2fe2d5895a9da139f8dc9b319a5fe5354ca137cbbce4e178d10"
dependencies = [
"atty",
"cast",
"clap",
"clap 2.33.3",
"criterion-plot",
"csv",
"itertools",
@ -197,6 +229,18 @@ version = "1.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "hermit-abi"
version = "0.1.19"
@ -206,6 +250,16 @@ dependencies = [
"libc",
]
[[package]]
name = "indexmap"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f647032dfaa1f8b6dc29bd3edb7bbef4861b8b8007ebb118d6db284fd59f6ee"
dependencies = [
"autocfg",
"hashbrown",
]
[[package]]
name = "itertools"
version = "0.10.3"
@ -247,7 +301,8 @@ dependencies = [
name = "jsonformat-cli"
version = "0.1.0"
dependencies = [
"clap",
"anyhow",
"clap 3.1.12",
"jsonformat",
]
@ -312,6 +367,12 @@ version = "11.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575"
[[package]]
name = "os_str_bytes"
version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64"
[[package]]
name = "plotters"
version = "0.3.1"
@ -340,6 +401,30 @@ dependencies = [
"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]]
name = "proc-macro2"
version = "1.0.37"
@ -479,9 +564,9 @@ dependencies = [
[[package]]
name = "strsim"
version = "0.8.0"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
@ -494,6 +579,15 @@ dependencies = [
"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]]
name = "textwrap"
version = "0.11.0"
@ -503,6 +597,12 @@ dependencies = [
"unicode-width",
]
[[package]]
name = "textwrap"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"
[[package]]
name = "tinytemplate"
version = "1.2.1"
@ -526,10 +626,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "vec_map"
version = "0.8.2"
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "walkdir"

View file

@ -16,7 +16,8 @@ categories = ["command-line-utilities"]
[dependencies]
jsonformat = { path = "..", version = "2.0.0" }
clap = "2.33.3"
clap = { version = "3.1.12", features = ["derive"] }
anyhow = "1.0.57"
[[bin]]
name = "jsonformat"

View file

@ -1,45 +1,52 @@
use std::{
error::Error,
fs::File,
io,
io::{BufReader, BufWriter, Read, Write},
path::PathBuf,
};
use clap::clap_app;
use anyhow::Context;
use clap::Parser;
use jsonformat::{format_reader_writer, Indentation};
fn main() -> Result<(), Box<dyn Error>> {
let matches = clap_app!(jsonformat =>
(version: "1.1")
(author: "nilstrieb <nilstrieb@gmail.com>")
(about: "Formats json from stdin or from a file")
(@arg stdout: -s --stdout "Output the result to stdout instead of the default output file. Windows only.")
(@arg indentation: -i --indent +takes_value "Set the indentation used (\\s for space, \\t for tab)")
(@arg output: -o --output +takes_value "The output file for the formatted json")
(@arg input: "The input file to format")
)
.get_matches();
#[derive(Parser)]
#[clap(author, about, version)]
struct Options {
#[clap(short, long)]
indentation: Option<String>,
#[clap(short, long)]
output: Option<PathBuf>,
input: Option<PathBuf>,
}
fn main() -> anyhow::Result<()> {
let options = Options::parse();
// Note: on-stack dynamic dispatch
let (mut file, mut stdin);
let reader: &mut dyn Read = match matches.value_of("input") {
// ugly af but works
let (mut file, stdin, mut stdin_lock);
let reader: &mut dyn Read = match &options.input {
Some(path) => {
file = File::open(path)?;
file = File::open(path)
.context(format!("Name: {}", path.display()))
.context("Open input file")?;
&mut file
}
None => {
stdin = std::io::stdin();
&mut stdin
stdin = io::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
.to_lowercase()
.chars()
.filter(|c| ['s', 't'].contains(c))
.collect::<String>()
.replace("s", " ")
.replace("t", "\t")
.replace('s', " ")
.replace('t', "\t")
});
let indent = match replaced_indent {
@ -47,36 +54,25 @@ fn main() -> Result<(), Box<dyn Error>> {
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
let (mut file, mut stdout);
let writer: &mut dyn Write = match output {
Some(filename) => {
file = File::create(filename)?;
let (mut file, stdout, mut stdout_lock);
let writer: &mut dyn Write = match &options.output {
Some(path) => {
file = File::create(path)
.context(path.display().to_string())
.context("Open output file")?;
&mut file
}
None => {
stdout = std::io::stdout();
&mut stdout
stdout = io::stdout();
stdout_lock = stdout.lock();
&mut stdout_lock
}
};
let mut reader = BufReader::new(reader);
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(())
}