structio-derive 0.8.0

The optional #[derive(Structio)] for structio. Enable structio's `derive` feature rather than depending on this crate directly.
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
//! From the shape of the type to the declaration macro that describes it.
//!
//! Every token that came from the user goes out under its own span, so a type
//! the derive repeats back, the adapter of a `with = ".."` among them, is
//! reported where it was written. Tokens the derive adds, the macro's name and
//! the bounds it appends, carry the type's name instead.
//!
//! That is also where an error out of the expansion lands. When a field's type
//! has no `Read` impl, one macro call covers every field, so the call site the
//! type checker has to point at is the declaration as a whole rather than the
//! field that caused it.

use proc_macro::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};

use crate::attr::{self, Format};
use crate::parse::{Field, FieldName, Input, Param, Payload, Shape};
use crate::{Error, Result};

pub(crate) fn expand(input: &Input) -> Result<TokenStream> {
    let is_enum = matches!(input.shape, Shape::Enum(_));
    let container = attr::container(&input.attrs, is_enum)?;
    let at = input.name.span();

    let mut out = Out::new(at);
    let root = root_path(container.krate.as_ref(), at)?;

    let (macro_name, header_case, header_tag, body) = match &input.shape {
        Shape::Struct(fields) if let Some(marker) = container.transparent => (
            per_format(container.format, "transparent"),
            None,
            None,
            transparent_body(fields, marker, at)?,
        ),
        Shape::Struct(fields) if container.array.is_some() => (
            per_format(container.format, "array"),
            None,
            None,
            array_body(fields, container.element.as_ref(), at)?,
        ),
        Shape::Struct(fields) => (
            per_format(container.format, "object"),
            container.rename_all.clone(),
            None,
            object_body(fields, container.write_only.is_some(), at)?,
        ),
        Shape::Enum(variants) => {
            if variants.is_empty() {
                return Err(Error::new(
                    at,
                    "an enum with no variants has no value to write",
                ));
            }
            let all_unit = variants.iter().all(|v| matches!(v.payload, Payload::Unit));
            // `unit_enum!` has no one-format form. Its JSON half is the
            // tagged macro's, and its BEVE half differs only in packing a run
            // of the enum as a string array, which the tagged form reads
            // back all the same, so a narrowed unit enum is a tagged one.
            let name = if all_unit && container.tag.is_none() && container.format == Format::Both {
                "unit_enum"
            } else {
                per_format(container.format, "tagged_enum")
            };
            (
                name,
                container.rename_all.clone(),
                container.tag.clone(),
                enum_body(variants, container.write_only.is_some(), at)?,
            )
        }
    };

    out.extend(root.clone());
    out.path_sep();
    out.ident(macro_name);
    out.punct('!', Spacing::Alone);

    let mut call = Out::new(at);
    if container.write_only.is_some() {
        call.ident("write_only");
    }
    if !input.generics.is_empty() {
        call.group(
            Delimiter::Bracket,
            impl_generics(
                &input.generics,
                &root,
                container.format,
                container.write_only.is_some(),
                at,
            ),
        );
    }
    call.extend(type_tokens(input));
    if header_case.is_some() || header_tag.is_some() {
        call.ident("as");
        if let Some(case) = header_case {
            call.lit(case);
        }
        if let Some(tag) = header_tag {
            call.ident("tag");
            call.lit(tag);
        }
    }
    call.extend(body);

    out.group(Delimiter::Parenthesis, call.tokens);
    out.punct(';', Spacing::Alone);
    Ok(out.tokens.into_iter().collect())
}

/// `::structio`, or the path `crate = ".."` gave.
fn root_path(krate: Option<&Literal>, at: Span) -> Result<Vec<TokenTree>> {
    match krate {
        Some(lit) => {
            let text = attr::string_content(lit)?;
            let stream: TokenStream = text.parse().map_err(|_| {
                Error::new(lit.span(), "`crate` takes a path, such as `my_structio`")
            })?;
            let tokens: Vec<TokenTree> = stream.into_iter().collect();
            if tokens.is_empty() {
                return Err(Error::new(
                    lit.span(),
                    "`crate` takes a path, such as `my_structio`",
                ));
            }
            Ok(tokens)
        }
        None => {
            let mut out = Out::new(at);
            out.path_sep();
            out.ident("structio");
            Ok(out.tokens)
        }
    }
}

