re-export libs, replace failure with anyhow

master
Ondřej Hruška 5 years ago
parent d919300b02
commit 53329aa5e8
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 8
      Cargo.toml
  2. 16
      src/lib.rs

@ -1,6 +1,6 @@
[package]
name = "clappconfig"
version = "0.2.0"
version = "0.3.0"
authors = ["Ondřej Hruška <ondra@ondrovo.com>"]
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"

@ -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<Self::Init>;
fn configure<'a>(self, _clap: &clap::ArgMatches<'a>) -> anyhow::Result<Self::Init>;
/// Initialize the app
fn init(name: &str, cfg_file_name: &str, version: Option<&str>) -> Fallible<Self::Init> {
fn init(name: &str, cfg_file_name: &str, version: Option<&str>) -> anyhow::Result<Self::Init> {
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<P: AsRef<Path>>(path: P) -> Fallible<String> {
fn read_file<P: AsRef<Path>>(path: P) -> anyhow::Result<String> {
let path = path.as_ref();
let mut file = File::open(path)?;

Loading…
Cancel
Save