Skip to content

Commit

Permalink
make core::error VS std::error more dry
Browse files Browse the repository at this point in the history
  • Loading branch information
vic1707 committed Nov 8, 2024
1 parent d504f92 commit d8239e8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
20 changes: 10 additions & 10 deletions nutype_macros/src/common/gen/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ pub fn gen_error_type_name(type_name: &TypeName) -> ErrorTypePath {
#[allow(unused_variables)]
pub fn gen_impl_error_trait(error_type_path: &ErrorTypePath) -> TokenStream {
cfg_if! {
if #[cfg(ERROR_IN_CORE)] {
quote! {
impl ::core::error::Error for #error_type_name {
fn source(&self) -> Option<&(dyn ::core::error::Error + 'static)> {
None
}
if #[cfg(any(ERROR_IN_CORE, feature = "std"))] {
cfg_if! {
if #[cfg(ERROR_IN_CORE)] {
let error = quote! { ::core::error::Error };
} else {
let error = quote! { ::std::error::Error };
}
}
} else if #[cfg(feature = "std")] {
};

quote! {
impl ::std::error::Error for #error_type_path {
fn source(&self) -> Option<&(dyn ::std::error::Error + 'static)> {
impl #error for #error_type_path {
fn source(&self) -> Option<&(dyn #error + 'static)> {
None
}
}
Expand Down
21 changes: 8 additions & 13 deletions nutype_macros/src/common/gen/parse_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,21 @@ pub fn gen_def_parse_error(
};

cfg_if! {
if #[cfg(ERROR_IN_CORE)] {
let generics_with_fromstr_and_debug_bounds = add_bound_to_all_type_params(
&generics_with_fromstr_bound,
syn::parse_quote!(::core::fmt::Debug),
);
let impl_error = quote! {
impl #generics_with_fromstr_and_debug_bounds ::core::error::Error for #parse_error_type_name #generics_without_bounds {
fn source(&self) -> Option<&(dyn ::core::error::Error + 'static)> {
None
}
if #[cfg(any(ERROR_IN_CORE, feature = "std"))] {
cfg_if! {
if #[cfg(ERROR_IN_CORE)] {
let error = quote! { ::core::error::Error };
} else {
let error = quote! { ::std::error::Error };
}
};
} else if #[cfg(feature = "std")] {
let generics_with_fromstr_and_debug_bounds = add_bound_to_all_type_params(
&generics_with_fromstr_bound,
syn::parse_quote!(::core::fmt::Debug),
);
let impl_error = quote! {
impl #generics_with_fromstr_and_debug_bounds ::std::error::Error for #parse_error_type_name #generics_without_bounds {
fn source(&self) -> Option<&(dyn ::std::error::Error + 'static)> {
impl #generics_with_fromstr_and_debug_bounds #error for #parse_error_type_name #generics_without_bounds {
fn source(&self) -> Option<&(dyn #error + 'static)> {
None
}
}
Expand Down

0 comments on commit d8239e8

Please sign in to comment.