diff options
| author | nsfisis <nsfisis@gmail.com> | 2024-07-15 13:58:32 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2024-07-15 13:58:32 +0900 |
| commit | a245b635c9099448a00eea15cec5bc61dcf1d026 (patch) | |
| tree | 7bf7476fa386f3746f9ad9471f2fa00bb45fa41d /src/main.rs | |
| parent | 8eae1719cd68929580ea2c8e795d238a1a03d81d (diff) | |
| download | reparojson-a245b635c9099448a00eea15cec5bc61dcf1d026.tar.gz reparojson-a245b635c9099448a00eea15cec5bc61dcf1d026.tar.zst reparojson-a245b635c9099448a00eea15cec5bc61dcf1d026.zip | |
initial implementation
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 77 |
1 files changed, 75 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs index 0672e51..c113039 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,76 @@ -fn main() { - println!("Hello, World!"); +use reparojson::{self, RepairErr, RepairOk, RepairResult}; +use std::ffi::{OsStr, OsString}; +use std::fs::File; +use std::io::{stdin, stdout, BufReader, BufWriter, Write}; +use std::process::ExitCode; + +struct Config { + quiet: bool, + file_path: Option<OsString>, +} + +fn parse_args() -> std::io::Result<Config> { + use clap::{arg, command, value_parser}; + + let matches = command!() + .arg(arg!(-q --quiet "Successfully exit if the input JSON is repaired")) + .arg( + arg!([FILE] "The input JSON file (default: STDIN)") + .value_parser(value_parser!(OsString)), + ) + .get_matches(); + + let quiet = matches.get_flag("quiet"); + let file_path = matches.get_one("FILE").cloned(); + Ok(Config { quiet, file_path }) +} + +fn repair(input_file_path: Option<OsString>, mut w: impl Write) -> RepairResult { + match input_file_path.as_ref() { + None => { + let reader = stdin().lock(); + let reader = BufReader::new(reader); + reparojson::repair(reader, &mut w) + } + Some(file_path) => { + if file_path == OsStr::new("-") { + let reader = stdin().lock(); + let reader = BufReader::new(reader); + reparojson::repair(reader, &mut w) + } else { + let reader = File::open(file_path)?; + let reader = BufReader::new(reader); + reparojson::repair(reader, &mut w) + } + } + } +} + +fn main() -> std::io::Result<ExitCode> { + let config = parse_args()?; + + let writer = stdout().lock(); + let mut writer = BufWriter::new(writer); + + let exit_code = match repair(config.file_path, &mut writer) { + Ok(RepairOk::Valid) => ExitCode::SUCCESS, + Ok(RepairOk::Repaired) => { + if config.quiet { + ExitCode::SUCCESS + } else { + ExitCode::from(1) + } + } + Err(RepairErr::Invalid(err)) => { + eprintln!("{}", err); + ExitCode::from(2) + } + Err(RepairErr::IoErr(err)) => { + eprintln!("{}", err); + ExitCode::from(3) + } + }; + + writer.flush()?; + Ok(exit_code) } |
