nrelm-macros 0.2.0

An idiomatic GUI library inspired by Elm and based on gtk3-rs
Documentation
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 {
    /// A row with a label and an action handler.
    Labeled { label: LitStr, handler: Handler },
    /// A custom widget that is inserted into the list box as-is.
    Widget(Expr),
}

#[derive(Debug)]
enum Handler {
    /// The message is sent to the component's sender.
    Msg(Expr),
    /// A custom handler block.
    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 })
    }
}