1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// SPDX-FileCopyrightText: 2022 Agathe Porte <microjoe@microjoe.org>
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! A Rust crate to easily import Glade-generated UI files into Rust code (proc
//! macros).

use proc_macro::TokenStream;
use quote::quote;

use syn::{Data, DataStruct, DeriveInput, Fields};

fn impl_liststore_item(ast: &DeriveInput) -> TokenStream {
    let name = &ast.ident;
    let fields = match &ast.data {
        Data::Struct(DataStruct {
            fields: Fields::Named(fields),
            ..
        }) => &fields.named,
        _ => panic!("expected a struct with named fields"),
    };

    let field_name = fields.iter().map(|field| &field.ident);
    let field_type = fields.iter().map(|field| &field.ty);
    let field_number = 0..fields.len() as i32;
    let from = quote! {
        fn from_liststore_iter<S>(list_store: &S, iter: &gtk::TreeIter) -> Option<Self>
            where S: TreeModelExt
        {
            Some(#name {
                #(
                    #field_name: list_store.value(iter, #field_number).get::<#field_type>().ok()?
                ),*
            })
        }
    };

    let field_name = fields.iter().map(|field| &field.ident);
    let field_pos = 0..fields.len();

    let insert = quote! {
        fn insert_into_liststore<S>(&self, list_store: &mut S) -> gtk::TreeIter
            where S: GtkListStoreExtManual
        {
            list_store.insert_with_values(
                None,
                &[
                    #(
                        (
                            #field_pos as u32,
                            &self.#field_name
                        )
                    ),*
                ],
            )
        }
    };

    let field_type = fields.iter().map(|field| &field.ty);
    let new_liststore = quote! {
        fn new_liststore() -> gtk::ListStore {
            gtk::ListStore::new(
                &[#(
                    #field_type::static_type()),*
                ]
            )
        }
    };

    let gen = quote! {
        impl gtk_liststore_item::ListStoreItem for #name {
            #from
            #insert
            #new_liststore
        }
    };
    gen.into()
}

#[proc_macro_derive(ListStoreItem)]
pub fn derive_liststore_item(input: TokenStream) -> TokenStream {
    let ast = syn::parse(input).unwrap();
    impl_liststore_item(&ast)
}