Skip to content

Commit 2fcc2d6

Browse files
committed
get more struct about supplying external spec
1 parent e515e55 commit 2fcc2d6

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

crates/formality-macros/src/parse.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,19 @@ pub(crate) fn derive_parse_with_spec(
2727

2828
let type_name = Literal::string(&format!("`{}`", s.ast().ident));
2929

30-
if s.variants().len() == 1 {
30+
if let syn::Data::Struct(_) = s.ast().data {
31+
// For structs, where there is only one variant, use the external grammar (if any).
3132
stream.extend(parse_variant(&type_name, &s.variants()[0], external_spec)?);
3233
} else {
34+
// If there are multiple variants, there should not be an external grammar.
35+
// Parse them all and require an unambiguous parse.
36+
if external_spec.is_some() {
37+
return Err(syn::Error::new_spanned(
38+
&s.ast().ident,
39+
"for enums provide the grammar on each variant".to_string(),
40+
));
41+
}
42+
3343
stream.extend(quote! {
3444
let mut __results = vec![];
3545
});

0 commit comments

Comments
 (0)