From 09825d6862f60bccb23d602bd28d43291752a827 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 23 May 2021 01:03:22 +0800 Subject: [PATCH] feat: works --- src/args.rs | 35 +++++++++++++++++++++++++++++++++++ src/main.rs | 34 ++++++++++++++++++++++------------ src/utils.rs | 11 +++++++++++ 3 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 src/args.rs create mode 100644 src/utils.rs diff --git a/src/args.rs b/src/args.rs new file mode 100644 index 0000000..76f6afb --- /dev/null +++ b/src/args.rs @@ -0,0 +1,35 @@ +use clap::{ArgMatches, App, Arg}; + +pub enum PlistFormat { + Xml, + Binary, +} + +pub struct ParsedArgs { + pub in_file: String, + pub format: PlistFormat, +} + +pub fn parse_args(matches: ArgMatches<'static>) -> ParsedArgs { + ParsedArgs { + in_file: matches.value_of("FILE").unwrap().to_string(), + format: match matches.value_of("format") { + Some("xml") => PlistFormat::Xml, + Some("bin") | Some("binary") => PlistFormat::Binary, + _ => failure_and_exit!("Plist format error."), + }, + } +} + +pub fn get_args_matches() -> ArgMatches<'static> { + App::new(env!("CARGO_PKG_NAME")) + .version(env!("CARGO_PKG_VERSION")) + .author(env!("CARGO_PKG_AUTHORS")) + .about(env!("CARGO_PKG_DESCRIPTION")) + .arg(Arg::with_name("format").short("f").long("format").takes_value(true) + .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")) + .get_matches() +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 00c016f..ceabcc7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,28 @@ -#[macro_use] extern crate rust_util; +#[macro_use] +extern crate rust_util; -use clap::{App, Arg}; +mod utils; +mod args; + +use plist::Value; +use crate::utils::print_bytes; +use crate::args::PlistFormat; fn main() { - let matches = App::new(env!("CARGO_PKG_NAME")) - .version(env!("CARGO_PKG_VERSION")) - .author(env!("CARGO_PKG_AUTHORS")) - .about(env!("CARGO_PKG_DESCRIPTION")) - .arg(Arg::with_name("FILE").required(true).index(1).help("plist file name")) - .get_matches(); + let matches = args::get_args_matches(); + let parsed_args = args::parse_args(matches); - let file = matches.value_of("FILE").unwrap(); - let plist_value = plist::from_file(file).unwrap_or_else(|e| { - failure_and_exit!("Read plist file: {}, failed: {}", file, e); + 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); }); - // println!(plist::to_writer_xml()) + 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); + } + PlistFormat::Binary => if let Err(e) = plist_value.to_writer_binary(&mut buf) { + failure_and_exit!("Write plist failed: {}", e); + } + } + print_bytes(&buf); } diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..edf75ae --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,11 @@ +use std::io::Write; + +pub fn print_bytes(buf: &[u8]) { + let mut out = std::io::stdout(); + if let Err(e) = out.write_all(buf) { + failure_and_exit!("Print bytes to stdout failed: {}", e); + } + if let Err(e) = out.flush() { + failure_and_exit!("Print bytes to stdout failed: {}", e); + } +} \ No newline at end of file