From d0bb96f2dade564d66001c6e1579ac6d65de5912 Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Sun, 31 Dec 2023 02:55:52 +0100 Subject: [PATCH] Make backtrace and color-eyre optional --- core/Cargo.toml | 6 ++++-- core/src/error/context.rs | 6 ++++++ core/src/terminal/component.rs | 1 + 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/core/Cargo.toml b/core/Cargo.toml index 7be86885..e9e1ca0a 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -18,13 +18,13 @@ rust-version = "1.60" [dependencies] abscissa_derive = { version = "0.7", path = "../derive" } -backtrace = "0.3" canonical-path = "2" fs-err = "2" once_cell = "1.17" # optional dependencies arc-swap = { version = "1", optional = true } +backtrace = { version = "0.3", optional = true } color-eyre = { version = "0.6", optional = true, default-features = false } clap = { version = "4", optional = true, features = ["derive"] } regex = { version = "1", optional = true } @@ -46,6 +46,7 @@ features = ["fmt", "env-filter", "ansi", "smallvec", "tracing-log"] [features] default = [ "application", + "backtrace", "secrets", "testing", ] @@ -57,6 +58,7 @@ application = [ "semver/serde", "terminal" ] +backtrace = ["dep:color-eyre", "dep:backtrace"] config = [ "secrets", "serde", @@ -65,7 +67,7 @@ config = [ ] options = ["clap"] secrets = ["secrecy"] -terminal = ["color-eyre", "termcolor"] +terminal = ["termcolor"] testing = ["regex", "wait-timeout"] trace = [ "tracing", diff --git a/core/src/error/context.rs b/core/src/error/context.rs index 3847b4da..592c23ca 100644 --- a/core/src/error/context.rs +++ b/core/src/error/context.rs @@ -1,6 +1,7 @@ //! Error contexts use super::BoxError; +#[cfg(feature = "backtrace")] use backtrace::Backtrace; use std::fmt::{self, Debug, Display}; @@ -14,6 +15,7 @@ where kind: Kind, /// Backtrace where error occurred + #[cfg(feature = "backtrace")] backtrace: Option, /// Source of the error @@ -26,9 +28,11 @@ where { /// Create a new error context pub fn new(kind: Kind, source: Option) -> Self { + #[cfg(feature = "backtrace")] let backtrace = Some(Backtrace::new_unresolved()); Context { kind, + #[cfg(feature = "backtrace")] backtrace, source, } @@ -40,11 +44,13 @@ where } /// Get the backtrace associated with this error (if available) + #[cfg(feature = "backtrace")] pub fn backtrace(&self) -> Option<&Backtrace> { self.backtrace.as_ref() } /// Extract the backtrace from the context, allowing it to be resolved. + #[cfg(feature = "backtrace")] pub fn into_backtrace(self) -> Option { self.backtrace } diff --git a/core/src/terminal/component.rs b/core/src/terminal/component.rs index 655450fe..06296e45 100644 --- a/core/src/terminal/component.rs +++ b/core/src/terminal/component.rs @@ -14,6 +14,7 @@ impl Terminal { // TODO(tarcieri): handle terminal reinit (without panicking) super::init(color_choice); + #[cfg(feature = "backtrace")] if color_choice != ColorChoice::Never { // TODO(tarcieri): avoid panicking here color_eyre::install().expect("couldn't install color-eyre");