nrelm-macros 0.2.0

An idiomatic GUI library inspired by Elm and based on gtk3-rs
Documentation
use proc_macro2::TokenStream as TokenStream2;
use quote::{ToTokens, quote, quote_spanned};
use syn::Ident;

use crate::widgets::codegen::{PropertyName, assign::AssignInfo};

/// Setter methods provided by `nrelm::RelmWidgetExt`.
///
/// These are generated as universal function call syntax
/// (`nrelm::RelmWidgetExt::set_expand(&widget, ...)`) instead of plain method
/// calls, because GTK3's `gtk::WidgetExt` also defines some of them (e.g.
/// `set_expand`, which sets the broken GTK3 "expand" property) causing
/// ambiguous method-call resolution (`E0034`) when both traits are in scope.
fn is_relm_widget_ext_setter(ident: &Ident) -> bool {
    matches!(
        ident.to_string().as_str(),
        "set_expand"
            | "set_align"
            | "set_margin_all"
            | "set_margin_vertical"
            | "set_margin_horizontal"
            | "set_focusable"
            | "set_class_active"
            | "set_size_group"
            | "set_tooltip"
            | "inline_css"
            | "add_css_class"
            | "remove_css_class"
    )
}

impl AssignInfo<'_> {
    /// The expression the widget is addressed by: either the plain widget name
    /// or the path through a `#[template]` widget (`template.child`).
    fn receiver_stream(&self) -> TokenStream2 {
        if let Some(template_path) = &self.template_path {
            quote! { #template_path }
        } else {
            let widget_name = self.widget_name;
            quote! { #widget_name }
        }
    }
}

impl PropertyName {
    pub(crate) fn assign_fn_stream(&self, info: &mut AssignInfo<'_>) -> TokenStream2 {
        let widget_name = info.receiver_stream();

        match self {
            PropertyName::Ident(ident) if is_relm_widget_ext_setter(ident) => {
                quote! { nrelm::RelmWidgetExt::#ident }
            }
            PropertyName::Ident(ident) => {
                quote! { #widget_name.#ident }
            }
            PropertyName::Path(path) => path.to_token_stream(),
            PropertyName::RelmContainerExtAssign(span) => {
                quote_spanned! { *span => #widget_name.container_add }
            }
        }
    }

    pub(crate) fn assign_args_stream(
        &self,
        info: &AssignInfo<'_>,
        init: bool,
    ) -> Option<TokenStream2> {
        let widget_name = info.receiver_stream();
        let receiver = if init {
            quote! { & #widget_name }
        } else {
            // In `update_view` the widgets are destructured from
            // `&mut Self::Widgets`, so e.g. a `&mut` widget binding coerces
            // to `&` when passed by value.
            widget_name
        };

        match self {
            PropertyName::Ident(ident) if is_relm_widget_ext_setter(ident) => {
                Some(quote_spanned! { ident.span() => #receiver, })
            }
            PropertyName::RelmContainerExtAssign(_) | PropertyName::Ident(_) => None,
            PropertyName::Path(_) => Some(quote_spanned! { info.widget_name.span() => #receiver, }),
        }
    }
}