fn per_format(format: Format, base: &'static str) -> &'static str {
    match (format, base) {
        (Format::Both, b) => b,
        (Format::Json, "object") => "json_object",
        (Format::Json, "array") => "json_array",
        (Format::Json, "transparent") => "json_transparent",
        (Format::Json, _) => "json_tagged_enum",
        (Format::Beve, "object") => "beve_object",
        (Format::Beve, "array") => "beve_array",
        (Format::Beve, "transparent") => "beve_transparent",
        (Format::Beve, _) => "beve_tagged_enum",
    }
}

/// The bracketed impl generics: each parameter as declared, with the bound the
/// impls need through it appended to every type parameter.
///
/// That is the format's read-and-write bound, and `Default`, because a read
/// constructs a value of the parameter to fill. A `write_only` declaration
/// generates no read, so it appends the write bound alone and no `Default`:
/// the two halves of the burden go together, and narrowing the direction is
/// what lifts them.
fn impl_generics(
    params: &[Param],
    root: &[TokenTree],
    format: Format,
    write_only: bool,
    at: Span,
) -> Vec<TokenTree> {
    let mut out = Out::new(at);
    for (i, param) in params.iter().enumerate() {
        if i > 0 {
            out.punct(',', Spacing::Alone);
        }
        match param {
            Param::Lifetime { name, bounds } => {
                out.extend(name.iter().cloned());
                if !bounds.is_empty() {
                    out.punct(':', Spacing::Alone);
                    out.extend(bounds.iter().cloned());
                }
            }
            Param::Const { decl, .. } => out.extend(decl.iter().cloned()),
            Param::Type { name, bounds } => {
                out.tokens.push(TokenTree::Ident(name.clone()));
                out.punct(':', Spacing::Alone);
                if !bounds.is_empty() {
                    out.extend(bounds.iter().cloned());
                    out.punct('+', Spacing::Alone);
                }
                out.extend(root.iter().cloned());
                out.path_sep();
                match format {
                    Format::Both => {}
                    Format::Json => {
                        out.ident("json");
                        out.path_sep();
                    }
                    Format::Beve => {
                        out.ident("beve");
                        out.path_sep();
                    }
                }
                if write_only {
                    out.ident("Write");
                } else {
                    out.ident("ReadWrite");
                    out.punct('+', Spacing::Alone);
                    out.path_sep();
                    out.ident("core");
                    out.path_sep();
                    out.ident("default");
                    out.path_sep();
                    out.ident("Default");
                }
            }
        }
    }
    out.tokens
}

/// `Name<'a, T, N>`, or `Name` alone.
fn type_tokens(input: &Input) -> Vec<TokenTree> {
    let mut out = Out::new(input.name.span());
    out.tokens.push(TokenTree::Ident(input.name.clone()));
    if input.generics.is_empty() {
        return out.tokens;
    }
    out.punct('<', Spacing::Alone);
    for (i, param) in input.generics.iter().enumerate() {
        if i > 0 {
            out.punct(',', Spacing::Alone);
        }
        match param {
            Param::Lifetime { name, .. } => out.extend(name.iter().cloned()),
            Param::Type { name, .. } | Param::Const { name, .. } => {
                out.tokens.push(TokenTree::Ident(name.clone()));
            }
        }
    }
    out.punct('>', Spacing::Alone);
    out.tokens
}

/// `{ #[required] "key" => field as With, .., }`
fn object_body(fields: &[Field], write_only: bool, at: Span) -> Result<Vec<TokenTree>> {
    if let Some(Field {
        name: FieldName::Index(index),
        ..
    }) = fields.first()
    {
        return Err(Error::new(index.span(), no_keys(fields.len())));
    }

    let mut body = Out::new(at);
    let mut skipped = false;
    for field in fields {
        let opts = attr::field(&field.attrs, false)?;
        if opts.skip.is_some() {
            skipped = true;
            continue;
        }
        if let Some(required) = opts.required {
            if write_only {
                return Err(Error::new(
                    required,
                    "`required` is a rule about reading: a document that \
                     leaves this member out is `MissingKey`. A type declared \
                     `write_only` is never read, so there is nothing to \
                     require",
                ));
            }
            body.punct('#', Spacing::Alone);
            let mut marker = Out::new(required);
            marker.ident("required");
            body.group(Delimiter::Bracket, marker.tokens);
        }
        if !opts.aliases.is_empty() && write_only {
            return Err(Error::new(
                opts.aliases[0].span(),
                "an alias is a rule about reading: it is a further key a \
                 document may use for this field, and the key the field is \
                 written under is the declared one. A type declared \
                 `write_only` is never read, so nothing would ever look this \
                 one up",
            ));
        }
        if let Some(key) = opts.rename {
            body.lit(key);
            body.punct('=', Spacing::Joint);
            body.punct('>', Spacing::Alone);
        }
        body.tokens.push(field.name.token());
        if let Some(with) = opts.with {
            body.ident("as");
            body.extend(adapter(&with)?);
        }
        for alias in opts.aliases {
            body.punct('|', Spacing::Alone);
            body.lit(alias);
        }
        body.punct(',', Spacing::Alone);
    }
    if skipped {
        body.rest();
    }
    let mut out = Out::new(at);
    out.group(Delimiter::Brace, body.tokens);
    Ok(out.tokens)
}

/// `{ field }`, or `{ field as With }`
///
/// The one field is the whole declaration: a transparent struct is written as
/// that field, so there is no key to rename or alias, no absence to require
/// and nothing left over to skip. `with` is the one field attribute that
/// survives, and it earns its place: a newtype around a type from another
/// crate is much of what a transparent declaration wraps, and the adapter is
/// how that type is described at all.
fn transparent_body(fields: &[Field], marker: Span, at: Span) -> Result<Vec<TokenTree>> {
    let [field] = fields else {
        return Err(Error::new(
            marker,
            format!(
                "`transparent` writes a struct as the one field it holds, and \
                 this one has {}. Declare it as an object, or positionally \
                 with `#[structio(array)]`",
                fields.len()
            ),
        ));
    };
    let opts = attr::field(&field.attrs, false)?;
    let other = [
        opts.rename.as_ref().map(|lit| ("rename", lit.span())),
        opts.aliases.first().map(|lit| ("alias", lit.span())),
        opts.required.map(|at| ("required", at)),
        opts.skip.map(|at| ("skip", at)),
    ]
    .into_iter()
    .flatten()
    .next();
    if let Some((name, span)) = other {
        return Err(Error::new(
            span,
            format!(
                "a transparent struct is written as this field and nothing \
                 else: it has no key, it is never a member that could be \
                 absent, and skipping it would leave nothing to write. \
                 `{name}` has nothing to apply to"
            ),
        ));
    }

    let mut body = Out::new(at);
    body.tokens.push(field.name.token());
    if let Some(with) = opts.with {
        body.ident("as");
        body.extend(adapter(&with)?);
    }
    let mut out = Out::new(at);
    out.group(Delimiter::Brace, body.tokens);
    Ok(out.tokens)
}

/// `[ Elem ; a, b, c, .. ]`
fn array_body(fields: &[Field], element: Option<&Literal>, at: Span) -> Result<Vec<TokenTree>> {
    let mut body = Out::new(at);
    if let Some(element) = element {
        body.extend(adapter(element)?);
        body.punct(';', Spacing::Alone);
    }
    let mut skipped = false;
    for field in fields {
        let opts = attr::field(&field.attrs, true)?;
        if opts.skip.is_some() {
            skipped = true;
            continue;
        }
        body.tokens.push(field.name.token());
        body.punct(',', Spacing::Alone);
    }
    if skipped {
        body.rest();
    }
    let mut out = Out::new(at);
    out.group(Delimiter::Bracket, body.tokens);
    Ok(out.tokens)
}

/// What to say to a tuple struct declared as an object.
///
/// An object is not one of its options: its fields have no names, so there is
/// nothing for the keys to be. The positional declaration is, and it wants no
/// names, which is why the derive takes a tuple struct there and only there.
/// A one-field struct is the exception worth naming separately: what its
/// author almost always wants is the field's own value, not a one-element
/// array around it.
fn no_keys(count: usize) -> &'static str {
    if count == 1 {
        "a tuple struct has no field names to be keys. `#[structio(array)]` \
         declares it positional, which writes this field as a one-element \
         array; `#[structio(transparent)]` writes it as the field's own value"
    } else {
        "a tuple struct has no field names to be keys. Declare it positional \
         with `#[structio(array)]`, which writes the fields in order and asks \
         for no names, or give the fields names"
    }
}

