Skip to content

Commit

Permalink
feat: split lib into cargo features
Browse files Browse the repository at this point in the history
  • Loading branch information
sonro committed Oct 19, 2022
1 parent d20365a commit bdba927
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- Features for conditional compilation of separate utilities. See #12.
- All features enabled by default [report, error, cli-error]. See #12.

## [0.3.0] - 2022-10-19

### Added
Expand Down
29 changes: 25 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,29 @@ readme = "README.md"
[package.metadata]
msrv = "1.61.0"

[features]
default = ["cli-error", "error", "report"]
cli-error = []
error = ["dep:anyhow"]
report = ["dep:anyhow", "dep:atty", "dep:colored"]

[dependencies]
anyhow = "1.0.63"
atty = "0.2.14"
colored = "2.0.0"
exitcode = "1.1.2"
anyhow = { version = "1.0.63", optional = true }
atty = { version = "0.2.14", optional = true }
colored = { version = "2.0.0", optional = true }
exitcode = { version = "1.1.2" }

[[bin]]
name = "report_err_full_test"
test = false
required-features = ["error", "report"]

[[bin]]
name = "report_err_test"
test = false
required-features = ["error", "report"]

[[bin]]
name = "status_test"
test = false
required-features = ["report"]
18 changes: 17 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
use std::{fmt::Display, path::PathBuf};
#[cfg(feature = "error")]
use std::fmt::Display;
#[cfg(feature = "cli-error")]
use std::path::PathBuf;

#[cfg(feature = "error")]
use error::HelpMsg;

#[cfg(feature = "cli-error")]
mod cli_error;
#[cfg(feature = "error")]
mod error;
#[cfg(feature = "error")]
mod macros;

#[cfg(feature = "report")]
pub mod report;

#[cfg(any(feature = "report", feature = "error"))]
pub use anyhow;
#[cfg(feature = "report")]
pub use colored;

#[cfg(feature = "report")]
pub use colored::Color;

/// Wrapper around a dynamic error type with an optional help message.
Expand All @@ -24,6 +35,7 @@ pub use colored::Color;
/// - `Error` may contain a help message in order to suggest further actions a
/// user might take.
#[derive(Debug)]
#[cfg(feature = "error")]
pub struct Error {
inner: anyhow::Error,
help: Option<HelpMsg>,
Expand All @@ -50,6 +62,7 @@ pub struct Error {
/// ```
#[derive(Clone, Default)]
#[repr(transparent)]
#[cfg(feature = "error")]
pub struct Chain<'a> {
inner: anyhow::Chain<'a>,
}
Expand Down Expand Up @@ -108,6 +121,7 @@ pub struct Chain<'a> {
/// }
///
/// ```
#[cfg(feature = "error")]
pub type Result<T, E = Error> = core::result::Result<T, E>;

/// Provides `wrap`, `wrap_help` and `wrap_help_owned` methods for `Result`.
Expand Down Expand Up @@ -145,6 +159,7 @@ pub type Result<T, E = Error> = core::result::Result<T, E>;
/// Ok(content)
/// }
/// ```
#[cfg(feature = "error")]
pub trait ErrorWrap<T, E>: error::wrap::private::Sealed
where
E: Send + Sync + 'static,
Expand Down Expand Up @@ -185,6 +200,7 @@ pub trait ExitCode {

/// Standard command line application error
#[derive(Debug, PartialEq, Eq, Hash)]
#[cfg(feature = "cli-error")]
pub enum CliError {
/// Invalid configuration
Config,
Expand Down
2 changes: 2 additions & 0 deletions src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ where
/// //
/// // try something else
/// ```
#[cfg(feature = "error")]
pub fn err(err: &Error) {
let color = atty::is(atty::Stream::Stderr);
let mut f = stderr().lock();
Expand Down Expand Up @@ -148,6 +149,7 @@ pub fn err(err: &Error) {
/// }
/// }
/// ```
#[cfg(feature = "error")]
pub fn err_full(err: &Error) {
let color = atty::is(atty::Stream::Stderr);
let mut f = stderr().lock();
Expand Down

0 comments on commit bdba927

Please sign in to comment.