-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #137 from epage/exhaust
fix!: Make the API more resilient to change
- Loading branch information
Showing
5 changed files
with
71 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<Cow<'static, str>>, | ||
homepage: Option<Cow<'static, str>>, | ||
} | ||
|
||
impl Metadata { | ||
/// See [`metadata!`] | ||
pub fn new(name: impl Into<Cow<'static, str>>, version: impl Into<Cow<'static, str>>) -> 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<Cow<'static, str>>) -> 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<Cow<'static, str>>) -> 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")) | ||
}}; | ||
} | ||
|
||
|
@@ -94,43 +116,45 @@ 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 <[email protected]>".into(), | ||
/// homepage: "support.mycompany.com".into(), | ||
/// }); | ||
/// setup_panic!(Metadata::new(env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")) | ||
/// .authors("My Company Support <[email protected]>") | ||
/// .homepage("support.mycompany.com") | ||
/// ); | ||
/// ``` | ||
#[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); | ||
}}; | ||
|
||
() => { | ||
$crate::setup_panic!($crate::metadata!()); | ||
}; | ||
} | ||
|
||
#[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)] | ||
#[non_exhaustive] | ||
#[derive(Copy, Clone, PartialEq, Eq)] | ||
pub enum PanicStyle { | ||
/// Normal panic | ||
|
@@ -203,10 +227,10 @@ fn write_msg<P: AsRef<Path>>( | |
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!( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,10 @@ | ||
use human_panic::metadata; | ||
use human_panic::setup_panic; | ||
|
||
fn main() { | ||
setup_panic!(Metadata { | ||
name: env!("CARGO_PKG_NAME").into(), | ||
version: env!("CARGO_PKG_VERSION").into(), | ||
authors: "My Company Support <[email protected]".into(), | ||
homepage: "support.mycompany.com".into(), | ||
}); | ||
setup_panic!(metadata!() | ||
.authors("My Company Support <[email protected]") | ||
.homepage("support.mycompany.com")); | ||
|
||
println!("A normal log message"); | ||
panic!("OMG EVERYTHING IS ON FIRE!!!"); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[email protected] | ||
|
@@ -26,7 +26,7 @@ fn debug() { | |
snapbox::cmd::Command::new(snapbox::cmd::cargo_bin!("custom-panic-test")) | ||
.assert() | ||
.stderr_matches(snapbox::str![[r#" | ||
thread 'main' panicked at tests/custom-panic/src/main.rs:12:5: | ||
thread 'main' panicked at tests/custom-panic/src/main.rs:10:5: | ||
OMG EVERYTHING IS ON FIRE!!! | ||
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace | ||
"#]]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ fn release() { | |
single-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-ee5cb7e8-1010-4824-bf57-b9726141e881.toml". Submit an issue or email with the subject of "single-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 "single-panic-test Crash Report" and include the report as an attachment. | ||
- Authors: Human Panic Authors <[email protected]> | ||
|