feat: works

This commit is contained in:
2021-05-23 01:03:22 +08:00
parent 0a35401532
commit 09825d6862
3 changed files with 68 additions and 12 deletions

35
src/args.rs Normal file
View File

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

View File

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

11
src/utils.rs Normal file
View File

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