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
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
|
||||
///
|
||||
/// 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));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue