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
use defmt_parser::Level;
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, punctuated::Punctuated};

use crate::{construct, function_like::log};

use self::args::Args;

mod args;

pub(crate) fn eq(args: TokenStream) -> TokenStream {
    expand(args, BinOp::Eq)
}

pub(crate) fn ne(args: TokenStream) -> TokenStream {
    expand(args, BinOp::Ne)
}

fn expand(args: TokenStream, binop: BinOp) -> TokenStream {
    let args = parse_macro_input!(args as Args);

    let left = args.left;
    let right = args.right;

    let mut formatting_args = Punctuated::new();

    let extra_string = if let Some(log_args) = args.log_args {
        if let Some(args) = log_args.formatting_args {
            formatting_args.extend(args);
        }
        format!(": {}", log_args.format_string.value())
    } else {
        String::new()
    };

    let vals = match binop {
        BinOp::Eq => &["left_val", "right_val"][..],
        BinOp::Ne => &["left_val"][..],
    };

    for val in vals {
        formatting_args.push(construct::variable(val));
    }

    let panic_msg = match binop {
        BinOp::Eq => format!(
            "panicked at 'assertion failed: `(left == right)`{}'
 left: `{{:?}}`
right: `{{:?}}`",
            extra_string
        ),
        BinOp::Ne => format!(
            "panicked at 'assertion failed: `(left != right)`{}'
left/right: `{{:?}}`",
            extra_string
        ),
    };

    let log_args = log::Args {
        format_string: construct::string_literal(&panic_msg),
        formatting_args: Some(formatting_args),
    };
    let log_stmt = log::expand_parsed(Level::Error, log_args);

    let mut cond = quote!(*left_val == *right_val);
    if binop == BinOp::Eq {
        cond = quote!(!(#cond));
    }

    quote!(
        // evaluate arguments first
        match (&(#left), &(#right)) {
            (left_val, right_val) => {
                // following `core::assert_eq!`
                if #cond {
                    #log_stmt;
                    defmt::export::panic()
                }
            }
        }
    )
    .into()
}

#[derive(PartialEq)]
enum BinOp {
    Eq,
    Ne,
}