day-macros 0.7.0

Compile-time macros for Day.
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// Copyright © The Daybrite Project
// SPDX-License-Identifier: MPL-2.0

//! `#[builder(TraitName)]`: a piece's builder trait, generated from its inherent impl
//! (docs/api-style.md "Typed builders and erasure").
//!
//! The attribute goes on the inherent `impl` block that holds a piece's builder methods. The
//! block is emitted unchanged, followed by three generated items:
//!
//! - `pub trait TraitName: Sized`, declaring every `pub fn` of the block that takes `self` by
//!   value and returns `Self`;
//! - `impl TraitName for <the piece>`, forwarding each method to the inherent one;
//! - `impl<Inner: TraitName + Piece> TraitName for Decorated<Inner>`, forwarding through
//!   `Decorated::map_inner`, which is what lets `label(…).padding(8.0).font(…)` resolve.
//!
//! A trait that carries one of the impl's type parameters names it in the attribute
//! (`#[builder(NavBuilder<K>)]`); the parameter's bounds are copied from the impl's generic list.
//! `Decorated` and `Piece` must be in scope where the attribute is used, as they are under
//! `use day::prelude::*`.
//!
//! Methods that take `&self`, `&mut self` or no receiver, return anything other than `Self`,
//! or are not `pub` are left out: they are not builder steps. `#[doc]`, `#[cfg]` and `#[allow]`
//! attributes are carried onto the generated declarations.
//!
//! Same construction as the crate's other macros: a token walk, with neither syn nor quote.

use proc_macro::{Delimiter, Spacing, TokenStream, TokenTree};

/// One builder method of the impl block, as the pieces the generated items need.
struct Method {
    /// `#[doc]`, `#[cfg]` and `#[allow]` attributes, verbatim.
    attrs: String,
    name: String,
    /// The method's own generic parameter list, `<…>` included, or empty.
    generics: String,
    /// The parameters after `self`, each as `name: Type`, verbatim.
    params: Vec<String>,
    /// The parameter names, in order.
    args: Vec<String>,
    /// The method's `where` clause, `where` included, or empty.
    where_clause: String,
}

/// The attribute's argument: the trait name and the impl type parameters it carries.
struct TraitSpec {
    name: String,
    params: Vec<String>,
}

pub fn builder(attr: TokenStream, item: TokenStream) -> TokenStream {
    match expand(attr, item.clone()) {
        Ok(generated) => {
            let mut out = item;
            out.extend(generated);
            out
        }
        Err(message) => {
            let mut out = item;
            out.extend(crate::compile_error(&format!("#[builder]: {message}")));
            out
        }
    }
}

