formati 0.1.3

Enhanced formatting macros with dotted notation and expression interpolation
Documentation
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use std::collections::HashMap;

use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, ToTokens as _};
use syn::{
    parse::{Parse, ParseStream},
    parse_macro_input,
    punctuated::Punctuated,
    Expr, ExprAssign, LitStr, Token,
};

/// input: `"literal"` [`,` expr ]*
struct Input {
    fmt_lit: LitStr,
    rest: Punctuated<Expr, Token![,]>,
}

impl Parse for Input {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        // the format string literal is always required
        let fmt_lit: LitStr = input.parse()?;

        // no arguments at all
        if input.is_empty() {
            return Ok(Self {
                fmt_lit,
                rest: Punctuated::new(),
            });
        }

        // if we **do** see a comma, decide whether it starts a real argument list
        // or is just a trailing comma before the right-paren / end-of-macro.
        if input.peek(Token![,]) {
            let _: Token![,] = input.parse()?; // eat the comma

            // it's a lone trailing comma
            if input.is_empty() {
                return Ok(Self {
                    fmt_lit,
                    rest: Punctuated::new(),
                });
            }

            // more input, parse the normal arg list
            let rest = Punctuated::<Expr, Token![,]>::parse_terminated(input)?;
            return Ok(Self { fmt_lit, rest });
        }

        // anything else after the literal is a syntax error; let syn report it
        Err(input.error("expected `,` or end of macro input"))
    }
}

/// Process anyhow-like error macros with dotted notation support
pub fn wrap(wrapped: TokenStream2, input: TokenStream) -> TokenStream {
    let Input { fmt_lit, rest } = parse_macro_input!(input as Input);

    let (out_lit, dot_args) = formati_args(&fmt_lit);

    let mut named = Vec::new();
    let mut positional = Vec::new();
    for expr in rest {
        match expr {
            x @ Expr::Assign(ExprAssign { .. }) => named.push(x.to_token_stream()),
            x => positional.push(x.to_token_stream()),
        }
    }

    let lit = LitStr::new(&out_lit, fmt_lit.span());

    TokenStream::from(quote! {
        ::#wrapped!(
            #lit
            #(, #named)*
            #(, #dot_args)*
            #(, #positional)*
        )
    })
}

/// Process a format string, handling dotted/tuple notations and complex expressions
pub fn formati_args(fmt_lit: &LitStr) -> (String, Vec<proc_macro2::TokenStream>) {
    let src = fmt_lit.value();
    let mut out_lit = String::with_capacity(src.len());
    let mut dot_args = Vec::<proc_macro2::TokenStream>::new();
    let mut expr_map: HashMap<String, usize> = HashMap::new();

    let bytes = src.as_bytes();
    let mut i = 0;

    while i < bytes.len() {
        match bytes[i] {
            b'{' if bytes.get(i + 1) == Some(&b'{') => {
                out_lit.push_str("{{");
                i += 2;
            }
            b'}' if bytes.get(i + 1) == Some(&b'}') => {
                out_lit.push_str("}}");
                i += 2;
            }
            b'{' => {
                // Find the matching closing brace, properly handling nested braces
                let start_inner = i + 1;
                let mut j = start_inner;
                let mut depth = 1;
                let mut in_string = false;
                let mut in_char = false;
                let mut escape_next = false;

                while j < bytes.len() && depth != 0 {
                    let ch = bytes[j] as char;

                    if escape_next {
                        escape_next = false;
                        j += 1;
                        continue;
                    }

                    match ch {
                        '\\' if in_string || in_char => {
                            escape_next = true;
                        }
                        '"' if !in_char => {
                            in_string = !in_string;
                        }
                        '\'' if !in_string => {
                            // Simple char literal detection
                            in_char = !in_char;
                        }
                        '{' if !in_string && !in_char => {
                            depth += 1;
                        }
                        '}' if !in_string && !in_char => {
                            depth -= 1;
                        }
                        _ => {}
                    }
                    j += 1;
                }

                if depth != 0 {
                    panic!("formati!: unmatched `{{` at position {}", i);
                }

                let piece = &src[start_inner..j - 1];
                i = j;

                let (head, spec) = split_head_spec(piece);

                if should_extract_expression(head) {
                    // Try to parse the expression - if it fails, treat as regular placeholder
                    match syn::parse_str::<Expr>(head) {
                        Ok(expr) => {
                            // Successfully parsed - extract it
                            let key = head.to_string();

                            let idx = match expr_map.get(&key) {
                                Some(&idx) => idx,
                                None => {
                                    let idx = dot_args.len();
                                    expr_map.insert(key, idx);
                                    dot_args.push(expr.to_token_stream());
                                    idx
                                }
                            };

                            // replace with indexed `{idx[:spec]}` placeholder
                            out_lit.push('{');
                            out_lit.push_str(&idx.to_string());
                            if !spec.is_empty() {
                                out_lit.push(':');
                                out_lit.push_str(spec);
                            }
                            out_lit.push('}');
                        }
                        Err(_) => {
                            // Failed to parse - keep as regular placeholder
                            out_lit.push('{');
                            out_lit.push_str(piece);
                            out_lit.push('}');
                        }
                    }
                } else {
                    // keep original placeholder verbatim
                    out_lit.push('{');
                    out_lit.push_str(piece);
                    out_lit.push('}');
                }
            }
            ch => {
                out_lit.push(ch as char);
                i += 1;
            }
        }
    }

    (out_lit, dot_args)
}

// split `HEAD[:SPEC]`, ignoring `::` (path separators) and handling complex expressions
fn split_head_spec(s: &str) -> (&str, &str) {
    let mut chars = s.char_indices().peekable();
    let mut paren_depth = 0;
    let mut bracket_depth = 0;
    let mut brace_depth = 0;
    let mut angle_depth = 0;
    let mut in_string = false;
    let mut in_char = false;
    let mut escape_next = false;

    while let Some((idx, c)) = chars.next() {
        if escape_next {
            escape_next = false;
            continue;
        }

        match c {
            '\\' if in_string || in_char => {
                escape_next = true;
            }
            '"' if !in_char => {
                in_string = !in_string;
            }
            '\'' if !in_string => {
                // Handle char literals and lifetimes
                if in_char {
                    in_char = false;
                } else {
                    // Check if this is a lifetime (preceded by &, comma, space, or start)
                    let is_lifetime = if idx == 0 {
                        false
                    } else {
                        match s.chars().nth(idx - 1) {
                            Some('&') | Some(',') | Some(' ') | Some('<') => {
                                // Look ahead to see if it's a lifetime
                                let rest: String = chars.clone().map(|(_, c)| c).collect();
                                rest.chars()
                                    .next()
                                    .is_some_and(|c| c.is_alphabetic() || c == '_')
                            }
                            _ => false,
                        }
                    };

                    if !is_lifetime {
                        in_char = true;
                    }
                }
            }
            _ if in_string || in_char => {
                continue;
            }
            '(' => paren_depth += 1,
            ')' => paren_depth -= 1,
            '[' => bracket_depth += 1,
            ']' => bracket_depth -= 1,
            '{' => brace_depth += 1,
            '}' => brace_depth -= 1,
            '<' => {
                // More sophisticated generic detection
                if should_count_as_generic(s, idx) {
                    angle_depth += 1;
                }
            }
            '>' => {
                if angle_depth > 0 {
                    angle_depth -= 1;
                }
            }
            ':' if paren_depth == 0
                && bracket_depth == 0
                && brace_depth == 0
                && angle_depth == 0 =>
            {
                // Check if this is part of '::'
                if let Some((_, ':')) = chars.peek() {
                    chars.next(); // consume the second ':'
                    continue;
                }
                // Found a format specifier separator
                return (&s[..idx], &s[idx + 1..]);
            }
            _ => {}
        }
    }

    (s, "")
}

