From 24069da00ff97d04553c75acc180fe747deae08a Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 19 Dec 2024 06:28:55 +0100 Subject: [PATCH] Use `unwrap_or_else` and `panic!` instead of `expect` and `format!` in macros too --- micronfig_macros/src/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/micronfig_macros/src/lib.rs b/micronfig_macros/src/lib.rs index 4051ab9..ecd0c64 100644 --- a/micronfig_macros/src/lib.rs +++ b/micronfig_macros/src/lib.rs @@ -138,12 +138,12 @@ pub fn config(input: TokenStream) -> TokenStream { (Conversion::TryFrom, true) => quote! { let value: Option<#typ> = value .map(|v| v.try_into()) - .map(|v| v.expect(&format!("to be able to convert {}", #identifier_string))); + .map(|v| v.unwrap_or_else(|err| panic!("Couldn't perform conversion `{:?} => {:?}`: {:#?}", v, #identifier_string, err))); }, (Conversion::FromStr, true) => quote! { let value: Option<#typ> = value .map(|v| v.parse()) - .map(|v| v.expect(&format!("to be able to parse {}", #identifier_string))); + .map(|v| v.unwrap_or_else(|err| panic!("Couldn't perform conversion `{:?} > {:?}`: {:#?}", v, #identifier_string, err))); }, (Conversion::From, false) => quote! { let value: #typ = value @@ -152,12 +152,12 @@ pub fn config(input: TokenStream) -> TokenStream { (Conversion::TryFrom, false) => quote! { let value: #typ = value .try_into() - .expect(&format!("to be able to convert {}", #identifier_string)); + .unwrap_or_else(|err| panic!("Couldn't perform conversion `{:?} => {:?}`: {:#?}", value, #identifier_string, err)); }, (Conversion::FromStr, false) => quote! { let value: #typ = value .parse() - .expect(&format!("to be able to parse {}", #identifier_string)); + .unwrap_or_else(|err| panic!("Couldn't perform conversion `{:?} > {:?}`: {:#?}", value, #identifier_string, err)); }, } } @@ -169,7 +169,7 @@ pub fn config(input: TokenStream) -> TokenStream { true => quote! {}, false => quote! { let value: String = value - .expect(&format!("that configuration variable {} was set", #identifier_string)); + .unwrap_or_else(|_| panic!("Unset configuration variable: {}", #identifier_string)); }, };