feat: output to file

This commit is contained in:
2021-05-23 01:14:20 +08:00
parent 09825d6862
commit 8dc1156f93
2 changed files with 31 additions and 8 deletions

View File

@@ -8,6 +8,7 @@ pub enum PlistFormat {
pub struct ParsedArgs {
pub in_file: String,
pub format: PlistFormat,
pub out_file: Option<String>,
}
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()
}

View File

@@ -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);
}