use syn::parse::{Parse, ParseStream};
enum Start {
ProductEntity(ProductEntity),
Rust(RustCode),
}
impl Parse for Start {
fn parse(input: ParseStream) -> syn::Result<Self> {
if input.peek(syn::token::Dollar) {
input.parse().map(Start::ProductEntity)
} else {
input.parse().map(Start::Rust)
}
}
}
#[test]
fn test_start() {
let input = quote! {
$ { }
$ ( )
$ [ ]
$ rust ;
};
let start: Start = syn::parse2(input).unwrap();
match start {
Start::ProductEntity(_) => {}
Start::Rust(_) => {}
}
}
struct RustCode {
code: Vec<Token>,
}
impl Parse for RustCode {
fn parse(input: ParseStream) -> syn::Result<Self> {
input.parse().map(|code| RustCode { code })
}
}
enum ProductEntity {
Scope(Scope),
Definition(Definition),
Reference(Reference),
}
impl Parse for ProductEntity {
fn parse(input: ParseStream) -> syn::Result<Self> {
input.parse::<syn::token::Dollar>()?;
if input.peek(syn::token::Brace) {
input.parse().map(ProductEntity::Scope)
} else if input.peek(syn::token::Paren) {
input.parse().map(ProductEntity::Definition)
} else {
input.parse().map(ProductEntity::Reference)
}
}
}