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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
extern crate proc_macro;

use proc_macro::TokenStream;
use quote::{quote};
use serde::{Deserialize, Serialize};
use syn::{parse_macro_input, Expr, ExprIf, ItemImpl, ImplItem, Stmt};

#[derive(Debug, Deserialize, Serialize)]
struct EventRule {
    event_expression: String,
    event_routine: EventRoutine,
}

#[derive(Debug, Deserialize, Serialize)]
enum EventRoutine {
    ConditionalScheduling(Vec<ConditionalScheduling>),
    UnconditionalStateTransition(String),
}

#[derive(Debug, Deserialize, Serialize)]
struct ConditionalScheduling {
    follow_up_event: String,
    condition: String,
}

impl EventRule {
    fn new_conditional_scheduling(event_expression: String, conditional_schedulings: Vec<ConditionalScheduling>) -> Self {
        Self {
            event_expression,
            event_routine: EventRoutine::ConditionalScheduling(conditional_schedulings),
        }
    }

    fn new_unconditional_state_transition(event_expression: String, event_routine: String) -> Self {
        Self {
            event_expression,
            event_routine: EventRoutine::UnconditionalStateTransition(event_routine),
        }
    }
}

#[proc_macro_attribute]
pub fn event_rules(_attr: TokenStream, item: TokenStream) -> TokenStream {
    let input = parse_macro_input!(item as ItemImpl);
    let output = TokenStream::from(quote!(#input));

    // Check to see if we have an impl AsModel for NewModel, an impl NewModel, or something else
    let mut is_impl = false;
    let mut is_impl_as_model = false;
    if let None = &input.trait_ {
        is_impl = true;
    } else if let Some(trait_) = &input.trait_ {
        if trait_.1.segments[0].ident.to_string() == "AsModel" {
            is_impl_as_model = true;
        }
    }

    let mut event_rules = Vec::new();

    // Get the unconditional state transition event rules if is_impl
    if is_impl {
        input.items.iter()
            .filter_map(|method| {
                if let ImplItem::Method(method) = method {
                    Some(method)
                } else {
                    None
                }
            })
            .for_each(|method| {
                let name = &method.sig.ident;
                let routine = &method.block;
                event_rules.push(
                    EventRule::new_unconditional_state_transition(
                        quote!(#name).to_string(),
                        quote!(#routine).to_string(),
                    )
                )
            });
    }

    // Get the conditional scheduling event rules if is_impl_as_model
    if is_impl_as_model {
        // populate events_ext
        input.items.iter()
            .filter_map(|method| {
                if let ImplItem::Method(method) = method {
                    Some(method)
                } else {
                    None
                }
            })
            .filter(|method| {
                &method.sig.ident.to_string() == "events_ext"
            })
            .for_each(|method| {
                // [0] assumes the event_ext and event_int starts with a if block
                if let Stmt::Expr(expr) = &method.block.stmts[0] {
                    if let Expr::If(if_) = expr {
                        let mut conditional_schedulings = Vec::new();
                        // Approach assumes the final else case is an error return
                        extract_ext_cases(&mut conditional_schedulings, if_);
                        event_rules.push(
                            EventRule::new_conditional_scheduling(
                                String::from("events_ext"),
                                conditional_schedulings,
                            )
                        )
                    }
                }
            });
        // populate events_int
        input.items.iter()
            .filter_map(|method| {
                if let ImplItem::Method(method) = method {
                    Some(method)
                } else {
                    None
                }
            })
            .filter(|method| {
                &method.sig.ident.to_string() == "events_int"
            })
            .for_each(|method| {
                // [0] assumes the event_ext and event_int starts with a if block
                if let Stmt::Expr(expr) = &method.block.stmts[0] {
                    if let Expr::If(if_) = expr {
                        let mut conditional_schedulings = Vec::new();
                        // Approach assumes the final else case is an error return
                        extract_int_cases(&mut conditional_schedulings, if_);
                        event_rules.push(
                            EventRule::new_conditional_scheduling(
                                String::from("events_int"),
                                conditional_schedulings,
                            )
                        )
                    }
                }
            });
    }
    println!["{:?}", serde_json::to_string(&event_rules).unwrap()];
    output
}

fn extract_ext_cases(cases: &mut Vec<ConditionalScheduling>, expr_if: &ExprIf) {
    let cond = &expr_if.cond;
    if let Stmt::Semi(expr, _) = &expr_if.then_branch.stmts[0] {
        if let Expr::Try(expr_try) = expr {
            if let Expr::MethodCall(method_call) = &*expr_try.expr {
                let method = &method_call.method;
                cases.push(
                    ConditionalScheduling{
                        follow_up_event: quote!(#method).to_string(),
                        condition: quote!(#cond).to_string(),
                    }
                )
            }
        }
    }
    let else_branch = &expr_if.else_branch;
    if let Some(else_) = else_branch {
        if let Expr::If(next_branch) = &*else_.1 {
            extract_ext_cases(cases, &next_branch)
        }
    }
}

fn extract_int_cases(cases: &mut Vec<ConditionalScheduling>, expr_if: &ExprIf) {
    let cond = &expr_if.cond;
    if let Stmt::Expr(expr) = &expr_if.then_branch.stmts[0] {
        if let Expr::MethodCall(method_call) = expr {
            let method = &method_call.method;
            cases.push(
                ConditionalScheduling{
                    follow_up_event: quote!(#method).to_string(),
                    condition: quote!(#cond).to_string(),
                }
            )
        }
    }
    let else_branch = &expr_if.else_branch;
    if let Some(else_) = else_branch {
        if let Expr::If(next_branch) = &*else_.1 {
            extract_int_cases(cases, &next_branch)
        }
    }
}