| 1 | use crate::error::{Error, Result}; |
|---|---|
| 2 | use crate::expr::{self, Expr}; |
| 3 | use crate::{iter, token}; |
| 4 | use proc_macro::{Span, TokenStream}; |
| 5 | |
| 6 | pub struct Args { |
| 7 | pub condition: Expr, |
| 8 | pub then: Then, |
| 9 | } |
| 10 | |
| 11 | pub enum Then { |
| 12 | Const(Span), |
| 13 | Attribute(TokenStream), |
| 14 | } |
| 15 | |
| 16 | pub fn parse(input: TokenStream) -> Result<Args> { |
| 17 | let ref mut input: &mut IterImpl = iter::new(tokens:input); |
| 18 | let condition: Expr = expr::parse(iter:input)?; |
| 19 | |
| 20 | token::parse_punct(iter:input, ch:',')?; |
| 21 | if input.peek().is_none() { |
| 22 | return Err(Error::new(Span::call_site(), msg:"expected one or more attrs")); |
| 23 | } |
| 24 | |
| 25 | let const_span: Option = token::parse_optional_keyword(iter:input, keyword:"const"); |
| 26 | let then: Then = if let Some(const_span: Span) = const_span { |
| 27 | token::parse_optional_punct(iter:input, ch:','); |
| 28 | token::parse_end(iter:input)?; |
| 29 | Then::Const(const_span) |
| 30 | } else { |
| 31 | Then::Attribute(input.collect()) |
| 32 | }; |
| 33 | |
| 34 | Ok(Args { condition, then }) |
| 35 | } |
| 36 |
