From 9b4a0425549ae56f3b33145eaa35c1a144ab40e6 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 1 Feb 2023 09:19:12 -0600 Subject: [PATCH 1/4] feat: Expose `metadata!` constructor --- src/lib.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0f2a1ab..ca14fa4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -52,6 +52,8 @@ use std::panic::PanicInfo; use std::path::{Path, PathBuf}; /// A convenient metadata struct that describes a crate +/// +/// See [`metadata!`] pub struct Metadata { /// The crate version pub version: Cow<'static, str>, @@ -63,6 +65,19 @@ pub struct Metadata { pub homepage: Cow<'static, str>, } +/// Initialize [`Metadata`] +#[macro_export] +macro_rules! metadata { + () => { + Metadata { + version: env!("CARGO_PKG_VERSION").into(), + name: env!("CARGO_PKG_NAME").into(), + authors: env!("CARGO_PKG_AUTHORS").replace(":", ", ").into(), + homepage: env!("CARGO_PKG_HOMEPAGE").into(), + } + }; +} + /// `human-panic` initialisation macro /// /// You can either call this macro with no arguments `setup_panic!()` or @@ -112,12 +127,7 @@ macro_rules! setup_panic { #[cfg(not(debug_assertions))] match ::std::env::var("RUST_BACKTRACE") { Err(_) => { - let meta = Metadata { - version: env!("CARGO_PKG_VERSION").into(), - name: env!("CARGO_PKG_NAME").into(), - authors: env!("CARGO_PKG_AUTHORS").replace(":", ", ").into(), - homepage: env!("CARGO_PKG_HOMEPAGE").into(), - }; + let meta = $crate::metadata!(); panic::set_hook(Box::new(move |info: &PanicInfo| { let file_path = handle_dump(&meta, info); From 7c53320c565cc2288b6e8c875dc3a787babc5cf3 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 1 Feb 2023 09:22:06 -0600 Subject: [PATCH 2/4] refactor: Reduce duplication in setup_panic --- src/lib.rs | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ca14fa4..265ca2d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -108,26 +108,7 @@ macro_rules! setup_panic { #[cfg(not(debug_assertions))] match ::std::env::var("RUST_BACKTRACE") { Err(_) => { - panic::set_hook(Box::new(move |info: &PanicInfo| { - let file_path = handle_dump(&$meta, info); - print_msg(file_path, &$meta) - .expect("human-panic: printing error message to console failed"); - })); - } - Ok(_) => {} - } - }; - - () => { - #[allow(unused_imports)] - use std::panic::{self, PanicInfo}; - #[allow(unused_imports)] - use $crate::{handle_dump, print_msg, Metadata}; - - #[cfg(not(debug_assertions))] - match ::std::env::var("RUST_BACKTRACE") { - Err(_) => { - let meta = $crate::metadata!(); + let meta = $meta; panic::set_hook(Box::new(move |info: &PanicInfo| { let file_path = handle_dump(&meta, info); @@ -138,6 +119,10 @@ macro_rules! setup_panic { Ok(_) => {} } }; + + () => { + $crate::setup_panic!($crate::metadata!()); + }; } /// Utility function that prints a message to our human users From 4c94e7fe413a351dbc43ab0a0ed5c56a308e060a Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 1 Feb 2023 09:24:00 -0600 Subject: [PATCH 3/4] feat: Expose `PanicStyle::default` --- src/lib.rs | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 265ca2d..f3a7a23 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -105,9 +105,9 @@ macro_rules! setup_panic { #[allow(unused_imports)] use $crate::{handle_dump, print_msg, Metadata}; - #[cfg(not(debug_assertions))] - match ::std::env::var("RUST_BACKTRACE") { - Err(_) => { + match $crate::PanicStyle::default() { + $crate::PanicStyle::Debug => {} + $crate::PanicStyle::Human => { let meta = $meta; panic::set_hook(Box::new(move |info: &PanicInfo| { @@ -116,7 +116,6 @@ macro_rules! setup_panic { .expect("human-panic: printing error message to console failed"); })); } - Ok(_) => {} } }; @@ -125,6 +124,28 @@ macro_rules! setup_panic { }; } +/// Style of panic to be used +#[derive(Copy, Clone, PartialEq, Eq)] +pub enum PanicStyle { + /// Normal panic + Debug, + /// Human-formatted panic + Human, +} + +impl Default for PanicStyle { + fn default() -> Self { + if cfg!(debug_assertions) { + PanicStyle::Debug + } else { + match ::std::env::var("RUST_BACKTRACE") { + Ok(_) => PanicStyle::Debug, + Err(_) => PanicStyle::Human, + } + } + } +} + /// Utility function that prints a message to our human users #[cfg(feature = "color")] pub fn print_msg>( From 073a236397861e95378b69147c47a2e0ff049d5d Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 1 Feb 2023 09:26:40 -0600 Subject: [PATCH 4/4] docs: Fix typo --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0095303..2e6cef8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixes - Use normal panics when `RUST_BACKTRACE` is enabled -- Skip unnecesary frames in backtrace +- Skip unnecessary frames in backtrace ## 2018-04-18, Version 0.3.0