1
0
mirror of https://github.com/jht5945/prettyjson.git synced 2025-12-27 09:10:04 +08:00

style: code style

This commit is contained in:
2020-08-02 22:25:31 +08:00
parent f031b9ce0c
commit 2691f380aa
3 changed files with 21 additions and 31 deletions

6
Cargo.lock generated
View File

@@ -160,7 +160,7 @@ version = "0.1.2"
dependencies = [ dependencies = [
"argparse 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "argparse 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"json 0.11.15 (registry+https://github.com/rust-lang/crates.io-index)", "json 0.11.15 (registry+https://github.com/rust-lang/crates.io-index)",
"rust_util 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "rust_util 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
"term 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", "term 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@@ -242,7 +242,7 @@ dependencies = [
[[package]] [[package]]
name = "rust_util" name = "rust_util"
version = "0.1.0" version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -363,7 +363,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)" = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84" "checksum redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)" = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84"
"checksum redox_users 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4ecedbca3bf205f8d8f5c2b44d83cd0690e39ee84b951ed649e9f1841132b66d" "checksum redox_users 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4ecedbca3bf205f8d8f5c2b44d83cd0690e39ee84b951ed649e9f1841132b66d"
"checksum rust-argon2 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4ca4eaef519b494d1f2848fc602d18816fed808a981aedf4f1f00ceb7c9d32cf" "checksum rust-argon2 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4ca4eaef519b494d1f2848fc602d18816fed808a981aedf4f1f00ceb7c9d32cf"
"checksum rust_util 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5dc9b9d45cf4e9a8a28c1c622f0a2bba9af88b85ec04c494d80605a717c1c022" "checksum rust_util 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "9e9cf201657d8553fd7eddf4c20e00b1bdebca40e9fa2ede5c87f6874d02750f"
"checksum rustc-demangle 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783" "checksum rustc-demangle 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783"
"checksum syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)" = "dff0acdb207ae2fe6d5976617f887eb1e35a2ba52c13c7234c790960cdad9238" "checksum syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)" = "dff0acdb207ae2fe6d5976617f887eb1e35a2ba52c13c7234c790960cdad9238"
"checksum synstructure 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "67656ea1dc1b41b1451851562ea232ec2e5a80242139f7e679ceccfb5d61f545" "checksum synstructure 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "67656ea1dc1b41b1451851562ea232ec2e5a80242139f7e679ceccfb5d61f545"

View File

@@ -12,4 +12,4 @@ license = "MIT"
argparse = "0.2.2" argparse = "0.2.2"
json = "0.11.14" json = "0.11.14"
term = "0.5.2" term = "0.5.2"
rust_util = "0.1.0" rust_util = "0.6.3"

View File

@@ -1,19 +1,13 @@
extern crate argparse; extern crate argparse;
extern crate term; extern crate term;
extern crate json; extern crate json;
#[macro_use]
extern crate rust_util; extern crate rust_util;
mod opt; mod opt;
use std::{ use std::{ io, fs, fs::File };
fs::{self, File}, use rust_util::{ XResult, util_io::* };
io::{self}
};
use rust_util::{
XResult,
util_msg::*,
util_io::*,
};
use opt::*; use opt::*;
const NAME: &str = env!("CARGO_PKG_NAME"); const NAME: &str = env!("CARGO_PKG_NAME");
@@ -22,7 +16,7 @@ const GIT_HASH: &str = env!("GIT_HASH");
fn print_version() { fn print_version() {
print!(r#"{} {} - {} print!(r#"{} {} - {}
Copyright (C) 2019 Hatter Jiang. Copyright (C) 2019-2020 Hatter Jiang.
License MIT <https://opensource.org/licenses/MIT> License MIT <https://opensource.org/licenses/MIT>
Written by Hatter Jiang Written by Hatter Jiang
@@ -34,7 +28,7 @@ fn main() {
let options = Options::parse_args_static(); let options = Options::parse_args_static();
if options.verbose { 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 { if options.version {
@@ -43,46 +37,42 @@ fn main() {
} }
if options.tab_width > 100 { 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; return;
} }
let read: XResult<String> = match options.file.len() { let read: XResult<String> = match options.file.len() {
0 => read_to_string(&mut io::stdin()), 0 => read_to_string(&mut io::stdin()),
_ => match File::open(&options.file) { _ => match File::open(&options.file) {
Ok(mut f) => read_to_string(&mut f),
Err(err) => { Err(err) => {
print_message(MessageType::ERROR, &format!("Open file: {}, failed: {}", &options.file, err)); failure!("Open file: {}, failed: {}", &options.file, err);
return; return;
}, },
Ok(mut f) => read_to_string(&mut f),
}, },
}; };
let json_object = match read { let json_object = match read {
Err(err) => {
print_message(MessageType::ERROR, &format!("Read JSON failed: {}", err));
return;
},
Ok(content) => match json::parse(&content) { Ok(content) => match json::parse(&content) {
Ok(json_object) => json_object,
Err(err) => { Err(err) => {
print_message(MessageType::ERROR, &format!("Parse JSON failed: {}", err)); failure!("Parse JSON failed: {}", err);
return; 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); let pretty_json = json::stringify_pretty(json_object, options.tab_width);
println!("{}", pretty_json); 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) { match fs::write(&options.file, pretty_json) {
Err(err) => { Err(err) => failure!("Write JSON file failed: {}", err),
print_message(MessageType::ERROR, &format!("Write JSON file failed: {}", err)); Ok(_) => success!("Write JSON file success."),
},
Ok(_) => {
print_message(MessageType::OK, "Write JSON file success.");
},
} }
} }
} }