Skip to content

Commit

Permalink
refactor: Change reference name in wrapped error from source to err
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmah309 committed Oct 24, 2024
1 parent d705853 commit 1605ee4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ error_set! {
InvalidCredentials
};
LoginError = {
// Below is equivalent to `#[display("Io Error: {}", source)]` or `#[display("Io Error: {}")]`
// Below is equivalent to `#[display("Io Error: {}", err)]`
#[display("Io Error: {0}")]
IoError(std::io::Error),
} || AuthError;
Expand Down
8 changes: 4 additions & 4 deletions impl/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl PartialEq for AstErrorEnumVariant {
AstErrorEnumVariant::WrappedVariant(var2),
) => {
// Does not include name, because we only care about the type, since each set can only have one of a type
return is_type_path_equal(&var1.source, &var2.source);
return is_type_path_equal(&var1.error_type, &var2.error_type);
}
(
AstErrorEnumVariant::InlineVariant(variant1),
Expand Down Expand Up @@ -217,7 +217,7 @@ pub(crate) struct AstWrappedErrorVariant {
pub(crate) attributes: Vec<Attribute>,
pub(crate) display: Option<DisplayAttribute>,
pub(crate) name: Ident,
pub(crate) source: syn::TypePath,
pub(crate) error_type: syn::TypePath,
}

impl Parse for AstWrappedErrorVariant {
Expand All @@ -233,15 +233,15 @@ impl Parse for AstWrappedErrorVariant {
attributes,
display,
name,
source,
error_type: source,
})
}
}

