mirror of
https://github.com/Noratrieb/jsonformat.git
synced 2026-01-14 14:15:03 +01:00
Format code with rustfmt
This commit is contained in:
parent
f8c49ca661
commit
a78e6d3e4b
2 changed files with 22 additions and 20 deletions
25
src/lib.rs
25
src/lib.rs
|
|
@ -3,9 +3,9 @@
|
||||||
//!
|
//!
|
||||||
//! It does not do anything more than that, which makes it so fast.
|
//! It does not do anything more than that, which makes it so fast.
|
||||||
|
|
||||||
use std::io::{BufReader, BufWriter, Write};
|
|
||||||
use utf8_chars::BufReadCharsExt;
|
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
use std::io::{BufReader, BufWriter, Read, Write};
|
||||||
|
use utf8_chars::BufReadCharsExt;
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Set the indentation used for the formatting.
|
/// Set the indentation used for the formatting.
|
||||||
|
|
@ -42,11 +42,15 @@ pub fn format_json(json: &str, indentation: Indentation) -> String {
|
||||||
/// 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_buffered<R, W>(reader: &mut BufReader<R>, writer: &mut BufWriter<W>, indentation: Indentation) -> Result<(), Box<dyn Error>>
|
pub fn format_json_buffered<R, W>(
|
||||||
|
reader: &mut BufReader<R>,
|
||||||
|
writer: &mut BufWriter<W>,
|
||||||
|
indentation: Indentation,
|
||||||
|
) -> Result<(), Box<dyn Error>>
|
||||||
where
|
where
|
||||||
R: std::io::Read,
|
R: Read,
|
||||||
W: std::io::Write {
|
W: Write,
|
||||||
|
{
|
||||||
let mut escaped = false;
|
let mut escaped = false;
|
||||||
let mut in_string = false;
|
let mut in_string = false;
|
||||||
let mut indent_level = 0usize;
|
let mut indent_level = 0usize;
|
||||||
|
|
@ -124,9 +128,14 @@ where
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn indent_buffered<W>(writer: &mut BufWriter<W>, level: usize, indent_str: Indentation) -> Result<(), Box<dyn Error>>
|
fn indent_buffered<W>(
|
||||||
|
writer: &mut BufWriter<W>,
|
||||||
|
level: usize,
|
||||||
|
indent_str: Indentation,
|
||||||
|
) -> Result<(), Box<dyn Error>>
|
||||||
where
|
where
|
||||||
W: std::io::Write {
|
W: std::io::Write,
|
||||||
|
{
|
||||||
for _ in 0..level {
|
for _ in 0..level {
|
||||||
match indent_str {
|
match indent_str {
|
||||||
Indentation::Default => {
|
Indentation::Default => {
|
||||||
|
|
|
||||||
17
src/main.rs
17
src/main.rs
|
|
@ -1,5 +1,5 @@
|
||||||
use clap::clap_app;
|
use clap::clap_app;
|
||||||
use jsonformat::{Indentation, format_json_buffered};
|
use jsonformat::{format_json_buffered, Indentation};
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{BufReader, BufWriter, Read, Write};
|
use std::io::{BufReader, BufWriter, Read, Write};
|
||||||
|
|
@ -17,13 +17,8 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
let reader: Box<dyn Read> = match matches.value_of("input") {
|
let reader: Box<dyn Read> = match matches.value_of("input") {
|
||||||
Some(path) => {
|
Some(path) => Box::new(File::open(path)?),
|
||||||
let f = File::open(path)?;
|
None => Box::new(std::io::stdin()),
|
||||||
Box::new(BufReader::new(f))
|
|
||||||
},
|
|
||||||
None => {
|
|
||||||
Box::new(std::io::stdin())
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let replaced_indent = matches.value_of("indentation").map(|value| {
|
let replaced_indent = matches.value_of("indentation").map(|value| {
|
||||||
|
|
@ -55,10 +50,8 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
|
||||||
output = windows_output_default_file.as_deref().or(output);
|
output = windows_output_default_file.as_deref().or(output);
|
||||||
|
|
||||||
let writer : Box<dyn Write> = match output {
|
let writer: Box<dyn Write> = match output {
|
||||||
Some(file) => {
|
Some(file) => Box::new(File::create(file)?),
|
||||||
Box::new(File::create(file)?)
|
|
||||||
}
|
|
||||||
None => Box::new(std::io::stdout()),
|
None => Box::new(std::io::stdout()),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue