| 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 = iter::new(input); |
| 18 |
let condition = expr::parse(input)?; |
| 19 |
|
| 20 |
token::parse_punct(input, ',')?; |
| 21 |
if input.peek().is_none() { |
| 22 |
return Err(Error::new(Span::call_site(), "expected one or more attrs")); |
| 23 |
} |
| 24 |
|
| 25 |
let const_span = token::parse_optional_keyword(input, "const"); |
| 26 |
let then = if let Some(const_span) = const_span { |
| 27 |
token::parse_optional_punct(input, ','); |
| 28 |
token::parse_end(input)?; |
| 29 |
Then::Const(const_span) |
| 30 |
} else { |
| 31 |
Then::Attribute(input.collect()) |
| 32 |
}; |
| 33 |
|
| 34 |
Ok(Args { condition, then }) |
| 35 |
} |