use syn::punctuated::Punctuated;
use syn::token::Comma;
use syn::{Expr, LitStr};
mod codegen;
#[derive(Debug)]
pub(crate) struct List {
rows: Punctuated<Row, Comma>,
}
#[derive(Debug)]
enum Row {
Labeled { label: LitStr, handler: Handler },
Widget(Expr),
}
#[derive(Debug)]
enum Handler {
Msg(Expr),
Block(Expr),
}
impl syn::parse::Parse for List {
fn parse(input: syn::parse::ParseStream<'_>) -> syn::Result<Self> {
let mut rows = Punctuated::new();
while !input.is_empty() {
if input.peek(LitStr) {
let label: LitStr = input.parse()?;
let _arrow: syn::token::FatArrow = input.parse().map_err(|_| {
syn::Error::new(
label.span(),
"expected `=>` and an action after the row label",
)
})?;
let handler_expr: Expr = input.parse()?;
let handler = if matches!(handler_expr, Expr::Block(_)) {
Handler::Block(handler_expr)
} else {
Handler::Msg(handler_expr)
};
rows.push(Row::Labeled { label, handler });
} else {
let widget: Expr = input.parse()?;
rows.push(Row::Widget(widget));
}
if input.is_empty() {
break;
}
let comma: Comma = input.parse()?;
rows.push_punct(comma);
}
Ok(List { rows })
}
}