From 2ceb965de99c0c433ff99d45593788244fdca4c1 Mon Sep 17 00:00:00 2001 From: Garrett Berg Date: Fri, 9 Feb 2018 23:23:58 -0700 Subject: [PATCH] fix #43: pub use crates, add set_log_verbosity and feature=full-throttle --- Cargo.toml | 29 +++++++++++++++++++++++++---- docs/Readme.md | 4 ++-- src/easy_log.rs | 28 ++++++++++++++++++++++++++++ src/lib.rs | 32 +++++++++++++++++++++----------- src/main_macro.rs | 23 +++-------------------- 5 files changed, 79 insertions(+), 37 deletions(-) create mode 100644 src/easy_log.rs diff --git a/Cargo.toml b/Cargo.toml index 83eef3b..88aa018 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,11 +13,32 @@ keywords = ["cli", "prelude"] [dependencies] failure = "0.1.1" failure_derive = "0.1.1" -serde = "1.0.27" -serde_derive = "1.0.27" structopt = "0.1.6" structopt-derive = "0.1.6" log = "0.4.1" env_logger = "0.5.3" -glob = "0.2.11" -rayon = "0.9.0" + +[dependencies.serde] +version = "1.0.27" +optional = true + +[dependencies.serde_derive] +version = "1.0.27" +optional = true + +[dependencies.glob] +version = "0.2.11" +optional = true + +[dependencies.rayon] +version = "0.9.0" +optional = true + +[features] +default = ["full-throttle"] +full-throttle = [ + "serde", + "serde_derive", + "glob", + "rayon", +] diff --git a/docs/Readme.md b/docs/Readme.md index f15f25a..c234202 100644 --- a/docs/Readme.md +++ b/docs/Readme.md @@ -65,7 +65,7 @@ Let's import all the good stuff: #[macro_use] extern crate quicli; use quicli::prelude::*; ``` - + That's it. That's all the imports you should need for now! ## Write a CLI struct @@ -138,7 +138,7 @@ main!(|args: Cli, log_level: verbosity| { let content = read_file(&args.file)?; let content_lines = content.lines(); let first_n_lines = content_lines.take(args.count); - + info!("Reading first {} lines of {:?}", args.count, args.file); for line in first_n_lines { diff --git a/src/easy_log.rs b/src/easy_log.rs new file mode 100644 index 0000000..928413e --- /dev/null +++ b/src/easy_log.rs @@ -0,0 +1,28 @@ +use super::prelude; + +/// Set the logs verbosity based on an integer value: +/// +/// - `0`: error +/// - `1`: warn +/// - `2`: info +/// - `3`: debug +/// - `>=4`: trace +/// +/// This is used in the [`main!`] macro. You should typically use that instead. +/// +/// [`main!`]: macro.main.html +pub fn set_log_verbosity(verbosity: u64) -> prelude::Result<()> { + let log_level = match verbosity { + 0 => prelude::LogLevel::Error, + 1 => prelude::LogLevel::Warn, + 2 => prelude::LogLevel::Info, + 3 => prelude::LogLevel::Debug, + _ => prelude::LogLevel::Trace, + }.to_level_filter(); + + prelude::LoggerBuiler::new() + .filter(Some(env!("CARGO_PKG_NAME")), log_level) + .filter(None, prelude::LogLevel::Warn.to_level_filter()) + .try_init()?; + Ok(()) +} diff --git a/src/lib.rs b/src/lib.rs index f28ed9f..f0d0943 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,38 +6,47 @@ #![allow(unused_imports)] #![deny(missing_docs)] -#[macro_use] extern crate serde_derive; -extern crate serde; +#[cfg(feature="full-throttle")] +#[macro_use] +pub extern crate serde_derive; +#[cfg(feature="full-throttle")] +pub extern crate serde; -#[macro_use] extern crate structopt_derive; -extern crate structopt; +#[macro_use] +pub extern crate structopt_derive; +pub extern crate structopt; #[macro_use] extern crate failure_derive; #[macro_use] extern crate failure; -#[macro_use] extern crate log; -extern crate env_logger; +#[macro_use] pub extern crate log; +pub extern crate env_logger; -extern crate rayon; +#[cfg(feature="full-throttle")] +pub extern crate rayon; +#[cfg(feature="full-throttle")] pub mod fs; mod main_macro; +mod easy_log; +pub use easy_log::set_log_verbosity; mod reexports { + #[cfg(feature="full-throttle")] #[doc(hidden)] pub use serde_derive::*; #[doc(hidden)] pub use structopt_derive::*; - #[doc(hidden)] pub mod structopt { - pub use ::structopt::*; - } - #[doc(hidden)] pub use structopt::StructOpt; + #[doc(hidden)] pub use structopt::{self, StructOpt}; #[doc(hidden)] pub use failure_derive::*; #[doc(hidden)] pub use failure::*; #[doc(hidden)] pub use log::*; + #[cfg(feature="full-throttle")] pub use rayon::prelude::*; + + pub use super::set_log_verbosity; } /// Prelude – import all of this @@ -53,6 +62,7 @@ pub mod prelude { /// A handy alias for `Result` that carries a generic error type. pub type Result = ::std::result::Result; + #[cfg(feature="full-throttle")] pub use fs::*; #[doc(hidden)] pub use env_logger::Builder as LoggerBuiler; diff --git a/src/main_macro.rs b/src/main_macro.rs index 52aa17d..1a8a452 100644 --- a/src/main_macro.rs +++ b/src/main_macro.rs @@ -34,18 +34,7 @@ macro_rules! main { fn main() { fn run() -> $crate::prelude::Result<()> { let $args = <$cli>::from_args(); - let log_level = match $args.$verbosity { - 0 => $crate::prelude::LogLevel::Error, - 1 => $crate::prelude::LogLevel::Warn, - 2 => $crate::prelude::LogLevel::Info, - 3 => $crate::prelude::LogLevel::Debug, - _ => $crate::prelude::LogLevel::Trace, - }.to_level_filter(); - - $crate::prelude::LoggerBuiler::new() - .filter(Some(env!("CARGO_PKG_NAME")), log_level) - .filter(None, $crate::prelude::LogLevel::Warn.to_level_filter()) - .try_init()?; + $crate::prelude::set_log_verbosity($args.verbosity)?; $body @@ -66,10 +55,7 @@ macro_rules! main { fn main() { fn run() -> $crate::prelude::Result<()> { let $args = <$cli>::from_args(); - $crate::prelude::LoggerBuiler::new() - .filter(Some(env!("CARGO_PKG_NAME")), $crate::prelude::LogLevel::Error.to_level_filter()) - .filter(None, $crate::prelude::LogLevel::Warn.to_level_filter()) - .try_init()?; + $crate::prelude::set_log_verbosity(0)?; // ERROR level verbosity $body @@ -91,10 +77,7 @@ macro_rules! main { ($body:expr) => { fn main() { fn run() -> $crate::prelude::Result<()> { - $crate::prelude::LoggerBuiler::new() - .filter(Some(env!("CARGO_PKG_NAME")), $crate::prelude::LogLevel::Error.to_level_filter()) - .filter(None, $crate::prelude::LogLevel::Warn.to_level_filter()) - .try_init()?; + $crate::prelude::set_log_verbosity(0)?; // ERROR level verbosity $body Ok(())