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
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, ToTokens};
use syn::Ident;

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

impl Property {
    fn error_stream(&self, stream: &mut TokenStream2, w_name: &Ident) {
        match &self.ty {
            PropertyType::ParseError(error) => error.error_stream(stream, w_name),
            PropertyType::SignalHandler(_) | PropertyType::Assign(_) => (),
            PropertyType::Widget(widget) => widget.error_stream(stream),
            PropertyType::ConditionalWidget(cond_widget) => cond_widget.error_stream(stream),
        }
    }
}

impl Properties {
    fn error_stream(&self, stream: &mut TokenStream2, w_name: &Ident) {
        for prop in &self.properties {
            prop.error_stream(stream, w_name);
        }
    }
}

impl ParseError {
    fn error_stream(&self, stream: &mut TokenStream2, w_name: &Ident) {
        match self {
            ParseError::Ident((ident, tokens)) => stream.extend(quote! {
                #tokens
                #w_name.#ident ;
            }),
            ParseError::Path((path, tokens)) => stream.extend(quote! {
                #tokens
                #path ;
            }),
            ParseError::Generic(generic_error) => generic_error.to_tokens(stream),
        }
    }
}

impl Widget {
    pub(crate) fn error_stream(&self, stream: &mut TokenStream2) {
        let w_name = &self.name;
        self.properties.error_stream(stream, w_name);
        if let Some(returned_widget) = &self.returned_widget {
            returned_widget.error_stream(stream);
        }
    }
}

impl ConditionalWidget {
    fn error_stream(&self, stream: &mut TokenStream2) {
        match &self.branches {
            ConditionalBranches::If(if_branches) => {
                for branch in if_branches {
                    branch.widget.error_stream(stream);
                }
            }
            ConditionalBranches::Match((_, _, match_arms)) => {
                for arm in match_arms {
                    arm.widget.error_stream(stream);
                }
            }
        }
    }
}

impl ReturnedWidget {
    fn error_stream(&self, stream: &mut TokenStream2) {
        let w_name = &self.name;
        self.properties.error_stream(stream, w_name);
    }
}