From fda5aabc90aebb53bfda5ec80424fa6872c34466 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 16 Apr 2024 21:18:10 -0500 Subject: [PATCH 1/4] fix!: Make Method non-exhaustive --- src/report.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/report.rs b/src/report.rs index cbed546..f1a4e63 100644 --- a/src/report.rs +++ b/src/report.rs @@ -12,8 +12,8 @@ use std::{env, fs::File, io::Write, path::Path, path::PathBuf}; use uuid::Uuid; /// Method of failure. -#[allow(clippy::exhaustive_enums)] #[derive(Debug, Serialize, Clone, Copy)] +#[non_exhaustive] pub enum Method { /// Failure caused by a panic. Panic, From 45919c80d905dbb97a9fc54206396d7b747c875f Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 16 Apr 2024 21:36:16 -0500 Subject: [PATCH 2/4] fix!: Pull out a setup_panic methid BREAKING CHANGE: Require the `Metadata` parameter to be fully qualified --- src/lib.rs | 38 ++++++++++++++----------- tests/custom-panic/src/main.rs | 1 + tests/custom-panic/tests/integration.rs | 4 +-- tests/single-panic/tests/integration.rs | 2 +- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ff7949b..9da231b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -94,6 +94,7 @@ macro_rules! metadata { /// /// ``` /// use human_panic::setup_panic; +/// use human_panic::Metadata; /// /// setup_panic!(Metadata { /// name: env!("CARGO_PKG_NAME").into(), @@ -105,23 +106,7 @@ macro_rules! metadata { #[macro_export] macro_rules! setup_panic { ($meta:expr) => {{ - #[allow(unused_imports)] - use std::panic::{self, PanicInfo}; - #[allow(unused_imports)] - use $crate::{handle_dump, print_msg, Metadata}; - - match $crate::PanicStyle::default() { - $crate::PanicStyle::Debug => {} - $crate::PanicStyle::Human => { - let meta = $meta; - - 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"); - })); - } - } + $crate::setup_panic(|| $meta); }}; () => { @@ -129,6 +114,25 @@ macro_rules! setup_panic { }; } +#[doc(hidden)] +pub fn setup_panic(meta: impl Fn() -> Metadata) { + #[allow(unused_imports)] + use std::panic::{self, PanicInfo}; + + match PanicStyle::default() { + PanicStyle::Debug => {} + PanicStyle::Human => { + let meta = meta(); + + 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"); + })); + } + } +} + /// Style of panic to be used #[allow(clippy::exhaustive_enums)] #[derive(Copy, Clone, PartialEq, Eq)] diff --git a/tests/custom-panic/src/main.rs b/tests/custom-panic/src/main.rs index ad7843e..1689caa 100644 --- a/tests/custom-panic/src/main.rs +++ b/tests/custom-panic/src/main.rs @@ -1,4 +1,5 @@ use human_panic::setup_panic; +use human_panic::Metadata; fn main() { setup_panic!(Metadata { diff --git a/tests/custom-panic/tests/integration.rs b/tests/custom-panic/tests/integration.rs index 85a5810..6c728a9 100644 --- a/tests/custom-panic/tests/integration.rs +++ b/tests/custom-panic/tests/integration.rs @@ -8,7 +8,7 @@ fn release() { custom-panic-test had a problem and crashed. To help us diagnose the problem you can send us a crash report. - We have generated a report file at "/tmp/report-f599c209-16f1-4858-a6b4-2591a01061e2.toml". Submit an issue or email with the subject of "custom-panic-test Crash Report" and include the report as an attachment. + We have generated a report file at "[..].toml". Submit an issue or email with the subject of "custom-panic-test Crash Report" and include the report as an attachment. - Homepage: support.mycompany.com - Authors: My Company Support From 2215653557495e48117560a51223c01602407bde Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 16 Apr 2024 21:18:47 -0500 Subject: [PATCH 3/4] fix!: Make PanicStyle non-exhaustive --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 9da231b..42453d8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -134,7 +134,7 @@ pub fn setup_panic(meta: impl Fn() -> Metadata) { } /// Style of panic to be used -#[allow(clippy::exhaustive_enums)] +#[non_exhaustive] #[derive(Copy, Clone, PartialEq, Eq)] pub enum PanicStyle { /// Normal panic From 86a3f15d97e6b24009482d9e079a5a07e9273784 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 16 Apr 2024 21:39:12 -0500 Subject: [PATCH 4/4] fix!: Add a builder API to Metadata --- src/lib.rs | 62 ++++++++++++++++--------- tests/custom-panic/src/main.rs | 11 ++--- tests/custom-panic/tests/integration.rs | 2 +- 3 files changed, 46 insertions(+), 29 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 42453d8..6195518 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -55,28 +55,50 @@ use std::path::{Path, PathBuf}; /// A convenient metadata struct that describes a crate /// /// See [`metadata!`] -#[allow(clippy::exhaustive_structs)] pub struct Metadata { - /// The crate version - pub version: Cow<'static, str>, - /// The crate name - pub name: Cow<'static, str>, + name: Cow<'static, str>, + version: Cow<'static, str>, + authors: Option>, + homepage: Option>, +} + +impl Metadata { + /// See [`metadata!`] + pub fn new(name: impl Into>, version: impl Into>) -> Self { + Self { + name: name.into(), + version: version.into(), + authors: None, + homepage: None, + } + } + /// The list of authors of the crate - pub authors: Cow<'static, str>, + pub fn authors(mut self, value: impl Into>) -> Self { + let value = value.into(); + if !value.is_empty() { + self.authors = value.into(); + } + self + } + /// The URL of the crate's website - pub homepage: Cow<'static, str>, + pub fn homepage(mut self, value: impl Into>) -> Self { + let value = value.into(); + if !value.is_empty() { + self.homepage = value.into(); + } + self + } } /// Initialize [`Metadata`] #[macro_export] macro_rules! metadata { () => {{ - $crate::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(), - } + $crate::Metadata::new(env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")) + .authors(env!("CARGO_PKG_AUTHORS").replace(":", ", ")) + .homepage(env!("CARGO_PKG_HOMEPAGE")) }}; } @@ -96,12 +118,10 @@ macro_rules! metadata { /// use human_panic::setup_panic; /// use human_panic::Metadata; /// -/// setup_panic!(Metadata { -/// name: env!("CARGO_PKG_NAME").into(), -/// version: env!("CARGO_PKG_VERSION").into(), -/// authors: "My Company Support ".into(), -/// homepage: "support.mycompany.com".into(), -/// }); +/// setup_panic!(Metadata::new(env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")) +/// .authors("My Company Support ") +/// .homepage("support.mycompany.com") +/// ); /// ``` #[macro_export] macro_rules! setup_panic { @@ -207,10 +227,10 @@ fn write_msg>( name )?; - if !homepage.is_empty() { + if let Some(homepage) = homepage { writeln!(buffer, "- Homepage: {homepage}")?; } - if !authors.is_empty() { + if let Some(authors) = authors { writeln!(buffer, "- Authors: {authors}")?; } writeln!( diff --git a/tests/custom-panic/src/main.rs b/tests/custom-panic/src/main.rs index 1689caa..b06bcc3 100644 --- a/tests/custom-panic/src/main.rs +++ b/tests/custom-panic/src/main.rs @@ -1,13 +1,10 @@ +use human_panic::metadata; use human_panic::setup_panic; -use human_panic::Metadata; fn main() { - setup_panic!(Metadata { - name: env!("CARGO_PKG_NAME").into(), - version: env!("CARGO_PKG_VERSION").into(), - authors: "My Company Support