Skip to content

Commit

Permalink
More descriptive panic when running out of enum variants
Browse files Browse the repository at this point in the history
  • Loading branch information
CAD97 committed Oct 16, 2018
1 parent 7c862b0 commit bc8451f
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,22 @@ fn derive_FromPest_DataEnum(name: Ident, input: DataEnum) -> DeriveResult {
}
}).fold_results(vec![], accumulate)?;

let variant_names = input
.variants
.iter()
.map(|variant| variant.ident.clone())
.collect_vec();

Ok(quote! {
#(#variants)else* else {
panic!(
"Unexpected {}{:?}",
stringify!(#name),
concat!(
"Expected one of [",
#(#variant_names,)*
"] when parsing ",
stringify!(#name),
", got {:?}"
),
::std::iter::repeat_with(|| it.next_untyped())
.take_while(Option::is_some)
.map(Option::unwrap)
Expand Down

0 comments on commit bc8451f

Please sign in to comment.