1
0
mirror of https://github.com/jht5945/prettyjson.git synced 2025-12-28 01:40:05 +08:00

first viersion

This commit is contained in:
2019-07-21 12:22:45 +08:00
parent bfcd16c75f
commit 80ebf47b55
5 changed files with 428 additions and 4 deletions

83
src/main.rs Normal file
View File

@@ -0,0 +1,83 @@
extern crate argparse;
extern crate term;
extern crate json;
mod util;
use std::{
fs::File,
io::{
self,
prelude::*,
}
};
use argparse::{ArgumentParser, StoreTrue, Store};
use util::*;
const VERSION: &str = "0.1";
fn print_version() {
print!(r#"prettyjson {}
Copyright (C) 2019 Hatter Jiang.
License MIT <https://opensource.org/licenses/MIT>
Written by Hatter Jiang
"#, VERSION);
}
fn read_to_string(read: &mut Read) -> XResult<String> {
let mut buffer = String::new();
read.read_to_string(&mut buffer)?;
Ok(buffer)
}
fn main() {
let mut version = false;
let mut tab_width = 4u16;
let mut file = String::new();
{
let mut ap = ArgumentParser::new();
ap.set_description("prettyjson - command line JSON pretty tool.");
ap.refer(&mut tab_width).add_option(&["-w", "--tab-width"], Store, "Tab width, default 4");
ap.refer(&mut version).add_option(&["-v", "--version"], StoreTrue, "Print version");
ap.refer(&mut file).add_argument("FILE", Store, "FILE");
ap.parse_args_or_exit();
}
if version {
print_version();
return;
}
if tab_width > 100 {
print_message(MessageType::ERROR, &format!("Tab width is invalid: {}", tab_width));
return;
}
let read: XResult<String> = match file.len() {
0 => read_to_string(&mut io::stdin()),
_ => match File::open(&file) {
Err(err) => {
print_message(MessageType::ERROR, &format!("Open file: {}, failed: {}", &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) {
Err(err) => {
print_message(MessageType::ERROR, &format!("Parse JSON pailed: {}", err));
return;
},
Ok(json_object) => json_object,
},
};
println!("{}", json::stringify_pretty(json_object, tab_width));
}

24
src/util.rs Normal file
View File

@@ -0,0 +1,24 @@
pub type XResult<T> = Result<T, Box<dyn std::error::Error>>;
#[allow(dead_code)]
pub enum MessageType { INFO, OK, WARN, ERROR, }
pub fn print_message_ex(color: Option<term::color::Color>, h: &str, message: &str) {
let mut t = term::stdout().unwrap();
match color {
Some(c) => t.fg(c).unwrap(),
None => (),
}
write!(t, "{}", h).unwrap();
t.reset().unwrap();
println!(" {}", message);
}
pub fn print_message(mt: MessageType, message: &str) {
match mt {
MessageType::OK => print_message_ex(Some(term::color::GREEN), "[OK ]", message),
MessageType::WARN => print_message_ex(Some(term::color::YELLOW), "[WARN ]", message),
MessageType::ERROR => print_message_ex(Some(term::color::RED), "[ERROR]", message),
MessageType::INFO => print_message_ex(None, "[INFO]", message),
}
}