bin: switch from failure to anyhow for error handling

This commit is contained in:
Matt Bilker
2019-11-08 05:23:00 +00:00
parent 60a05c00b8
commit deb2314f14
2 changed files with 7 additions and 20 deletions

View File

@@ -16,11 +16,12 @@ quick-xml = "0.17.0"
rustc-hex = "2.0.1"
snafu = "0.6.0"
anyhow = { version = "1.0.19", optional = true }
clap = { version = "2.32.0", optional = true }
pretty_env_logger = { version = "0.3.0", optional = true }
[features]
build_binary = ["clap", "pretty_env_logger"]
build_binary = ["anyhow", "clap", "pretty_env_logger"]
[profile.release]
lto = true

View File

@@ -1,12 +1,10 @@
#[macro_use] extern crate failure;
use std::fs;
use std::io::{self, Error as IoError, Read, Write};
use anyhow::Context;
use byteorder::{BigEndian, ByteOrder};
use clap::{App, Arg};
use encoding_rs::Encoding;
use failure::Fallible;
use kbinxml::{EncodingType, Options, Printer};
fn display_buf(buf: &[u8]) -> Result<(), IoError> {
@@ -46,7 +44,9 @@ fn compare_slice(left: &[u8], right: &[u8]) {
}
}
fn run() -> Fallible<()> {
fn main() -> Result<(), anyhow::Error> {
pretty_env_logger::init();
let matches = App::new("kbinxml")
.about(env!("CARGO_PKG_DESCRIPTION"))
.version(env!("CARGO_PKG_VERSION"))
@@ -70,7 +70,7 @@ fn run() -> Fallible<()> {
let file_name = matches.value_of("input").unwrap();
let output_encoding = if let Some(label) = matches.value_of("encoding") {
let encoding = Encoding::for_label(label.as_bytes())
.ok_or_else(|| format_err!("No encoding found for label"))?;
.with_context(|| "No encoding found for label")?;
Some(EncodingType::from_encoding(encoding)?)
} else {
@@ -116,17 +116,3 @@ fn run() -> Fallible<()> {
Ok(())
}
fn main() {
pretty_env_logger::init();
if let Err(e) = run() {
eprintln!("Error: {}", e);
for cause in e.iter_causes() {
eprintln!("Cause: {}", cause);
}
eprintln!("{}", e.backtrace());
}
}