Skip to content

Commit

Permalink
return typed errors when parsing the user module
Browse files Browse the repository at this point in the history
  • Loading branch information
mrnaveira committed Jul 29, 2022
1 parent 3e47613 commit dbd7b76
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions dan_layer/template_macros/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use syn::{
parse::{Parse, ParseStream},
punctuated::Punctuated,
token::Comma,
Error,
FnArg,
Ident,
ImplItem,
Expand Down Expand Up @@ -51,24 +52,24 @@ impl Parse for TemplateAst {
// get the contents of the "mod" block
let items = match module.content {
Some((_, items)) => items,
None => panic!("empty module"),
None => return Err(Error::new(module.ident.span(), "empty module")),
};

// there should be two items: the "struct" and the "impl" blocks
if items.len() != 2 {
panic!("invalid number of module sections");
return Err(Error::new(module.ident.span(), "invalid number of module sections"));
}

// get the "struct" block
let struct_section = match &items[0] {
syn::Item::Struct(struct_item) => struct_item.clone(),
_ => panic!("the first section is not a 'struct'"),
_ => return Err(Error::new(module.ident.span(), "the first section is not a 'struct'")),
};

// get the "impl" block
let impl_section = match &items[1] {
syn::Item::Impl(impl_item) => impl_item.clone(),
_ => panic!("the second section is not an 'impl'"),
_ => return Err(Error::new(module.ident.span(), "the second section is not an 'impl'")),
};

let template_name = struct_section.ident.clone();
Expand Down

0 comments on commit dbd7b76

Please sign in to comment.