diff --git a/Cargo.toml b/Cargo.toml index 267a679..3c7d387 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "clappconfig" -version = "0.2.0" +version = "0.3.0" authors = ["Ondřej Hruška "] edition = "2018" license = "MIT" @@ -15,12 +15,8 @@ categories = [ [dependencies] log = "0.4" env_logger = "0.7" -failure = "0.1" +anyhow = "1.0.28" serde = "1.0" serde_json = "1.0" clap = "2.33" json5 = "0.2" - -[dev-dependencies] -smart-default = "0.6" -serde_derive = "1.0" diff --git a/src/lib.rs b/src/lib.rs index 5cf69de..008ceb6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,6 @@ #[macro_use] -extern crate log; +pub extern crate log; -use failure::{bail, Fallible}; use serde::de::DeserializeOwned; use serde::Serialize; use std::collections::HashMap; @@ -11,6 +10,9 @@ use std::io::Read; use std::path::{Path, PathBuf}; use std::str::FromStr; +// re-export libs appearing in public API +pub use clap; + pub const LOG_LEVELS: [&str; 6] = ["off", "error", "warn", "info", "debug", "trace"]; /// Implement this for the main config struct @@ -56,10 +58,10 @@ pub trait AppConfig: Sized + Serialize + DeserializeOwned + Debug + Default { /// Configure the config object using args. /// Logging has already been inited and configured. - fn configure<'a>(self, _clap: &clap::ArgMatches<'a>) -> Fallible; + fn configure<'a>(self, _clap: &clap::ArgMatches<'a>) -> anyhow::Result; /// Initialize the app - fn init(name: &str, cfg_file_name: &str, version: Option<&str>) -> Fallible { + fn init(name: &str, cfg_file_name: &str, version: Option<&str>) -> anyhow::Result { let version = version.unwrap_or_else(|| env!("CARGO_PKG_VERSION")); let clap = clap::App::new(name) .arg( @@ -160,7 +162,7 @@ pub trait AppConfig: Sized + Serialize + DeserializeOwned + Debug + Default { let mut level = config.logging(); if !LOG_LEVELS.contains(&level) { - bail!("Invalid default log level: {}", level); + anyhow::bail!("Invalid default log level: {}", level); } /* env RUST_LOG overrides default if set, but can be changed by CLI args */ @@ -169,7 +171,7 @@ pub trait AppConfig: Sized + Serialize + DeserializeOwned + Debug + Default { level = env_level; if !LOG_LEVELS.contains(&level) { - bail!("Invalid env log level: {}", level); + anyhow::bail!("Invalid env log level: {}", level); } } @@ -239,7 +241,7 @@ pub trait AppConfig: Sized + Serialize + DeserializeOwned + Debug + Default { } } -fn read_file>(path: P) -> Fallible { +fn read_file>(path: P) -> anyhow::Result { let path = path.as_ref(); let mut file = File::open(path)?;