fack-codegen 0.2.0

Standalone code generation engine for fack error handling
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
//! Format syntax, field capture resolution, and formatting requirements.

use alloc::{
    string::{String, ToString},
    vec::Vec,
};

use proc_macro2::TokenStream;
use quote::{ToTokens, quote};
use syn::{
    Expr, Ident, LitStr, Token,
    parse::{Parse, ParseStream},
    punctuated::Punctuated,
};

use crate::{
    expand::Expand,
    field::{FieldId, Fields},
    resolve::Resolve,
};

/// A source-level Rust format string and explicit arguments.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Format {
    /// Source format string literal.
    literal: LitStr,

    /// Explicit Rust formatting arguments.
    arguments: Punctuated<Argument, Token![,]>,
}

impl Parse for Format {
    /// Parse the format literal together with any explicit Rust arguments.
    #[inline]
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let literal = input.parse()?;
        let arguments = if input.peek(Token![,]) {
            let _ = input.parse::<Token![,]>()?;

            Punctuated::<Argument, Token![,]>::parse_terminated(input)?
        } else {
            Punctuated::new()
        };

        Ok(Self { literal, arguments })
    }
}

/// One explicit Rust format argument.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Argument {
    /// Optional explicit named-argument identifier.
    name: Option<Ident>,

    /// Rust expression supplying the argument value.
    expression: Expr,
}

impl Parse for Argument {
    /// Parse one positional or explicitly named Rust format argument.
    #[inline]
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let name = if input.peek(Ident) && input.peek2(Token![=]) && !input.peek2(Token![==]) {
            let name = input.parse()?;
            let _ = input.parse::<Token![=]>()?;

            Some(name)
        } else {
            None
        };
        let expression = input.parse()?;

        Ok(Self { name, expression })
    }
}

impl ToTokens for Argument {
    /// Reconstruct the explicit argument syntax for the generated `write!`
    /// call.
    #[inline]
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let Self { name, expression } = self;

        match name {
            Some(name) => quote::quote!(#name = #expression).to_tokens(tokens),
            None => expression.to_tokens(tokens),
        }
    }
}

/// A formatting trait required by one captured field.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum FormatTrait {
    /// `Display` formatting.
    Display,

    /// `Debug` formatting.
    Debug,

    /// Lower hexadecimal formatting.
    LowerHex,

    /// Upper hexadecimal formatting.
    UpperHex,

    /// Octal formatting.
    Octal,

    /// Binary formatting.
    Binary,

    /// Lower exponential formatting.
    LowerExp,

    /// Upper exponential formatting.
    UpperExp,

    /// Pointer formatting.
    Pointer,
}

/// One resolved field use in a format string.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct FormatUse {
    /// Resolved captured field.
    field: FieldId,

    /// Formatting trait required by the capture.
    format_trait: FormatTrait,
}

impl FormatUse {
    /// Return the referenced field.
    #[inline]
    #[must_use]
    pub const fn field(&self) -> FieldId {
        let Self { field, .. } = self;

        *field
    }

    /// Return the formatting trait required by this use.
    #[inline]
    #[must_use]
    pub const fn format_trait(&self) -> FormatTrait {
        let Self { format_trait, .. } = self;

        *format_trait
    }
}

/// A validated format string whose field references have been resolved.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Resolved {
    /// The rewritten format string consumed by generated `write!` calls.
    literal: LitStr,

    /// Explicit Rust format arguments supplied by the user.
    arguments: syn::punctuated::Punctuated<Argument, syn::Token![,]>,

    /// Formatting requirements introduced by captured fields.
    uses: Vec<FormatUse>,

    /// Every field that must be bound for formatting.
    fields: Vec<FieldId>,

    /// Whether the message can use `Formatter::write_str` directly.
    static_message: bool,
}

