diff --git a/Cargo.lock b/Cargo.lock index 913f89a..6fb7915 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -282,7 +282,7 @@ dependencies = [ [[package]] name = "jsonformat" -version = "0.1.0" +version = "1.0.0" dependencies = [ "clap 3.0.0-beta.2", "criterion", diff --git a/Cargo.toml b/Cargo.toml index dd5b306..fbc3d87 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "jsonformat" -version = "0.1.0" +version = "1.0.0" authors = ["Nilstrieb "] edition = "2018" license = "MIT" diff --git a/benches/bench.rs b/benches/bench.rs index 5bd3956..002aa06 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -1,10 +1,10 @@ use criterion::{criterion_group, criterion_main, Criterion}; -use jsonformat::format_json; +use jsonformat::{format_json, Indentation}; use std::{fs, io}; /// You need a json file called massive.json in your project root fn format_massive_json(file: &str) -> io::Result { - Ok(format_json(&file, None)) + Ok(format_json(&file, Indentation::Default)) } fn criterion_benchmark(c: &mut Criterion) { diff --git a/src/lib.rs b/src/lib.rs index 22e4aab..13f0612 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,13 +1,31 @@ +//! +//! jsonformat is a library for formatting json. +//! +//! It does not do anything more than that, which makes it so fast. + +/// +/// Set the indentation used for the formatting. +/// +/// Note: It is *not* recommended to set indentation to anything oder than some spaces or some tabs, +/// but nothing is stopping you from doing that. +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +pub enum Indentation<'a> { + /// Use the default indentation, which is two spaces + Default, + /// Use a custom indentation String + Custom(&'a str), +} + /// /// # Formats a json string /// -/// The indentation can be set to any custom value +/// The indentation can be set to any value using [Indentation](jsonformat::Indentation) /// The default value is two spaces /// The default indentation is faster than a custom one /// -pub fn format_json(json: &str, indentation: Option<&str>) -> String { +pub fn format_json(json: &str, indentation: Indentation) -> String { // at least as big as the input to avoid resizing - // this might be too big if the input string is formatted in a weird way, but that's not expected + // this might be too big if the input string is formatted in a weird way, but that's not expected, and it will still be efficient let mut out = String::with_capacity(json.len()); let mut escaped = false; @@ -86,14 +104,14 @@ pub fn format_json(json: &str, indentation: Option<&str>) -> String { out } -fn indent(buf: &mut String, level: usize, indent_str: Option<&str>) { +fn indent(buf: &mut String, level: usize, indent_str: Indentation) { for _ in 0..level { match indent_str { - None => { + Indentation::Default => { buf.push(' '); buf.push(' '); } - Some(indent) => { + Indentation::Custom(indent) => { buf.push_str(indent); } } @@ -107,27 +125,27 @@ mod test { #[test] fn echoes_primitive() { let json = "1.35"; - assert_eq!(json, format_json(json, None)); + assert_eq!(json, format_json(json, Indentation::Default)); } #[test] fn ignore_whitespace_in_string() { let json = "\" hallo \""; - assert_eq!(json, format_json(json, None)); + assert_eq!(json, format_json(json, Indentation::Default)); } #[test] fn remove_leading_whitespace() { let json = " 0"; let expected = "0"; - assert_eq!(expected, format_json(json, None)); + assert_eq!(expected, format_json(json, Indentation::Default)); } #[test] fn handle_escaped_strings() { let json = " \" hallo \\\" \" "; let expected = "\" hallo \\\" \""; - assert_eq!(expected, format_json(json, None)); + assert_eq!(expected, format_json(json, Indentation::Default)); } #[test] @@ -136,7 +154,7 @@ mod test { let expected = "{ \"a\": 0 }"; - assert_eq!(expected, format_json(json, None)); + assert_eq!(expected, format_json(json, Indentation::Default)); } #[test] @@ -147,7 +165,7 @@ mod test { 2, null ]"; - assert_eq!(expected, format_json(json, None)); + assert_eq!(expected, format_json(json, Indentation::Default)); } #[test] @@ -163,7 +181,7 @@ mod test { } ]"; - assert_eq!(expected, format_json(json, None)); + assert_eq!(expected, format_json(json, Indentation::Default)); } #[test] @@ -178,6 +196,6 @@ mod test { } ]"; - assert_eq!(expected, format_json(expected, None)); + assert_eq!(expected, format_json(expected, Indentation::Default)); } } diff --git a/src/main.rs b/src/main.rs index 501a4c7..238950f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use clap::clap_app; -use jsonformat::format_json; +use jsonformat::{format_json, Indentation}; use std::fs; use std::io; use std::io::Read; @@ -36,7 +36,12 @@ fn main() -> Result<(), io::Error> { .replace("t", "\t") }); - let formatted = format_json(&str, replaced_indent.as_deref()); + let indent = match replaced_indent { + Some(ref str) => Indentation::Custom(str), + None => Indentation::Default, + }; + + let formatted = format_json(&str, indent); let mut output = matches.value_of("output"); let mut windows_output_default_file: Option = None;