Skip to content

Commit

Permalink
feat: Add support links
Browse files Browse the repository at this point in the history
Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun committed Apr 17, 2024
1 parent 259bc32 commit 2d6b767
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
24 changes: 22 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub struct Metadata {
version: Cow<'static, str>,
authors: Option<Cow<'static, str>>,
homepage: Option<Cow<'static, str>>,
support: Option<Cow<'static, str>>,
}

impl Metadata {
Expand All @@ -70,6 +71,7 @@ impl Metadata {
version: version.into(),
authors: None,
homepage: None,
support: None,
}
}

Expand All @@ -90,6 +92,15 @@ impl Metadata {
}
self
}

/// The support information
pub fn support(mut self, value: impl Into<Cow<'static, str>>) -> Self {
let value = value.into();
if !value.is_empty() {
self.support = value.into();
}
self
}
}

/// Initialize [`Metadata`]
Expand Down Expand Up @@ -121,6 +132,7 @@ macro_rules! metadata {
/// setup_panic!(Metadata::new(env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"))
/// .authors("My Company Support <support@mycompany.com>")
/// .homepage("support.mycompany.com")
/// .support("- Open a support request by email to support@mycompany.com")
/// );
/// ```
#[macro_export]
Expand Down Expand Up @@ -206,8 +218,13 @@ fn write_msg<P: AsRef<Path>>(
file_path: Option<P>,
meta: &Metadata,
) -> IoResult<()> {
let (_version, name, authors, homepage) =
(&meta.version, &meta.name, &meta.authors, &meta.homepage);
let Metadata {
name,
authors,
homepage,
support,
..
} = meta;

writeln!(buffer, "Well, this is embarrassing.\n")?;
writeln!(
Expand All @@ -233,6 +250,9 @@ fn write_msg<P: AsRef<Path>>(
if let Some(authors) = authors {
writeln!(buffer, "- Authors: {authors}")?;
}
if let Some(support) = support {
writeln!(buffer, "\nTo submit the crash report:\n\n{support}")?;
}
writeln!(
buffer,
"\nWe take privacy seriously, and do not perform any \
Expand Down
3 changes: 2 additions & 1 deletion tests/custom-panic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use human_panic::setup_panic;
fn main() {
setup_panic!(metadata!()
.authors("My Company Support <support@mycompany.com")
.homepage("support.mycompany.com"));
.homepage("www.mycompany.com")
.support("- Open a support request by email to support@mycompany.com"));

println!("A normal log message");
panic!("OMG EVERYTHING IS ON FIRE!!!");
Expand Down
8 changes: 6 additions & 2 deletions tests/custom-panic/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ fn release() {
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
- Homepage: www.mycompany.com
- Authors: My Company Support <support@mycompany.com
To submit the crash report:
- Open a support request by email to support@mycompany.com
We take privacy seriously, and do not perform any automated error collection. In order to improve the software, we rely on people to submit reports.
Thank you kindly!
Expand All @@ -26,7 +30,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:10:5:
thread 'main' panicked at tests/custom-panic/src/main.rs:11:5:
OMG EVERYTHING IS ON FIRE!!!
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
"#]])
Expand Down

0 comments on commit 2d6b767

Please sign in to comment.