impl std::fmt::Debug for AstWrappedErrorVariant {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let source = &self
.source
.error_type
.path
.segments
.iter()
Expand Down
30 changes: 15 additions & 15 deletions impl/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ fn add_enum(error_enum_node: &ErrorEnumGraphNode, token_stream: &mut TokenStream
match error_variant {
AstErrorEnumVariant::WrappedVariant(variant) => {
let name = &variant.name;
let source = &variant.source;
let error_type = &variant.error_type;
let attributes = &variant.attributes;
error_variant_tokens.append_all(quote::quote! {
#(#attributes)*
#name(#source),
#name(#error_type),
});
}
AstErrorEnumVariant::InlineVariant(variant) => {
Expand Down Expand Up @@ -122,7 +122,7 @@ fn impl_error(error_enum_node: &ErrorEnumGraphNode, token_stream: &mut TokenStre
has_source_match_branches = true;
let name = &variant.name;
source_match_branches.append_all(quote::quote! {
#enum_name::#name(ref source) => source.source(),
#enum_name::#name(ref err) => err.source(),
});
}
}
Expand Down Expand Up @@ -175,7 +175,7 @@ fn impl_display(error_enum_node: &ErrorEnumGraphNode, token_stream: &mut TokenSt
// e.g. `"{}"`
if is_format_str(&string) {
error_variant_tokens.append_all(quote::quote! {
#enum_name::#name(ref source) => write!(f, #tokens, source),
#enum_name::#name(ref err) => write!(f, #tokens, err),
});
} else {
// e.g. `"literal str"`
Expand All @@ -186,12 +186,12 @@ fn impl_display(error_enum_node: &ErrorEnumGraphNode, token_stream: &mut TokenSt
} else {
// e.g. `"field: {}", source.field`
error_variant_tokens.append_all(quote::quote! {
#enum_name::#name(ref source) => write!(f, #tokens),
#enum_name::#name(ref err) => write!(f, #tokens),
});
}
} else {
error_variant_tokens.append_all(quote::quote! {
#enum_name::#name(ref source) => write!(f, "{}", source),
#enum_name::#name(ref err) => write!(f, "{}", err),
});
}
}
Expand Down Expand Up @@ -276,7 +276,7 @@ fn impl_froms(error_enum_node: &ErrorEnumGraphNode, token_stream: &mut TokenStre
let error_variant_with_source_matching_sub_error_variant = error_enum.error_variants.iter().filter_map(|error_variant| {
match error_variant {
AstErrorEnumVariant::WrappedVariant(source_error_variant) => {
if is_type_path_equal(&source_error_variant.source, &sub_error_variant.source) {
if is_type_path_equal(&source_error_variant.error_type, &sub_error_variant.error_type) {
return Some(source_error_variant);
}
else {
Expand All @@ -289,7 +289,7 @@ fn impl_froms(error_enum_node: &ErrorEnumGraphNode, token_stream: &mut TokenStre
let error_variant_name =
&error_variant_with_source_matching_sub_error_variant.name;
error_branch_tokens.append_all(quote::quote! {
#sub_error_enum_name::#sub_error_variant_name(source) => #error_enum_name::#error_variant_name(source),
#sub_error_enum_name::#sub_error_variant_name(err) => #error_enum_name::#error_variant_name(err),
});
}
AstErrorEnumVariant::InlineVariant(sub_error_variant) => {
Expand Down Expand Up @@ -331,10 +331,10 @@ fn impl_froms(error_enum_node: &ErrorEnumGraphNode, token_stream: &mut TokenStre
};
}) {
let variant_name = &source.name;
let source = &source.source;
let error_type = &source.error_type;
token_stream.append_all(quote::quote! {
impl From<#source> for #error_enum_name {
fn from(error: #source) -> Self {
impl From<#error_type> for #error_enum_name {
fn from(error: #error_type) -> Self {
#error_enum_name::#variant_name(error)
}
}
Expand Down Expand Up @@ -535,16 +535,16 @@ mod coerce_macro {
AstErrorEnumVariant::WrappedVariant(source_variant) => {
let variant = source_variant.name;
match_arms_return_err.append_all(quote::quote! {
Err(#enum1_name::#variant(source)) => { return Err(#enum2_name::#variant(source)); },
Err(#enum1_name::#variant(err)) => { return Err(#enum2_name::#variant(err)); },
});
match_arms_err.append_all(quote::quote! {
Err(#enum1_name::#variant(source)) => { Err(#enum2_name::#variant(source)) },
Err(#enum1_name::#variant(err)) => { Err(#enum2_name::#variant(err)) },
});
match_arms_return.append_all(quote::quote! {
#enum1_name::#variant(source) => { return #enum2_name::#variant(source); },
#enum1_name::#variant(err) => { return #enum2_name::#variant(err); },
});
match_arms.append_all(quote::quote! {
#enum1_name::#variant(source) => { #enum2_name::#variant(source) },
#enum1_name::#variant(err) => { #enum2_name::#variant(err) },
});
}
AstErrorEnumVariant::InlineVariant(variant) => {
Expand Down
24 changes: 12 additions & 12 deletions impl/src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,36 @@ fn only_one_source_of_each_type_per_enum_and_unique_variant_names_per_enum(
for error_enum in error_enums {
for variant in &error_enum.error_variants {
match variant {
AstErrorEnumVariant::WrappedVariant(source_variant) => {
let source_variant_name = &source_variant.name;
if unique_variants.contains(source_variant_name) {
AstErrorEnumVariant::WrappedVariant(error_variant) => {
let error_variant_name = &error_variant.name;
if unique_variants.contains(error_variant_name) {
return Err(syn::parse::Error::new_spanned(
quote::quote! {source_variant},
quote::quote! {error_variant},
&format!(
"A variant with name '{0}' already exists in error enum '{1}'",
source_variant_name, error_enum.error_name
error_variant_name, error_enum.error_name
),
));
}
unique_variants.insert(source_variant_name);
let source_variant_source = source_variant
.source
unique_variants.insert(error_variant_name);
let source_error_variant = error_variant
.error_type
.path
.segments
.iter()
.map(|seg| seg.ident.to_string())
.collect::<Vec<_>>()
.join("::");
if unique_sources.contains(&source_variant_source) {
if unique_sources.contains(&source_error_variant) {
return Err(syn::parse::Error::new_spanned(
&source_variant.source,
&error_variant.error_type,
&format!(
"A variant with source '{0}' already exists in error enum '{1}'",
source_variant_source, error_enum.error_name
source_error_variant, error_enum.error_name
),
));
}
unique_sources.insert(source_variant_source);
unique_sources.insert(source_error_variant);
}
AstErrorEnumVariant::InlineVariant(variant) => {
if unique_variants.contains(&variant.name) {
Expand Down
4 changes: 2 additions & 2 deletions tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,11 +402,11 @@ pub mod display_ref_error {
IoError(std::io::Error),
};
Y = {
#[display("Y io error: {}", source)]
#[display("Y io error: {}", err)]
IoError(std::io::Error),
};
Y2 = {
#[display("Y2 io error type: {}", source.kind())]
#[display("Y2 io error type: {}", err.kind())]
IoError(std::io::Error),
};
Z = {
Expand Down

0 comments on commit 1605ee4

Please sign in to comment.