change the api

This commit is contained in:
nora 2021-08-18 19:16:16 +02:00
parent 376a9c53bc
commit 426fb16d7e
5 changed files with 43 additions and 20 deletions

2
Cargo.lock generated
View file

@ -282,7 +282,7 @@ dependencies = [
[[package]]
name = "jsonformat"
version = "0.1.0"
version = "1.0.0"
dependencies = [
"clap 3.0.0-beta.2",
"criterion",

View file

@ -1,6 +1,6 @@
[package]
name = "jsonformat"
version = "0.1.0"
version = "1.0.0"
authors = ["Nilstrieb <nilstrieb@gmail.com>"]
edition = "2018"
license = "MIT"

View file

@ -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<String> {
Ok(format_json(&file, None))
Ok(format_json(&file, Indentation::Default))
}
fn criterion_benchmark(c: &mut Criterion) {

View file

@ -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));
}
}

View file

@ -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<String> = None;