fn should_count_as_generic(s: &str, idx: usize) -> bool {
    if idx == 0 {
        return false;
    }

    match s.chars().nth(idx - 1) {
        // Definitely generic contexts
        Some(c) if c.is_alphanumeric() || c == '_' => true, // identifier
        Some(':') => true,                                  // ::< or :
        Some('>') => true,                                  // nested generics Type<U>

        // Definitely comparison contexts
        Some('=') | Some('!') | Some('<') => false, // ==<, !=<, <<, etc.

        // Whitespace - need deeper analysis
        Some(' ') | Some('\t') | Some('\n') => is_likely_generic_with_space(s, idx),

        // Default to comparison for safety
        _ => false,
    }
}

fn is_likely_generic_with_space(s: &str, idx: usize) -> bool {
    // Look backward to find the last non-whitespace token
    let before_whitespace = s[..idx].trim_end();

    if before_whitespace.is_empty() {
        return false;
    }

    // Check if it ends with something that could take generics
    let last_token = before_whitespace.split_whitespace().last().unwrap_or("");

    // Pattern matching for likely generic contexts
    last_token
        .chars()
        .all(|c| c.is_alphanumeric() || c == '_' || c == ':')
        && (last_token.contains("::") ||
    last_token.chars().next().is_some_and(|c| c.is_uppercase()) || // PascalCase
    last_token.ends_with("_t") || // C-style type suffixes
    last_token.len() > 1) // Avoid single letters which are usually variables
}

fn should_extract_expression(head: &str) -> bool {
    // Don't extract if it's just a simple identifier or number
    if head.chars().all(|c| c.is_alphanumeric() || c == '_') {
        return false;
    }

    // Don't extract simple literals
    if head.parse::<i64>().is_ok() || head.parse::<f64>().is_ok() {
        return false;
    }

    // Extract if it contains dots, method calls, array indexing, references, or complex expressions
    head.contains('.')
       || head.contains("::")
       || head.contains('(')
       || head.contains('[')
       || head.contains('<')
       || head.starts_with('&')       // Reference expressions
       || head.starts_with("mut ")    // Mutable references
       || head.starts_with("&mut ")   // &mut expressions
       || head.starts_with('*')       // Dereference expressions
       || head.contains(" as ")       // Type casting
       || head.contains('?')          // Try operator
       || head.contains("..")         // Range expressions
       || head.contains('{')          // Struct literals, closures, blocks
       || head.contains(" if ")       // if expressions
       || head.contains(" match ")    // match expressions
       || is_complex_expression(head) // More sophisticated detection
}

fn is_complex_expression(head: &str) -> bool {
    // Check for operators that indicate complex expressions
    let operators = [
        "+", "-", "*", "/", "%", // Arithmetic
        "==", "!=", "<", ">", "<=", ">=", // Comparison
        "&&", "||", "!", // Logical
        "&", "|", "^", "<<", ">>", // Bitwise
        "=", "+=", "-=", "*=", "/=", // Assignment
    ];

    // Check for operators (but be careful about false positives in strings)
    let mut in_string = false;
    let mut escape_next = false;
    let chars: Vec<char> = head.chars().collect();

    for i in 0..chars.len() {
        let c = chars[i];

        if escape_next {
            escape_next = false;
            continue;
        }

        match c {
            '\\' if in_string => {
                escape_next = true;
            }
            '"' => {
                in_string = !in_string;
            }
            _ if in_string => {
                continue;
            }
            _ => {
                // Check if any operator starts at this position
                let remaining = &head[i..];
                for op in &operators {
                    if remaining.starts_with(op) {
                        // Make sure it's not part of a larger token
                        let after_op = i + op.len();
                        let before_ok = i == 0 || !chars[i - 1].is_alphanumeric();
                        let after_ok =
                            after_op >= chars.len() || !chars[after_op].is_alphanumeric();

                        if before_ok && after_ok {
                            return true;
                        }
                    }
                }
            }
        }
    }

    false
}