fn expand(attr: TokenStream, item: TokenStream) -> Result<TokenStream, String> {
    let spec = parse_attr(attr)?;
    let tokens: Vec<TokenTree> = item.into_iter().collect();
    let mut i = 0;
    // Outer attributes and visibility before `impl` are allowed and skipped.
    while i < tokens.len() {
        match &tokens[i] {
            TokenTree::Punct(p) if p.as_char() == '#' => i += 2,
            TokenTree::Ident(id) if id.to_string() == "impl" => break,
            _ => i += 1,
        }
    }
    if i >= tokens.len() {
        return Err("expected an inherent `impl` block".into());
    }
    i += 1;
    // The impl's generic parameters.
    let mut impl_generics: Vec<TokenTree> = Vec::new();
    if matches!(&tokens.get(i), Some(TokenTree::Punct(p)) if p.as_char() == '<') {
        let end = matching_angle(&tokens, i)?;
        impl_generics = tokens[i + 1..end].to_vec();
        i = end + 1;
    }
    // The self type, up to `where` or the body.
    let mut self_ty: Vec<TokenTree> = Vec::new();
    while i < tokens.len() {
        match &tokens[i] {
            TokenTree::Group(g) if g.delimiter() == Delimiter::Brace => break,
            TokenTree::Ident(id) if id.to_string() == "where" => break,
            t => self_ty.push(t.clone()),
        }
        i += 1;
    }
    if self_ty
        .iter()
        .any(|t| matches!(t, TokenTree::Ident(id) if id.to_string() == "for"))
    {
        return Err("the attribute goes on an inherent impl, not a trait impl".into());
    }
    let mut impl_where: Vec<TokenTree> = Vec::new();
    while i < tokens.len() {
        match &tokens[i] {
            TokenTree::Group(g) if g.delimiter() == Delimiter::Brace => break,
            t => impl_where.push(t.clone()),
        }
        i += 1;
    }
    let Some(TokenTree::Group(body)) = tokens.get(i) else {
        return Err("expected the impl block's body".into());
    };
    let methods = parse_methods(body.stream())?;
    if methods.is_empty() {
        return Err("the impl block has no `pub fn …(self, …) -> Self` builder methods".into());
    }

    // The trait's own generic parameters, with the bounds the impl gives them.
    let impl_params = split_top_level(&impl_generics, ',');
    let mut trait_params_bounded: Vec<String> = Vec::new();
    for p in &spec.params {
        let found = impl_params.iter().find(
            |group| matches!(group.first(), Some(TokenTree::Ident(id)) if id.to_string() == *p),
        );
        match found {
            Some(group) => trait_params_bounded.push(render(group)),
            None => {
                return Err(format!(
                    "`{p}` is named in the attribute but is not a type parameter of the impl"
                ));
            }
        }
    }
    let trait_generics_decl = if trait_params_bounded.is_empty() {
        String::new()
    } else {
        format!("<{}>", trait_params_bounded.join(", "))
    };
    let trait_generics_use = if spec.params.is_empty() {
        String::new()
    } else {
        format!("<{}>", spec.params.join(", "))
    };
    let trait_path = format!("{}{}", spec.name, trait_generics_use);
    let impl_generics_src = if impl_generics.is_empty() {
        String::new()
    } else {
        format!("<{}>", render(&impl_generics))
    };
    let self_ty_src = render(&self_ty);
    let impl_where_src = render(&impl_where);

    let mut out = String::new();
    // The trait.
    out.push_str(&format!(
        "#[doc = \"Builder methods of [`{}`], reachable through a [`Decorated`] (docs/api-style.md \\\"Typed builders and erasure\\\"): generic modifiers and typed ones chain in any order.\"]\n",
        first_ident(&self_ty)
    ));
    out.push_str(&format!(
        "pub trait {}{}: Sized {{\n",
        spec.name, trait_generics_decl
    ));
    for m in &methods {
        out.push_str(&format!(
            "{}fn {}{}(self{}) -> Self {};\n",
            m.attrs,
            m.name,
            m.generics,
            params_src(&m.params),
            m.where_clause
        ));
    }
    out.push_str("}\n");
    // The impl for the piece: each method forwards to the inherent one. `Self::name` resolves
    // to the inherent method, since inherent items take precedence over trait items.
    out.push_str(&format!(
        "impl{} {} for {} {} {{\n",
        impl_generics_src, trait_path, self_ty_src, impl_where_src
    ));
    for m in &methods {
        out.push_str(&format!(
            "{}fn {}{}(self{}) -> Self {} {{ Self::{}(self{}) }}\n",
            cfg_only(&m.attrs),
            m.name,
            m.generics,
            params_src(&m.params),
            m.where_clause,
            m.name,
            args_src(&m.args)
        ));
    }
    out.push_str("}\n");
    // The impl for `Decorated`: through `map_inner`, so the modifier chain stays flat.
    let decorated_generics = {
        let mut g = trait_params_bounded.clone();
        g.push(format!("DecoratedInner: {trait_path} + Piece"));
        format!("<{}>", g.join(", "))
    };
    out.push_str(&format!(
        "impl{} {} for Decorated<DecoratedInner> {{\n",
        decorated_generics, trait_path
    ));
    for m in &methods {
        out.push_str(&format!(
            "{}fn {}{}(self{}) -> Self {} {{ self.map_inner(|inner| inner.{}({})) }}\n",
            cfg_only(&m.attrs),
            m.name,
            m.generics,
            params_src(&m.params),
            m.where_clause,
            m.name,
            m.args.join(", ")
        ));
    }
    out.push_str("}\n");

    out.parse()
        .map_err(|e| format!("generated code did not parse: {e}\n{out}"))
}

fn parse_attr(attr: TokenStream) -> Result<TraitSpec, String> {
    let tokens: Vec<TokenTree> = attr.into_iter().collect();
    let Some(TokenTree::Ident(name)) = tokens.first() else {
        return Err("expected the builder trait's name, e.g. `#[builder(LabelBuilder)]`".into());
    };
    let mut params = Vec::new();
    if tokens.len() > 1 {
        if !matches!(&tokens[1], TokenTree::Punct(p) if p.as_char() == '<') {
            return Err("expected `TraitName` or `TraitName<K, …>`".into());
        }
        let end = matching_angle(&tokens, 1)?;
        for group in split_top_level(&tokens[2..end], ',') {
            match group.as_slice() {
                [TokenTree::Ident(id)] => params.push(id.to_string()),
                _ => return Err("trait parameters must be bare type parameter names".into()),
            }
        }
        if end + 1 != tokens.len() {
            return Err("unexpected tokens after the trait name".into());
        }
    }
    Ok(TraitSpec {
        name: name.to_string(),
        params,
    })
}

