From 2691f380aac744def287a68f55e59abc5e9bee55 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 2 Aug 2020 22:25:31 +0800 Subject: [PATCH] style: code style --- Cargo.lock | 6 +++--- Cargo.toml | 2 +- src/main.rs | 44 +++++++++++++++++--------------------------- 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6142050..4fbf94c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -160,7 +160,7 @@ version = "0.1.2" dependencies = [ "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)", - "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)", ] @@ -242,7 +242,7 @@ dependencies = [ [[package]] name = "rust_util" -version = "0.1.0" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "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_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_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 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" diff --git a/Cargo.toml b/Cargo.toml index 49976ee..550ec33 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,4 @@ license = "MIT" argparse = "0.2.2" json = "0.11.14" term = "0.5.2" -rust_util = "0.1.0" +rust_util = "0.6.3" diff --git a/src/main.rs b/src/main.rs index b69d011..1d1632b 100644 --- a/src/main.rs +++ b/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 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 = 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."), } } }