-
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.
- Loading branch information
Showing
3 changed files
with
46 additions
and
29 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")) | ||
}}; | ||
} | ||
|
||
|
@@ -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 <[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 { | ||
|
@@ -207,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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <[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