From 35332a1b2665b3c812ee495d5fbbb0d2ffa0c71d Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Thu, 28 Apr 2022 21:09:50 +0200 Subject: [PATCH] binary cleanup --- CHANGELOG.md | 5 ++++ Cargo.lock | 2 +- README.md | 3 --- benches/bench.rs | 20 ++++++++++---- jsonformat-cli/Cargo.toml | 2 +- jsonformat-cli/src/main.rs | 5 +++- src/lib.rs | 53 ++++++++++++++++++++++++-------------- 7 files changed, 59 insertions(+), 31 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..9585a00 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +# 2.0.0 + +There are many changes, the two formatting functions have been renamed, `format_reader_writer` now takes +a `W` and `R` instead of `&mut BufReader`, it now always adds a trailing newline. There may be a few more +small changes. \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 43befae..30e9682 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -299,7 +299,7 @@ dependencies = [ [[package]] name = "jsonformat-cli" -version = "0.1.0" +version = "0.2.0" dependencies = [ "anyhow", "clap 3.1.12", diff --git a/README.md b/README.md index b42e9f8..2c31bdd 100644 --- a/README.md +++ b/README.md @@ -30,9 +30,6 @@ OPTIONS: Reads from stdin if no file is supplied. Outputs to stdout if no output file is specified. -On Windows, it writes to a file called `_f.json`, unless the `--stdout` flag is used or a custom output -file is provided. This it to enable drag-and-drop in Windows explorer. - ## Error handling `jsonformat` does not report malformed json - it can't even fully know whether the json is actually malformed. Malformed json is just formatted kind of incorrectly, with no data lost and no crashes. If you find one, open an issue, diff --git a/benches/bench.rs b/benches/bench.rs index e4fa293..e750acf 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -1,21 +1,26 @@ -use std::{fs, io}; +use std::{fs, io, path::PathBuf}; use criterion::{black_box, criterion_group, criterion_main, Criterion}; use jsonformat::{format, format_reader_writer, Indentation}; fn criterion_benchmark(c: &mut Criterion) { - let file = include_str!("large-file.json"); + // using `include_str` makes the benches a lot less reliable for some reason??? + let file = PathBuf::from(file!()) + .parent() + .unwrap() + .join("large-file.json"); + let file = fs::read_to_string(file).unwrap(); c.bench_function("Format json default settings", |b| { b.iter(|| { - let json = format(&file, Indentation::Default); + let json = format(black_box(&file), Indentation::TwoSpace); black_box(json); }) }); c.bench_function("Format json custom indentation", |b| { b.iter(|| { - let json = format(&file, Indentation::Custom("123456")); + let json = format(black_box(&file), Indentation::Custom("123456")); black_box(json); }) }); @@ -24,7 +29,12 @@ fn criterion_benchmark(c: &mut Criterion) { b.iter(|| { let mut writer = Vec::with_capacity(file.len() * 2); - format_reader_writer(file.as_bytes(), &mut writer, Indentation::Default).unwrap(); + format_reader_writer( + black_box(file.as_bytes()), + &mut writer, + Indentation::TwoSpace, + ) + .unwrap(); black_box(writer); }) }); diff --git a/jsonformat-cli/Cargo.toml b/jsonformat-cli/Cargo.toml index 47a4a11..29de058 100644 --- a/jsonformat-cli/Cargo.toml +++ b/jsonformat-cli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "jsonformat-cli" -version = "0.1.0" +version = "0.2.0" authors = ["Nilstrieb "] edition = "2021" license = "MIT" diff --git a/jsonformat-cli/src/main.rs b/jsonformat-cli/src/main.rs index 1d81709..305532e 100644 --- a/jsonformat-cli/src/main.rs +++ b/jsonformat-cli/src/main.rs @@ -12,10 +12,13 @@ use jsonformat::{format_reader_writer, Indentation}; #[derive(Parser)] #[clap(author, about, version)] struct Options { + /// The indentation, s will replaced by a space and t by a tab. ss is the default. #[clap(short, long)] indentation: Option, #[clap(short, long)] + /// The output file output: Option, + /// The input file input: Option, } @@ -51,7 +54,7 @@ fn main() -> anyhow::Result<()> { let indent = match replaced_indent { Some(ref str) => Indentation::Custom(str), - None => Indentation::Default, + None => Indentation::TwoSpace, }; // Note: on-stack dynamic dispatch diff --git a/src/lib.rs b/src/lib.rs index e3d67a2..12d83ef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,11 +15,17 @@ use std::{ #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] pub enum Indentation<'a> { /// Use the default indentation, which is two spaces - Default, + TwoSpace, /// Use a custom indentation String Custom(&'a str), } +impl Default for Indentation<'_> { + fn default() -> Self { + Self::TwoSpace + } +} + /// # Formats a json string /// /// The indentation can be set to any value using [`Indentation`] @@ -91,7 +97,7 @@ where indent_level = indent_level.saturating_sub(1); if !newline_requested { // see comment below about newline_requested - writer.write_all(&[b'\n'])?; + writer.write_all(b"\n")?; indent(&mut writer, indent_level, indentation)?; } } @@ -109,7 +115,7 @@ where // newline only happens after { [ and , // this means we can safely assume that it being followed up by } or ] // means an empty object/array - writer.write_all(&[b'\n'])?; + writer.write_all(b"\n")?; indent(&mut writer, old_level, indentation)?; } @@ -121,6 +127,9 @@ where } } + // trailing newline + writer.write_all(b"\n")?; + Ok(()) } @@ -130,7 +139,7 @@ where { for _ in 0..level { match indent_str { - Indentation::Default => { + Indentation::TwoSpace => { writer.write_all(b" ")?; } Indentation::Custom(indent) => { @@ -148,28 +157,28 @@ mod test { #[test] fn echoes_primitive() { - let json = "1.35"; - assert_eq!(json, format(json, Indentation::Default)); + let json = "1.35\n"; + assert_eq!(json, format(json, Indentation::TwoSpace)); } #[test] fn ignore_whitespace_in_string() { - let json = "\" hallo \""; - assert_eq!(json, format(json, Indentation::Default)); + let json = "\" hallo \"\n"; + assert_eq!(json, format(json, Indentation::TwoSpace)); } #[test] fn remove_leading_whitespace() { let json = " 0"; - let expected = "0"; - assert_eq!(expected, format(json, Indentation::Default)); + let expected = "0\n"; + assert_eq!(expected, format(json, Indentation::TwoSpace)); } #[test] fn handle_escaped_strings() { let json = " \" hallo \\\" \" "; - let expected = "\" hallo \\\" \""; - assert_eq!(expected, format(json, Indentation::Default)); + let expected = "\" hallo \\\" \"\n"; + assert_eq!(expected, format(json, Indentation::TwoSpace)); } #[test] @@ -177,8 +186,9 @@ mod test { let json = "{\"a\":0}"; let expected = "{ \"a\": 0 -}"; - assert_eq!(expected, format(json, Indentation::Default)); +} +"; + assert_eq!(expected, format(json, Indentation::TwoSpace)); } #[test] @@ -188,8 +198,9 @@ mod test { 1, 2, null -]"; - assert_eq!(expected, format(json, Indentation::Default)); +] +"; + assert_eq!(expected, format(json, Indentation::TwoSpace)); } #[test] @@ -203,9 +214,10 @@ mod test { { \"a\": null } -]"; +] +"; - assert_eq!(expected, format(json, Indentation::Default)); + assert_eq!(expected, format(json, Indentation::TwoSpace)); } #[test] @@ -218,8 +230,9 @@ mod test { { \"a\": null } -]"; +] +"; - assert_eq!(expected, format(expected, Indentation::Default)); + assert_eq!(expected, format(expected, Indentation::TwoSpace)); } }