/// `{ "name" => Variant(_), Unit, }`
fn enum_body(
    variants: &[crate::parse::Variant],
    write_only: bool,
    at: Span,
) -> Result<Vec<TokenTree>> {
    let mut body = Out::new(at);
    for variant in variants {
        let opts = attr::variant(&variant.attrs)?;
        match variant.payload {
            Payload::Unit | Payload::One => {}
            Payload::Many(span) => {
                return Err(Error::new(
                    span,
                    "a variant carries one value: the tag wraps one payload, \
                     and the payload is a type of its own. Give these fields a \
                     struct, or a tuple",
                ));
            }
            Payload::Named(span) => {
                return Err(Error::new(
                    span,
                    "a variant with named fields is a stage 2 shape and this \
                     derive does not generate it yet; give the fields a struct \
                     declared on its own, and see docs/derive.md",
                ));
            }
        }
        if !opts.aliases.is_empty() && write_only {
            return Err(Error::new(
                opts.aliases[0].span(),
                "an alias is a rule about reading: it is a further name a \
                 document may use for this variant, and the name the variant \
                 is written under is the declared one. A type declared \
                 `write_only` is never read, so nothing would ever look this \
                 one up",
            ));
        }
        if let Some(name) = opts.rename {
            body.lit(name);
            body.punct('=', Spacing::Joint);
            body.punct('>', Spacing::Alone);
        }
        body.tokens.push(TokenTree::Ident(variant.name.clone()));
        if matches!(variant.payload, Payload::One) {
            let mut hole = Out::new(variant.name.span());
            hole.ident("_");
            body.group(Delimiter::Parenthesis, hole.tokens);
        }
        for alias in opts.aliases {
            body.punct('|', Spacing::Alone);
            body.lit(alias);
        }
        body.punct(',', Spacing::Alone);
    }
    let mut out = Out::new(at);
    out.group(Delimiter::Brace, body.tokens);
    Ok(out.tokens)
}

/// The tokens of a type named in a string, `with = "Vec<Millis>"`, each
/// spanned at the string so a type that does not exist is reported there.
fn adapter(lit: &Literal) -> Result<Vec<TokenTree>> {
    let text = attr::string_content(lit)?;
    let stream: TokenStream = text
        .parse()
        .map_err(|_| Error::new(lit.span(), "expected a type"))?;
    let tokens: Vec<TokenTree> = stream
        .into_iter()
        .map(|tt| respan(tt, lit.span()))
        .collect();
    if tokens.is_empty() {
        return Err(Error::new(lit.span(), "expected a type"));
    }
    Ok(tokens)
}

fn respan(tt: TokenTree, span: Span) -> TokenTree {
    match tt {
        TokenTree::Group(g) => {
            let inner: TokenStream = g.stream().into_iter().map(|t| respan(t, span)).collect();
            let mut g = Group::new(g.delimiter(), inner);
            g.set_span(span);
            TokenTree::Group(g)
        }
        mut other => {
            other.set_span(span);
            other
        }
    }
}

/// A token builder that stamps one span on everything the derive adds.
struct Out {
    tokens: Vec<TokenTree>,
    span: Span,
}

impl Out {
    fn new(span: Span) -> Self {
        Out {
            tokens: Vec::new(),
            span,
        }
    }

    fn ident(&mut self, name: &str) {
        self.tokens
            .push(TokenTree::Ident(Ident::new(name, self.span)));
    }

    fn punct(&mut self, ch: char, spacing: Spacing) {
        let mut p = Punct::new(ch, spacing);
        p.set_span(self.span);
        self.tokens.push(TokenTree::Punct(p));
    }

    fn path_sep(&mut self) {
        self.punct(':', Spacing::Joint);
        self.punct(':', Spacing::Alone);
    }

    /// The `..` that tells the macro the omission of a field is deliberate.
    fn rest(&mut self) {
        self.punct('.', Spacing::Joint);
        self.punct('.', Spacing::Alone);
    }

    fn lit(&mut self, lit: Literal) {
        self.tokens.push(TokenTree::Literal(lit));
    }

    fn group(&mut self, delimiter: Delimiter, tokens: Vec<TokenTree>) {
        let mut g = Group::new(delimiter, tokens.into_iter().collect());
        g.set_span(self.span);
        self.tokens.push(TokenTree::Group(g));
    }

    fn extend(&mut self, tokens: impl IntoIterator<Item = TokenTree>) {
        self.tokens.extend(tokens);
    }
}