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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, quote_spanned};
use syn::Visibility;

use super::{ReturnedWidget, Widget};
use crate::widgets::{
    ConditionalBranches, ConditionalWidget, Properties, Property, PropertyType, SignalHandler,
};

impl Property {
    fn struct_fields_stream(&self, stream: &mut TokenStream2, vis: &Option<Visibility>) {
        match &self.ty {
            PropertyType::Widget(widget) => widget.struct_fields_stream(stream, vis),
            PropertyType::SignalHandler(signal_handler) => {
                signal_handler.struct_fields_stream(stream, vis);
            }
            PropertyType::ConditionalWidget(cond_widget) => {
                cond_widget.struct_fields_stream(stream, vis);
            }
            PropertyType::Assign(_) | PropertyType::ParseError(_) => (),
        }
    }
}

impl Properties {
    fn struct_fields_stream(&self, stream: &mut TokenStream2, vis: &Option<Visibility>) {
        for prop in &self.properties {
            prop.struct_fields_stream(stream, vis);
        }
    }
}

impl Widget {
    pub(crate) fn struct_fields_stream(&self, stream: &mut TokenStream2, vis: &Option<Visibility>) {
        if self.has_struct_field() {
            let name = &self.name;
            let ty = self.func_type_token_stream();

            stream.extend(if let Some(docs) = &self.doc_attr {
                quote! {
                    #[doc = #docs]
                    #vis #name: #ty,
                }
            } else {
                quote! {
                    #[allow(missing_docs)]
                    #vis #name: #ty,
                }
            });
        }

        self.properties.struct_fields_stream(stream, vis);
        if let Some(returned_widget) = &self.returned_widget {
            returned_widget.struct_fields_stream(stream, vis);
        }
    }
}

impl ConditionalWidget {
    fn struct_fields_stream(&self, stream: &mut TokenStream2, vis: &Option<Visibility>) {
        let name = &self.name;
        let gtk_import = crate::gtk_import();

        stream.extend(if let Some(docs) = &self.doc_attr {
            quote_spanned! {
                name.span() =>
                   #[doc = #docs]
                   #vis #name: #gtk_import::Stack,
            }
        } else {
            quote_spanned! {
                name.span() =>
                    #[allow(missing_docs)]
                    #vis #name: #gtk_import::Stack,
            }
        });

        match &self.branches {
            ConditionalBranches::If(if_branches) => {
                for branch in if_branches {
                    branch.widget.struct_fields_stream(stream, vis);
                }
            }
            ConditionalBranches::Match((_, _, match_arms)) => {
                for arm in match_arms {
                    arm.widget.struct_fields_stream(stream, vis);
                }
            }
        }
    }
}

impl ReturnedWidget {
    fn struct_fields_stream(&self, stream: &mut TokenStream2, vis: &Option<Visibility>) {
        if let Some(ty) = &self.ty {
            let name = &self.name;
            stream.extend(quote! {
                #[allow(missing_docs)]
                #vis #name: #ty,
            });
        }
        self.properties.struct_fields_stream(stream, vis);
    }
}

impl SignalHandler {
    fn struct_fields_stream(&self, stream: &mut TokenStream2, vis: &Option<Visibility>) {
        if let Some(signal_handler_id) = &self.handler_id {
            let gtk_import = crate::gtk_import();
            stream.extend(quote_spanned! {
                signal_handler_id.span() =>
                    #[allow(missing_docs)]
                    #vis #signal_handler_id: #gtk_import::glib::signal::SignalHandlerId,
            });
        }
    }
}