mirror of
https://github.com/Noratrieb/jsonformat.git
synced 2026-01-14 14:15:03 +01:00
change the api
This commit is contained in:
parent
376a9c53bc
commit
426fb16d7e
5 changed files with 43 additions and 20 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
|
@ -282,7 +282,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "jsonformat"
|
name = "jsonformat"
|
||||||
version = "0.1.0"
|
version = "1.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap 3.0.0-beta.2",
|
"clap 3.0.0-beta.2",
|
||||||
"criterion",
|
"criterion",
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "jsonformat"
|
name = "jsonformat"
|
||||||
version = "0.1.0"
|
version = "1.0.0"
|
||||||
authors = ["Nilstrieb <nilstrieb@gmail.com>"]
|
authors = ["Nilstrieb <nilstrieb@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
use criterion::{criterion_group, criterion_main, Criterion};
|
use criterion::{criterion_group, criterion_main, Criterion};
|
||||||
use jsonformat::format_json;
|
use jsonformat::{format_json, Indentation};
|
||||||
use std::{fs, io};
|
use std::{fs, io};
|
||||||
|
|
||||||
/// You need a json file called massive.json in your project root
|
/// You need a json file called massive.json in your project root
|
||||||
fn format_massive_json(file: &str) -> io::Result<String> {
|
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) {
|
fn criterion_benchmark(c: &mut Criterion) {
|
||||||
|
|
|
||||||
46
src/lib.rs
46
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
|
/// # 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 value is two spaces
|
||||||
/// The default indentation is faster than a custom one
|
/// 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
|
// 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 out = String::with_capacity(json.len());
|
||||||
|
|
||||||
let mut escaped = false;
|
let mut escaped = false;
|
||||||
|
|
@ -86,14 +104,14 @@ pub fn format_json(json: &str, indentation: Option<&str>) -> String {
|
||||||
out
|
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 {
|
for _ in 0..level {
|
||||||
match indent_str {
|
match indent_str {
|
||||||
None => {
|
Indentation::Default => {
|
||||||
buf.push(' ');
|
buf.push(' ');
|
||||||
buf.push(' ');
|
buf.push(' ');
|
||||||
}
|
}
|
||||||
Some(indent) => {
|
Indentation::Custom(indent) => {
|
||||||
buf.push_str(indent);
|
buf.push_str(indent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -107,27 +125,27 @@ mod test {
|
||||||
#[test]
|
#[test]
|
||||||
fn echoes_primitive() {
|
fn echoes_primitive() {
|
||||||
let json = "1.35";
|
let json = "1.35";
|
||||||
assert_eq!(json, format_json(json, None));
|
assert_eq!(json, format_json(json, Indentation::Default));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn ignore_whitespace_in_string() {
|
fn ignore_whitespace_in_string() {
|
||||||
let json = "\" hallo \"";
|
let json = "\" hallo \"";
|
||||||
assert_eq!(json, format_json(json, None));
|
assert_eq!(json, format_json(json, Indentation::Default));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn remove_leading_whitespace() {
|
fn remove_leading_whitespace() {
|
||||||
let json = " 0";
|
let json = " 0";
|
||||||
let expected = "0";
|
let expected = "0";
|
||||||
assert_eq!(expected, format_json(json, None));
|
assert_eq!(expected, format_json(json, Indentation::Default));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn handle_escaped_strings() {
|
fn handle_escaped_strings() {
|
||||||
let json = " \" hallo \\\" \" ";
|
let json = " \" hallo \\\" \" ";
|
||||||
let expected = "\" hallo \\\" \"";
|
let expected = "\" hallo \\\" \"";
|
||||||
assert_eq!(expected, format_json(json, None));
|
assert_eq!(expected, format_json(json, Indentation::Default));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -136,7 +154,7 @@ mod test {
|
||||||
let expected = "{
|
let expected = "{
|
||||||
\"a\": 0
|
\"a\": 0
|
||||||
}";
|
}";
|
||||||
assert_eq!(expected, format_json(json, None));
|
assert_eq!(expected, format_json(json, Indentation::Default));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -147,7 +165,7 @@ mod test {
|
||||||
2,
|
2,
|
||||||
null
|
null
|
||||||
]";
|
]";
|
||||||
assert_eq!(expected, format_json(json, None));
|
assert_eq!(expected, format_json(json, Indentation::Default));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -163,7 +181,7 @@ mod test {
|
||||||
}
|
}
|
||||||
]";
|
]";
|
||||||
|
|
||||||
assert_eq!(expected, format_json(json, None));
|
assert_eq!(expected, format_json(json, Indentation::Default));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -178,6 +196,6 @@ mod test {
|
||||||
}
|
}
|
||||||
]";
|
]";
|
||||||
|
|
||||||
assert_eq!(expected, format_json(expected, None));
|
assert_eq!(expected, format_json(expected, Indentation::Default));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use clap::clap_app;
|
use clap::clap_app;
|
||||||
use jsonformat::format_json;
|
use jsonformat::{format_json, Indentation};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
|
@ -36,7 +36,12 @@ fn main() -> Result<(), io::Error> {
|
||||||
.replace("t", "\t")
|
.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 output = matches.value_of("output");
|
||||||
let mut windows_output_default_file: Option<String> = None;
|
let mut windows_output_default_file: Option<String> = None;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue