From 0be2b8e05df01837ed2637bfcf198134f97abdea Mon Sep 17 00:00:00 2001 From: mcmah309 Date: Fri, 19 Jul 2024 20:39:40 +0000 Subject: [PATCH] refactor: Formatting --- impl/src/ast.rs | 8 ++------ impl/src/lib.rs | 25 ++++++++++++------------ impl/src/validate.rs | 45 ++++++++++++++++++++++++++++++++------------ src/lib.rs | 7 +++---- 4 files changed, 50 insertions(+), 35 deletions(-) diff --git a/impl/src/ast.rs b/impl/src/ast.rs index 75c93c3..cfa89eb 100644 --- a/impl/src/ast.rs +++ b/impl/src/ast.rs @@ -166,21 +166,17 @@ pub(crate) fn is_type_path_equal(path1: &syn::TypePath, path2: &syn::TypePath) - .all(|(seg1, seg2)| seg1.ident == seg2.ident); } - #[derive(Clone)] pub(crate) struct AstErrorVariant { pub(crate) attributes: Vec, - pub(crate) name: Ident, + pub(crate) name: Ident, } impl Parse for AstErrorVariant { fn parse(input: ParseStream) -> Result { let attributes = input.call(Attribute::parse_outer)?; let name = input.parse::()?; - Ok(AstErrorVariant { - attributes, - name, - }) + Ok(AstErrorVariant { attributes, name }) } } diff --git a/impl/src/lib.rs b/impl/src/lib.rs index e7273cb..1a6b936 100644 --- a/impl/src/lib.rs +++ b/impl/src/lib.rs @@ -31,9 +31,13 @@ fn construct_error_enums(error_set: AstErrorSet) -> syn::Result> let mut error_enum_builders: Vec = Vec::new(); for declaration in error_set.set_items.into_iter() { - let AstErrorDeclaration { attributes: attribute, error_name, parts } = declaration; + let AstErrorDeclaration { + attributes: attribute, + error_name, + parts, + } = declaration; - let mut error_enum_builder = ErrorEnumBuilder::new(error_name,attribute); + let mut error_enum_builder = ErrorEnumBuilder::new(error_name, attribute); for part in parts.into_iter() { match part { @@ -68,7 +72,7 @@ fn resolve(mut error_enum_builders: Vec) -> syn::Result( - index: usize, + index: usize, error_enum_builders: &'a mut [ErrorEnumBuilder], visited: &mut Vec, ) -> syn::Result> { @@ -87,8 +91,7 @@ fn resolve_helper<'a>( ), )); } - let ref_parts_to_resolve = error_enum_builders[index] - .ref_parts_to_resolve.clone(); + let ref_parts_to_resolve = error_enum_builders[index].ref_parts_to_resolve.clone(); if !ref_parts_to_resolve.is_empty() { for ref_part in ref_parts_to_resolve { let ref_error_enum_index = error_enum_builders @@ -97,10 +100,7 @@ fn resolve_helper<'a>( let ref_error_enum_index = match ref_error_enum_index { Some(e) => e, None => { - return Err(syn::parse::Error::new_spanned( - &ref_part, - format!("") - )); + return Err(syn::parse::Error::new_spanned(&ref_part, format!(""))); } }; if !error_enum_builders[ref_error_enum_index] @@ -111,7 +111,8 @@ fn resolve_helper<'a>( resolve_helper(ref_error_enum_index, error_enum_builders, visited)?; visited.pop(); } - let (this_error_enum_builder, ref_error_enum_builder) = indices::indices!(&mut *error_enum_builders, index, ref_error_enum_index); + let (this_error_enum_builder, ref_error_enum_builder) = + indices::indices!(&mut *error_enum_builders, index, ref_error_enum_index); for variant in ref_error_enum_builder.error_variants.iter() { let this_error_variants = &mut this_error_enum_builder.error_variants; if !this_error_variants.contains(&variant) { @@ -119,9 +120,7 @@ fn resolve_helper<'a>( } } } - error_enum_builders[index] - .ref_parts_to_resolve - .clear(); + error_enum_builders[index].ref_parts_to_resolve.clear(); } // Now that are refs are solved and included in this's error_variants, return them. Ok(error_enum_builders[index].error_variants.clone()) diff --git a/impl/src/validate.rs b/impl/src/validate.rs index 150b27f..dc396e5 100644 --- a/impl/src/validate.rs +++ b/impl/src/validate.rs @@ -14,8 +14,11 @@ fn all_enums_have_unique_names(error_enums: &Vec) -> Result<(), syn:: for error_enum in error_enums { if unique_names.contains(&error_enum.error_name) { return Err(syn::parse::Error::new_spanned( - quote::quote!{error_enum}, - &format!("'{0}' already exists as an error enum.", error_enum.error_name), + quote::quote! {error_enum}, + &format!( + "'{0}' already exists as an error enum.", + error_enum.error_name + ), )); } unique_names.insert(&error_enum.error_name); @@ -23,26 +26,41 @@ fn all_enums_have_unique_names(error_enums: &Vec) -> Result<(), syn:: Ok(()) } -fn only_one_source_of_each_type_per_enum_and_unique_variant_names_per_enum(error_enums: &Vec) -> Result<(), syn::Error> { +fn only_one_source_of_each_type_per_enum_and_unique_variant_names_per_enum( + error_enums: &Vec, +) -> Result<(), syn::Error> { let mut unique_variants: HashSet<&Ident> = HashSet::new(); let mut unique_sources: HashSet = HashSet::new(); for error_enum in error_enums { - for variant in &error_enum.error_variants { + for variant in &error_enum.error_variants { match variant { AstErrorEnumVariant::SourceErrorVariant(source_variant) => { let source_variant_name = &source_variant.name; if unique_variants.contains(source_variant_name) { return Err(syn::parse::Error::new_spanned( - quote::quote!{source_variant}, - &format!("A variant with name '{0}' already exists in error enum '{1}'", source_variant_name, error_enum.error_name), + quote::quote! {source_variant}, + &format!( + "A variant with name '{0}' already exists in error enum '{1}'", + source_variant_name, error_enum.error_name + ), )); } unique_variants.insert(source_variant_name); - let source_variant_source = source_variant.source.path.segments.iter().map(|seg| seg.ident.to_string()).collect::>().join("::"); + let source_variant_source = source_variant + .source + .path + .segments + .iter() + .map(|seg| seg.ident.to_string()) + .collect::>() + .join("::"); if unique_sources.contains(&source_variant_source) { return Err(syn::parse::Error::new_spanned( &source_variant.source, - &format!("A variant with source '{0}' already exists in error enum '{1}'", source_variant_source, error_enum.error_name), + &format!( + "A variant with source '{0}' already exists in error enum '{1}'", + source_variant_source, error_enum.error_name + ), )); } unique_sources.insert(source_variant_source); @@ -50,15 +68,18 @@ fn only_one_source_of_each_type_per_enum_and_unique_variant_names_per_enum(error AstErrorEnumVariant::Variant(variant) => { if unique_variants.contains(&variant.name) { return Err(syn::parse::Error::new_spanned( - quote::quote!{variant}, - &format!("A variant with name '{0}' already exists in error enum '{1}'", variant.name, error_enum.error_name), + quote::quote! {variant}, + &format!( + "A variant with name '{0}' already exists in error enum '{1}'", + variant.name, error_enum.error_name + ), )); } - }, + } } } unique_variants.clear(); unique_sources.clear(); } Ok(()) -} \ No newline at end of file +} diff --git a/src/lib.rs b/src/lib.rs index a17719a..f12eb70 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,13 +3,12 @@ pub use error_set_impl::*; pub trait CoerceResult { - fn coerce>(self) -> Result; + fn coerce>(self) -> Result; } -impl CoerceResult for Result -{ +impl CoerceResult for Result { #[inline(always)] fn coerce>(self) -> Result { self.map_err(Into::::into) } -} \ No newline at end of file +}