Skip to content

Commit

Permalink
refactor: Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmah309 committed Jul 19, 2024
1 parent ee39fd2 commit 0be2b8e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 35 deletions.
8 changes: 2 additions & 6 deletions impl/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Attribute>,
pub(crate) name: Ident,
pub(crate) name: Ident,
}

impl Parse for AstErrorVariant {
fn parse(input: ParseStream) -> Result<Self> {
let attributes = input.call(Attribute::parse_outer)?;
let name = input.parse::<Ident>()?;
Ok(AstErrorVariant {
attributes,
name,
})
Ok(AstErrorVariant { attributes, name })
}
}

Expand Down
25 changes: 12 additions & 13 deletions impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ fn construct_error_enums(error_set: AstErrorSet) -> syn::Result<Vec<ErrorEnum>>
let mut error_enum_builders: Vec<ErrorEnumBuilder> = 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 {
Expand Down Expand Up @@ -68,7 +72,7 @@ fn resolve(mut error_enum_builders: Vec<ErrorEnumBuilder>) -> syn::Result<Vec<Er
}

fn resolve_helper<'a>(
index: usize,
index: usize,
error_enum_builders: &'a mut [ErrorEnumBuilder],
visited: &mut Vec<Ident>,
) -> syn::Result<Vec<AstErrorEnumVariant>> {
Expand All @@ -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
Expand All @@ -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]
Expand All @@ -111,17 +111,16 @@ 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) {
this_error_variants.push(variant.clone());
}
}
}
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())
Expand Down
45 changes: 33 additions & 12 deletions impl/src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,51 +14,72 @@ fn all_enums_have_unique_names(error_enums: &Vec<ErrorEnum>) -> 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);
}
Ok(())
}

fn only_one_source_of_each_type_per_enum_and_unique_variant_names_per_enum(error_enums: &Vec<ErrorEnum>) -> Result<(), syn::Error> {
fn only_one_source_of_each_type_per_enum_and_unique_variant_names_per_enum(
error_enums: &Vec<ErrorEnum>,
) -> Result<(), syn::Error> {
let mut unique_variants: HashSet<&Ident> = HashSet::new();
let mut unique_sources: HashSet<String> = 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::<Vec<_>>().join("::");
let source_variant_source = source_variant
.source
.path
.segments
.iter()
.map(|seg| seg.ident.to_string())
.collect::<Vec<_>>()
.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);
}
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(())
}
}
7 changes: 3 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
pub use error_set_impl::*;

pub trait CoerceResult<T, E1> {
fn coerce<E2: From<E1>>(self) -> Result<T,E2>;
fn coerce<E2: From<E1>>(self) -> Result<T, E2>;
}

impl<T, E1> CoerceResult<T,E1> for Result<T, E1>
{
impl<T, E1> CoerceResult<T, E1> for Result<T, E1> {
#[inline(always)]
fn coerce<E2: From<E1>>(self) -> Result<T, E2> {
self.map_err(Into::<E2>::into)
}
}
}

0 comments on commit 0be2b8e

Please sign in to comment.