fn parse_methods(body: TokenStream) -> Result<Vec<Method>, String> {
    let tokens: Vec<TokenTree> = body.into_iter().collect();
    let mut methods = Vec::new();
    let mut i = 0;
    while i < tokens.len() {
        // Attributes.
        let mut attrs = String::new();
        while let (Some(TokenTree::Punct(p)), Some(TokenTree::Group(g))) =
            (tokens.get(i), tokens.get(i + 1))
            && p.as_char() == '#'
            && g.delimiter() == Delimiter::Bracket
        {
            let first = g.stream().into_iter().next();
            if let Some(TokenTree::Ident(id)) = first
                && matches!(
                    id.to_string().as_str(),
                    "doc" | "cfg" | "allow" | "cfg_attr"
                )
            {
                attrs.push_str(&format!("#[{}]\n", g.stream()));
            }
            i += 2;
        }
        // Visibility.
        let mut is_pub = false;
        if matches!(tokens.get(i), Some(TokenTree::Ident(id)) if id.to_string() == "pub") {
            i += 1;
            if matches!(tokens.get(i), Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Parenthesis)
            {
                // `pub(crate)` and friends: not part of the public builder surface.
                i += 1;
            } else {
                is_pub = true;
            }
        }
        // Qualifiers before `fn` (`const`, `async`, `unsafe`) disqualify a method; `const`
        // and `type` items are skipped to their `;`.
        let Some(TokenTree::Ident(kw)) = tokens.get(i) else {
            i += 1;
            continue;
        };
        match kw.to_string().as_str() {
            "fn" => {}
            "const" | "type" | "static" => {
                while i < tokens.len()
                    && !matches!(&tokens[i], TokenTree::Punct(p) if p.as_char() == ';')
                {
                    i += 1;
                }
                i += 1;
                continue;
            }
            _ => {
                // `async fn`, `unsafe fn`, macro invocations, …: skip to the end of the item.
                i = skip_item(&tokens, i);
                continue;
            }
        }
        i += 1;
        let Some(TokenTree::Ident(name)) = tokens.get(i) else {
            return Err("expected a method name after `fn`".into());
        };
        let name = name.to_string();
        i += 1;
        let mut generics = String::new();
        if matches!(tokens.get(i), Some(TokenTree::Punct(p)) if p.as_char() == '<') {
            let end = matching_angle(&tokens, i)?;
            generics = format!("<{}>", render(&tokens[i + 1..end]));
            i = end + 1;
        }
        let Some(TokenTree::Group(params)) = tokens.get(i) else {
            return Err(format!("expected `{name}`'s parameter list"));
        };
        if params.delimiter() != Delimiter::Parenthesis {
            return Err(format!("expected `{name}`'s parameter list"));
        }
        let param_tokens: Vec<TokenTree> = params.stream().into_iter().collect();
        i += 1;
        // The return type: tokens between `->` and `where` or the body.
        let mut ret: Vec<TokenTree> = Vec::new();
        let mut where_tokens: Vec<TokenTree> = Vec::new();
        let mut has_arrow = false;
        if let (Some(TokenTree::Punct(a)), Some(TokenTree::Punct(b))) =
            (tokens.get(i), tokens.get(i + 1))
            && a.as_char() == '-'
            && b.as_char() == '>'
        {
            has_arrow = true;
            i += 2;
            while i < tokens.len() {
                match &tokens[i] {
                    TokenTree::Group(g) if g.delimiter() == Delimiter::Brace => break,
                    TokenTree::Ident(id) if id.to_string() == "where" => break,
                    t => ret.push(t.clone()),
                }
                i += 1;
            }
        }
        while i < tokens.len() {
            match &tokens[i] {
                TokenTree::Group(g) if g.delimiter() == Delimiter::Brace => break,
                TokenTree::Punct(p) if p.as_char() == ';' => break,
                t => where_tokens.push(t.clone()),
            }
            i += 1;
        }
        // The body (or `;` for a declaration, which an inherent impl never has).
        i += 1;

        let Some((params, args)) = split_params(&param_tokens) else {
            continue; // no by-value `self` receiver: not a builder step
        };
        let returns_self = has_arrow
            && ret.len() == 1
            && matches!(&ret[0], TokenTree::Ident(id) if id.to_string() == "Self");
        if !is_pub || !returns_self {
            continue;
        }
        methods.push(Method {
            attrs,
            name,
            generics,
            params,
            args,
            where_clause: render(&where_tokens),
        });
    }
    Ok(methods)
}

