I am building a macro parser with syn and need to check if a ParseStream
is one of several keywords. The code currently looks similar to this:
mod kw {
syn::custom_keyword!(a);
syn::custom_keyword!(b);
syn::custom_keyword!(c);
}
enum MyKeywordEnum {
A(kw::a),
B(kw::b),
C(kw::c),
}
impl syn::parse::Parse for MyKeywordEnum {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
Ok(if let Ok(ret) = input.parse::<kw::a>() {
Self::A(ret)
} else if let Ok(ret) = input.parse::<kw::b>() {
Self::B(ret)
} else if let Ok(ret) = input.parse::<kw::c>() {
Self::C(ret)
} else {
abort!(input.span(), "Couldn't parse primitive type"); // Via #[proc_macro_error]
})
}
}
- Is there a built-in operator or macro to return immediately if an expression is
Option::Some
orResult::Ok
? - Is there a better way to organize these checks?
- Because
ParseStream::parse
statically compiles to the specificimpl Parse
type, I can't usematch
, right?