From 8dc1156f9379691297c149fc37cbf7e67aa0b245 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 23 May 2021 01:14:20 +0800 Subject: [PATCH] feat: output to file --- src/args.rs | 6 +++++- src/main.rs | 33 ++++++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/args.rs b/src/args.rs index 76f6afb..236464a 100644 --- a/src/args.rs +++ b/src/args.rs @@ -8,6 +8,7 @@ pub enum PlistFormat { pub struct ParsedArgs { pub in_file: String, pub format: PlistFormat, + pub out_file: Option, } pub fn parse_args(matches: ArgMatches<'static>) -> ParsedArgs { @@ -18,6 +19,7 @@ pub fn parse_args(matches: ArgMatches<'static>) -> ParsedArgs { Some("bin") | Some("binary") => PlistFormat::Binary, _ => failure_and_exit!("Plist format error."), }, + out_file: matches.value_of("out").map(ToString::to_string), } } @@ -30,6 +32,8 @@ pub fn get_args_matches() -> ArgMatches<'static> { .default_value("xml") .possible_values(&["xml", "binary", "bin"]) .help("Output plist format") - ).arg(Arg::with_name("FILE").required(true).index(1).help("plist file name")) + ) + .arg(Arg::with_name("out").short("o").long("out").takes_value(true).help("Output plist file")) + .arg(Arg::with_name("FILE").required(true).index(1).help("Input plist file name")) .get_matches() } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index ceabcc7..520ceb0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ mod args; use plist::Value; use crate::utils::print_bytes; use crate::args::PlistFormat; +use std::fs::File; fn main() { let matches = args::get_args_matches(); @@ -15,14 +16,32 @@ fn main() { let plist_value = Value::from_file(&parsed_args.in_file).unwrap_or_else(|e| { failure_and_exit!("Read plist file: {}, failed: {}", parsed_args.in_file, e); }); - let mut buf = vec![]; - match parsed_args.format { - PlistFormat::Xml => if let Err(e) = plist_value.to_writer_xml(&mut buf) { - failure_and_exit!("Write plist failed: {}", e); + if let Some(out_file) = parsed_args.out_file { + if File::open(&out_file).is_ok() { + failure_and_exit!("Output file exits: {}", out_file); } - PlistFormat::Binary => if let Err(e) = plist_value.to_writer_binary(&mut buf) { - failure_and_exit!("Write plist failed: {}", e); + match parsed_args.format { + PlistFormat::Xml => if let Err(e) = plist_value.to_file_xml(&out_file) { + failure_and_exit!("Write xml plist file failed: {}", e); + } else { + success!("Write xml plist file: {} success", out_file); + } + PlistFormat::Binary => if let Err(e) = plist_value.to_file_binary(&out_file) { + failure_and_exit!("Write binary plist file failed: {}", e); + } else { + success!("Write binary plist file: {} success", out_file); + } } + } else { + let mut buf = vec![]; + match parsed_args.format { + PlistFormat::Xml => if let Err(e) = plist_value.to_writer_xml(&mut buf) { + failure_and_exit!("Write xml plist failed: {}", e); + } + PlistFormat::Binary => if let Err(e) = plist_value.to_writer_binary(&mut buf) { + failure_and_exit!("Write binary plist failed: {}", e); + } + } + print_bytes(&buf); } - print_bytes(&buf); }