impl Resolved {
    /// Resolve format captures against one field list.
    fn resolve_format(raw: Format, fields: &Fields) -> syn::Result<Self> {
        let explicit_names = Self::explicit_names(&raw.arguments);
        let has_positional = raw.arguments.iter().any(|argument| Self::explicit_name(argument).is_none());
        let value = raw.literal.value();
        let mut read = value.as_str();
        let mut output = String::with_capacity(value.len());
        let mut uses = Vec::new();
        let mut selected = Vec::new();

        while let Some(open) = read.find('{') {
            output.push_str(&read[..open]);
            read = &read[open..];

            if let Some(rest) = read.strip_prefix("{{") {
                output.push_str("{{");
                read = rest;
                continue;
            }

            let Some(close) = read[1..].find('}') else {
                output.push_str(read);
                read = "";
                break;
            };
            let close = close + 1;
            let inside = &read[1..close];
            let rewritten = Self::resolve_capture(
                inside,
                fields,
                &explicit_names,
                has_positional,
                &mut uses,
                &mut selected,
                raw.literal.span(),
            )?;

            output.push('{');
            output.push_str(&rewritten);
            output.push('}');
            read = &read[close + 1..];
        }

        output.push_str(read);

        let static_message = raw.arguments.is_empty() && !value.contains('{') && !value.contains('}');
        let format = LitStr::new(&output, raw.literal.span());

        Ok(Self {
            literal: format,
            arguments: raw.arguments,
            uses,
            fields: selected,
            static_message,
        })
    }

    /// Resolve one `{...}` capture and rewrite tuple fields to generated local
    /// names.
    fn resolve_capture(
        inside: &str,
        fields: &Fields,
        explicit_names: &[String],
        has_positional: bool,
        uses: &mut Vec<FormatUse>,
        selected: &mut Vec<FieldId>,
        span: proc_macro2::Span,
    ) -> syn::Result<String> {
        let argument_end = inside.find(':').unwrap_or(inside.len());
        let argument = &inside[..argument_end];
        let spec = &inside[argument_end..];
        let mut rewritten = String::with_capacity(inside.len() + 1);

        match Self::capture_field(argument, fields, explicit_names, span)? {
            Some(field) => {
                if argument.as_bytes().first().is_some_and(u8::is_ascii_digit) && has_positional {
                    return Err(syn::Error::new(
                        span,
                        "ambiguous numeric field capture with explicit positional format arguments",
                    ));
                }

                Self::push_field(selected, field);
                uses.push(FormatUse {
                    field,
                    format_trait: Self::format_trait(spec),
                });

                match fields.name(field) {
                    Some(_) => rewritten.push_str(argument),
                    None => {
                        rewritten.push('_');
                        rewritten.push_str(argument);
                    }
                }
            }
            None => rewritten.push_str(argument),
        }

        rewritten.push_str(&Self::resolve_dynamic_spec(spec, fields, selected, span)?);

        Ok(rewritten)
    }

    /// Interpret a capture head as a field only when explicit arguments do not
    /// own it.
    fn capture_field(argument: &str, fields: &Fields, explicit_names: &[String], span: proc_macro2::Span) -> syn::Result<Option<FieldId>> {
        if argument.is_empty() {
            return Ok(None);
        }

        if argument.as_bytes().iter().all(u8::is_ascii_digit) {
            let index = argument
                .parse::<usize>()
                .map_err(|_| syn::Error::new(span, "invalid positional format capture"))?;

            return match index < fields.len() && fields.name(FieldId::from_index(index)).is_none() {
                true => Ok(Some(FieldId::from_index(index))),
                false => Ok(None),
            };
        }

        if explicit_names.iter().any(|name| name == argument) {
            return Ok(None);
        }

        let ident = syn::parse_str::<syn::Ident>(argument).map_err(|_| syn::Error::new(span, "invalid named format capture"))?;
        fields.named(&ident).map(Some)
    }

    /// Resolve field references used by dynamic width and precision specifiers.
    fn resolve_dynamic_spec(spec: &str, fields: &Fields, selected: &mut Vec<FieldId>, span: proc_macro2::Span) -> syn::Result<String> {
        let mut output = String::with_capacity(spec.len());
        let bytes = spec.as_bytes();
        let mut index = 0;

        while index < bytes.len() {
            let start = index;
            let is_word = bytes[index].is_ascii_alphanumeric() || bytes[index] == b'_';

            if !is_word {
                output.push(bytes[index] as char);
                index += 1;
                continue;
            }

            while index < bytes.len() && (bytes[index].is_ascii_alphanumeric() || bytes[index] == b'_') {
                index += 1;
            }

            let token = &spec[start..index];
            let dynamic = bytes.get(index) == Some(&b'$');

            if !dynamic {
                output.push_str(token);
                continue;
            }

            if token.as_bytes().iter().all(u8::is_ascii_digit) {
                let field_index = token
                    .parse::<usize>()
                    .map_err(|_| syn::Error::new(span, "invalid dynamic format field index"))?;

                if field_index < fields.len() && fields.name(FieldId::from_index(field_index)).is_none() {
                    let field = FieldId::from_index(field_index);
                    Self::push_field(selected, field);
                    output.push('_');
                }
            } else if let Ok(ident) = syn::parse_str::<syn::Ident>(token)
                && let Ok(field) = fields.named(&ident)
            {
                Self::push_field(selected, field);
            }

            output.push_str(token);
        }

        Ok(output)
    }

