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