/// Skip a non-`fn` item starting at `i`: to the end of its brace group or its `;`.
fn skip_item(tokens: &[TokenTree], mut i: usize) -> usize {
    while i < tokens.len() {
        match &tokens[i] {
            TokenTree::Group(g) if g.delimiter() == Delimiter::Brace => return i + 1,
            TokenTree::Punct(p) if p.as_char() == ';' => return i + 1,
            _ => i += 1,
        }
    }
    i
}

/// Split a parameter list into the parameters after a by-value `self` (`name: Type`, verbatim)
/// and their names. `None` when the first parameter is not `self` or `mut self`.
fn split_params(tokens: &[TokenTree]) -> Option<(Vec<String>, Vec<String>)> {
    let groups = split_top_level(tokens, ',');
    let receiver = groups.first()?;
    let is_self = match receiver.as_slice() {
        [TokenTree::Ident(s)] => s.to_string() == "self",
        [TokenTree::Ident(m), TokenTree::Ident(s)] => {
            m.to_string() == "mut" && s.to_string() == "self"
        }
        _ => false,
    };
    if !is_self {
        return None;
    }
    let mut params = Vec::new();
    let mut args = Vec::new();
    for group in &groups[1..] {
        if group.is_empty() {
            continue; // trailing comma
        }
        let colon = group
            .iter()
            .position(|t| matches!(t, TokenTree::Punct(p) if p.as_char() == ':'))?;
        let name = match &group[..colon] {
            [TokenTree::Ident(n)] => n.to_string(),
            [TokenTree::Ident(m), TokenTree::Ident(n)] if m.to_string() == "mut" => n.to_string(),
            _ => return None, // a pattern parameter: not forwardable by name
        };
        params.push(format!("{}: {}", name, render(&group[colon + 1..])));
        args.push(name);
    }
    Some((params, args))
}

/// Index of the `>` closing the `<` at `open`, with `->` and nested angles accounted for.
fn matching_angle(tokens: &[TokenTree], open: usize) -> Result<usize, String> {
    let mut depth = 0usize;
    let mut i = open;
    while i < tokens.len() {
        if let TokenTree::Punct(p) = &tokens[i] {
            match p.as_char() {
                '<' => depth += 1,
                '>' => {
                    // The `>` of `->` is not a closing angle.
                    let arrow = i > 0
                        && matches!(&tokens[i - 1], TokenTree::Punct(q)
                            if q.as_char() == '-' && q.spacing() == Spacing::Joint);
                    if !arrow {
                        depth -= 1;
                        if depth == 0 {
                            return Ok(i);
                        }
                    }
                }
                _ => {}
            }
        }
        i += 1;
    }
    Err("unbalanced `<` in a generic parameter list".into())
}

/// Split at every `sep` punct that sits outside angle brackets (groups are already single
/// tokens, so parentheses and braces need no tracking).
fn split_top_level(tokens: &[TokenTree], sep: char) -> Vec<Vec<TokenTree>> {
    let mut out = vec![Vec::new()];
    let mut depth = 0usize;
    for (i, t) in tokens.iter().enumerate() {
        if let TokenTree::Punct(p) = t {
            match p.as_char() {
                '<' => depth += 1,
                '>' => {
                    let arrow = i > 0
                        && matches!(&tokens[i - 1], TokenTree::Punct(q)
                            if q.as_char() == '-' && q.spacing() == Spacing::Joint);
                    if !arrow {
                        depth = depth.saturating_sub(1);
                    }
                }
                c if c == sep && depth == 0 => {
                    out.push(Vec::new());
                    continue;
                }
                _ => {}
            }
        }
        if let Some(last) = out.last_mut() {
            last.push(t.clone());
        }
    }
    out
}

fn render(tokens: &[TokenTree]) -> String {
    tokens.iter().cloned().collect::<TokenStream>().to_string()
}

fn first_ident(tokens: &[TokenTree]) -> String {
    tokens
        .iter()
        .find_map(|t| match t {
            TokenTree::Ident(id) => Some(id.to_string()),
            _ => None,
        })
        .unwrap_or_default()
}

fn params_src(params: &[String]) -> String {
    params.iter().map(|p| format!(", {p}")).collect()
}

fn args_src(args: &[String]) -> String {
    args.iter().map(|a| format!(", {a}")).collect()
}

/// Only the `#[cfg]` attributes of a method, for the generated impls (docs stay on the trait).
fn cfg_only(attrs: &str) -> String {
    attrs
        .lines()
        .filter(|l| l.starts_with("#[cfg"))
        .map(|l| format!("{l}\n"))
        .collect()
}