    /// Classify the Rust formatting trait required by one format specifier.
    fn format_trait(spec: &str) -> FormatTrait {
        match spec.chars().next_back() {
            Some('?') => FormatTrait::Debug,
            Some('o') => FormatTrait::Octal,
            Some('x') => FormatTrait::LowerHex,
            Some('X') => FormatTrait::UpperHex,
            Some('p') => FormatTrait::Pointer,
            Some('b') => FormatTrait::Binary,
            Some('e') => FormatTrait::LowerExp,
            Some('E') => FormatTrait::UpperExp,
            _ => FormatTrait::Display,
        }
    }

    /// Collect names reserved by explicit user-supplied format arguments.
    fn explicit_names(arguments: &syn::punctuated::Punctuated<Argument, syn::Token![,]>) -> Vec<String> {
        arguments
            .iter()
            .filter_map(|argument| argument.name.as_ref())
            .map(ToString::to_string)
            .collect()
    }

    /// Return the explicit name of one format argument when present.
    const fn explicit_name(argument: &Argument) -> Option<&syn::Ident> {
        argument.name.as_ref()
    }

    /// Record a selected field once while preserving first-use order.
    fn push_field(fields: &mut Vec<FieldId>, field: FieldId) {
        if !fields.contains(&field) {
            fields.push(field);
        }
    }
}

impl Resolved {
    /// Return the rewritten format literal consumed by generated code.
    #[inline]
    #[must_use]
    pub const fn literal(&self) -> &LitStr {
        let Self { literal, .. } = self;

        literal
    }

    /// Return explicit Rust format arguments.
    #[inline]
    #[must_use]
    pub const fn arguments(&self) -> &Punctuated<Argument, Token![,]> {
        let Self { arguments, .. } = self;

        arguments
    }

    /// Return formatting requirements introduced by captured fields.
    #[inline]
    #[must_use]
    pub fn uses(&self) -> &[FormatUse] {
        let Self { uses, .. } = self;

        uses
    }

    /// Return every field required by formatting.
    #[inline]
    #[must_use]
    pub fn fields(&self) -> &[FieldId] {
        let Self { fields, .. } = self;

        fields
    }

    /// Return whether formatting can use `Formatter::write_str` directly.
    #[inline]
    #[must_use]
    pub const fn is_static(&self) -> bool {
        let Self { static_message, .. } = self;

        *static_message
    }
}

impl Resolve for Format {
    /// Field collection used to resolve captures.
    type Context = Fields;

    /// Format representation whose field references are already validated.
    type Output = Resolved;

    /// Resolve every field-dependent part of the source format.
    #[inline]
    fn resolve(self, fields: &Self::Context) -> syn::Result<Self::Output> {
        Resolved::resolve_format(self, fields)
    }
}

/// Context required to expand one resolved format body.
#[derive(Clone, Debug)]
pub struct ExpandContext(TokenStream);

impl ExpandContext {
    /// Construct a format expansion context.
    #[inline]
    #[must_use]
    pub const fn new(root: TokenStream) -> Self {
        Self(root)
    }
}

impl Expand for &Resolved {
    /// Root-path context needed by non-static `write!` expansion.
    type Context = ExpandContext;

    /// Generate either `write_str` or `write!` from the resolved
    /// representation.
    #[inline]
    fn expand_with(self, context: Self::Context) -> syn::Result<TokenStream> {
        let ExpandContext(root) = context;
        let literal = self.literal();
        let arguments = self.arguments();

        if self.is_static() {
            Ok(quote! { f.write_str(#literal) })
        } else {
            Ok(quote! { #root::write!(f, #literal, #arguments) })
        }
    }
}