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
use quote::quote;

#[doc(hidden)]
#[proc_macro]
pub fn __do_check_mail_arms(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let input = syn::parse::<syn::Expr>(input).expect("Failed to parse input");

    let mut left_patterns = Vec::new();
    let mut right_patterns = Vec::new();
    let mut left_arms = Vec::new();
    let mut right_arms = Vec::new();

    if let syn::Expr::Group(group) = input {
        if let syn::Expr::Match(expr_match) = group.expr.as_ref() {
            for arm in expr_match.arms.iter() {
                let pattern = arm.pat.clone();
                let handler = arm.body.clone();

                // If pattern is an ident starting with MakeupMessage::, put it in the right arm.
                // Otherwise, put it in the left arm.

                let is_makeup_message = match &pattern {
                    syn::Pat::Ident(ref pat_ident) => {
                        pat_ident.ident.to_string().starts_with("MakeupMessage")
                    }
                    _ => false,
                };

                if is_makeup_message {
                    left_patterns.push(pattern.clone());
                    left_arms.push(handler);
                } else {
                    right_patterns.push(pattern.clone());
                    right_arms.push(handler);
                }
            }
        }
    }
    let output = quote! {
        match message {
            #(Either::Left(#left_patterns) => #left_arms)*
            #(Either::Right(#right_patterns) => #right_arms)*
            _ => {}
        }
    };

    output.into()
}