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
// Deny lints.
// Warn lints.
use TokenStream;
use ToTokens as _;
use parse_macro_input;
/// Convenience macro for writing grouped `match` arms for different underlying types.
///
/// Writing repetitive `match` arms for enumerations (or other pattern-matching
/// constructs) — especially when types, but not the API, differ —
/// can quickly become boilerplate. `delegate_match!` lets you list
/// several patterns up-front once and then re-uses a single body for each
/// of them, automatically expanding into equivalent ordinary Rust code.
///
/// ## Syntax outline
///
/// ```text
/// match <scrutinee_expr> {
/// [<arm_path>::]{ <entry_pat> [: <assoc_ts>][, ...] } [<arm_pat>] [if <guard_expr>] => <body_expr>[[,] ...]
/// }
/// ```
///
/// - `arm_path` — optional path prefix (e.g. `MyEnum` or `::std::io`)
/// - `entry_pat` — individual *entry pattern*, also available as the `$entry_pat` placeholder.
/// - `assoc_ts` — *associated syntax item*, also available as the `$assoc_ts` placeholder.
/// - `arm_pat` — an optional pattern appended to every entry.
/// - `guard_expr` — an optional `if` guard.
/// - `body_expr` — expression generated for each entry.
///
/// This expands to:
///
/// ```text
/// match <scrutinee_expr> {
/// <arm_path>::<entry_pat><arm_pat> if <guard_expr> => <body_expr>
/// }
/// ```
///
/// Two placeholders are substituted for every entry *before the code is
/// type-checked*, and they may appear in the following places:
/// - inside the delegate arm pattern `arm_pat` (if present),
/// - inside the match arm guard expression `guard_expr` (if present),
/// - inside the arm body expression `body_expr`.
///
/// The available placeholders are:
/// - `$entry_pat` — the entry pattern for a generated arm.
/// - `$assoc_ts` — the tokens following an entry, up until the next one (excluding the colon).
/// Can be any of the following:
/// - Expression
/// - Statement
/// - Pattern
/// - Type
/// The first that matches is used, in the above order.
///
/// The macro is supposed to accept standard Rust `match` expression syntax, extended with the above.
/// Any other deviation should generally be considered a bug.
///
/// ## Semantics
///
/// - For each *entry* in a grouped arm, the macro generates a regular match arm.
/// - Everything else (outer attributes, guards, commas...) is preserved exactly as you write it.
/// - The only exception to that is placeholder substitution.
/// - This macro performs generation before type-checking is done, so
/// the generated code is capable of working with different types, if constructed appropriately.
/// - The order of generated arms is the order of entries in the source code.
///
/// ## Examples
///
/// ### Delegating to the same code for multiple enum variants
///
/// ```rust
/// use delegate_match::delegate_match;
///
/// enum MouseEvent { Scroll(i16, i16), Position(i32, i32) }
/// let ev = MouseEvent::Scroll(10, 20);
///
/// delegate_match! {
/// match ev {
/// // This expands to two individual arms.
/// MouseEvent::{ Scroll, Position }(x, y) => {
/// println!("mouse event: $entry_pat -> ({x}, {y})")
/// }
/// }
/// }
/// ```
///
/// ### Using placeholders
///
/// ```rust
/// # use delegate_match::delegate_match;
/// # enum Msg { Ping, Log }
/// # let msg = Msg::Log;
/// delegate_match! {
/// match msg {
/// // `$assoc_ts` and `$entry_pat` are substituted at compile time.
/// Msg::{ Ping: "🏓", Log: "📝" } => {
/// // Outputs "🏓 Ping" or "📝 Log" depending on the variant.
/// println!("{} {}", $assoc_ts, stringify!($entry_pat))
/// }
/// }
/// }
/// ```
///
/// ### Adding an if guard to multiple entries
///
/// ```rust
/// # use delegate_match::delegate_match;
/// # enum Number { I32(i32), I64(i64) }
/// # let n = Number::I32(4);
/// delegate_match! {
/// match n {
/// // This works despite `val` being of different types for each variant.
/// // This is because a separate arm is generated for each entry!
/// Number::{ I32, I64 }(val) if val % 2 == 0 => {
/// println!("even {}", val)
/// }
/// // We must also account for the rest of the cases.
/// Number::{ I32, I64 }(val) => {
/// println!("odd {}", val)
/// }
/// }
/// }
/// ```