1
Fork 0
mirror of https://github.com/Steffo99/micronfig.git synced 2024-12-22 20:14:18 +00:00

Use unwrap_or_else and panic! instead of expect and format! in macros too

This commit is contained in:
Steffo 2024-12-19 06:28:55 +01:00
parent acd38f75e4
commit 24069da00f
Signed by: steffo
GPG key ID: 5ADA3868646C3FC0

View file

@ -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));
},
};