mirror of
https://github.com/jht5945/prettyjson.git
synced 2025-12-29 02:10:03 +08:00
style: code style
This commit is contained in:
44
src/main.rs
44
src/main.rs
@@ -1,19 +1,13 @@
|
||||
extern crate argparse;
|
||||
extern crate term;
|
||||
extern crate json;
|
||||
#[macro_use]
|
||||
extern crate rust_util;
|
||||
|
||||
mod opt;
|
||||
|
||||
use std::{
|
||||
fs::{self, File},
|
||||
io::{self}
|
||||
};
|
||||
use rust_util::{
|
||||
XResult,
|
||||
util_msg::*,
|
||||
util_io::*,
|
||||
};
|
||||
use std::{ io, fs, fs::File };
|
||||
use rust_util::{ XResult, util_io::* };
|
||||
use opt::*;
|
||||
|
||||
const NAME: &str = env!("CARGO_PKG_NAME");
|
||||
@@ -22,7 +16,7 @@ const GIT_HASH: &str = env!("GIT_HASH");
|
||||
|
||||
fn print_version() {
|
||||
print!(r#"{} {} - {}
|
||||
Copyright (C) 2019 Hatter Jiang.
|
||||
Copyright (C) 2019-2020 Hatter Jiang.
|
||||
License MIT <https://opensource.org/licenses/MIT>
|
||||
|
||||
Written by Hatter Jiang
|
||||
@@ -34,7 +28,7 @@ fn main() {
|
||||
let options = Options::parse_args_static();
|
||||
|
||||
if options.verbose {
|
||||
print_message(MessageType::DEBUG, &format!("{} version: {}, git hash: {}", NAME, VERSION, GIT_HASH));
|
||||
debugging!("{} version: {}, git hash: {}", NAME, VERSION, GIT_HASH);
|
||||
}
|
||||
|
||||
if options.version {
|
||||
@@ -43,46 +37,42 @@ fn main() {
|
||||
}
|
||||
|
||||
if options.tab_width > 100 {
|
||||
print_message(MessageType::ERROR, &format!("Tab width is invalid: {}", options.tab_width));
|
||||
failure!("Tab width is invalid: {}", options.tab_width);
|
||||
return;
|
||||
}
|
||||
|
||||
let read: XResult<String> = match options.file.len() {
|
||||
0 => read_to_string(&mut io::stdin()),
|
||||
_ => match File::open(&options.file) {
|
||||
Ok(mut f) => read_to_string(&mut f),
|
||||
Err(err) => {
|
||||
print_message(MessageType::ERROR, &format!("Open file: {}, failed: {}", &options.file, err));
|
||||
failure!("Open file: {}, failed: {}", &options.file, err);
|
||||
return;
|
||||
},
|
||||
Ok(mut f) => read_to_string(&mut f),
|
||||
},
|
||||
};
|
||||
|
||||
let json_object = match read {
|
||||
Err(err) => {
|
||||
print_message(MessageType::ERROR, &format!("Read JSON failed: {}", err));
|
||||
return;
|
||||
},
|
||||
Ok(content) => match json::parse(&content) {
|
||||
Ok(json_object) => json_object,
|
||||
Err(err) => {
|
||||
print_message(MessageType::ERROR, &format!("Parse JSON failed: {}", err));
|
||||
failure!("Parse JSON failed: {}", err);
|
||||
return;
|
||||
},
|
||||
Ok(json_object) => json_object,
|
||||
},
|
||||
Err(err) => {
|
||||
failure!("Read JSON failed: {}", err);
|
||||
return;
|
||||
},
|
||||
};
|
||||
|
||||
let pretty_json = json::stringify_pretty(json_object, options.tab_width);
|
||||
println!("{}", pretty_json);
|
||||
|
||||
if options.file.len() > 0 && options.replace_file {
|
||||
if !options.file.is_empty() && options.replace_file {
|
||||
match fs::write(&options.file, pretty_json) {
|
||||
Err(err) => {
|
||||
print_message(MessageType::ERROR, &format!("Write JSON file failed: {}", err));
|
||||
},
|
||||
Ok(_) => {
|
||||
print_message(MessageType::OK, "Write JSON file success.");
|
||||
},
|
||||
Err(err) => failure!("Write JSON file failed: {}", err),
|
||||
Ok(_) => success!("Write JSON file success."),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user