toolkit-zero-macros 1.3.0

Procedural macros for toolkit-zero
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
// ─── #[timelock] ─────────────────────────────────────────────────────────────
//
// Attribute macro that replaces a decorated `fn` with an inline
// `timelock(…)?` / `timelock_async(…).await?` call.
//
// Encryption path (params = None):
//   #[timelock(precision = Minute, format = Hour24, time(14, 37), salts = s, kdf = k)]
//   #[timelock(precision = Hour,   format = Hour24, time(14, 0),  salts = s, kdf = k, cadence = DayOfWeek(Tuesday))]
//   #[timelock(async, precision = Minute, format = Hour24, time(14, 37), salts = s, kdf = k)]
//
// Decryption path (params = Some):
//   #[timelock(params = header)]
//   #[timelock(async, params = header)]

use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use syn::{
    parse::{Parse, ParseStream},
    Expr, Ident, ItemFn, LitInt, Token,
};

// ─── Cadence value ────────────────────────────────────────────────────────────

enum CadenceArg {
    /// Not supplied (or explicit `None`) → `TimeLockCadence::None`
    Default,
    DayOfWeek(Ident),
    DayOfMonth(LitInt),
    MonthOfYear(Ident),
    DayOfWeekInMonth(Ident, Ident),
    DayOfMonthInMonth(LitInt, Ident),
    DayOfWeekAndDayOfMonth(Ident, LitInt),
}

impl CadenceArg {
    /// Parse after the `=` sign following the `cadence` keyword.
    fn parse_value(input: ParseStream) -> syn::Result<Self> {
        let ident: Ident = input.parse()?;
        match ident.to_string().as_str() {
            // explicit None → same as omitted
            "None" => Ok(CadenceArg::Default),

            "DayOfWeek" => {
                let content;
                syn::parenthesized!(content in input);
                let w: Ident = content.parse()?;
                validate_weekday(&w)?;
                Ok(CadenceArg::DayOfWeek(w))
            }
            "DayOfMonth" => {
                let content;
                syn::parenthesized!(content in input);
                let d: LitInt = content.parse()?;
                Ok(CadenceArg::DayOfMonth(d))
            }
            "MonthOfYear" => {
                let content;
                syn::parenthesized!(content in input);
                let m: Ident = content.parse()?;
                validate_month(&m)?;
                Ok(CadenceArg::MonthOfYear(m))
            }
            "DayOfWeekInMonth" => {
                let content;
                syn::parenthesized!(content in input);
                let w: Ident = content.parse()?;
                validate_weekday(&w)?;
                let _: Token![,] = content.parse()?;
                let m: Ident = content.parse()?;
                validate_month(&m)?;
                Ok(CadenceArg::DayOfWeekInMonth(w, m))
            }
            "DayOfMonthInMonth" => {
                let content;
                syn::parenthesized!(content in input);
                let d: LitInt = content.parse()?;
                let _: Token![,] = content.parse()?;
                let m: Ident = content.parse()?;
                validate_month(&m)?;
                Ok(CadenceArg::DayOfMonthInMonth(d, m))
            }
            "DayOfWeekAndDayOfMonth" => {
                let content;
                syn::parenthesized!(content in input);
                let w: Ident = content.parse()?;
                validate_weekday(&w)?;
                let _: Token![,] = content.parse()?;
                let d: LitInt = content.parse()?;
                Ok(CadenceArg::DayOfWeekAndDayOfMonth(w, d))
            }
            other => Err(syn::Error::new(
                ident.span(),
                format!(
                    "#[timelock]: unknown cadence variant `{other}`. \
                     Expected: None, DayOfWeek, DayOfMonth, MonthOfYear, \
                     DayOfWeekInMonth, DayOfMonthInMonth, DayOfWeekAndDayOfMonth"
                ),
            )),
        }
    }

    /// Produce the full `Option::Some(TimeLockCadence::…)` token stream for
    /// the encryption path.
    fn to_tokens(&self) -> TokenStream2 {
        let tl = quote! { ::toolkit_zero::encryption::timelock };
        match self {
            CadenceArg::Default => quote! {
                ::std::option::Option::Some(#tl::TimeLockCadence::None)
            },
            CadenceArg::DayOfWeek(w) => quote! {
                ::std::option::Option::Some(
                    #tl::TimeLockCadence::DayOfWeek(#tl::Weekday::#w)
                )
            },
            CadenceArg::DayOfMonth(d) => quote! {
                ::std::option::Option::Some(
                    #tl::TimeLockCadence::DayOfMonth(#d)
                )
            },
            CadenceArg::MonthOfYear(m) => quote! {
                ::std::option::Option::Some(
                    #tl::TimeLockCadence::MonthOfYear(#tl::Month::#m)
                )
            },
            CadenceArg::DayOfWeekInMonth(w, m) => quote! {
                ::std::option::Option::Some(
                    #tl::TimeLockCadence::DayOfWeekInMonth(
                        #tl::Weekday::#w,
                        #tl::Month::#m,
                    )
                )
            },
            CadenceArg::DayOfMonthInMonth(d, m) => quote! {
                ::std::option::Option::Some(
                    #tl::TimeLockCadence::DayOfMonthInMonth(#d, #tl::Month::#m)
                )
            },
            CadenceArg::DayOfWeekAndDayOfMonth(w, d) => quote! {
                ::std::option::Option::Some(
                    #tl::TimeLockCadence::DayOfWeekAndDayOfMonth(
                        #tl::Weekday::#w,
                        #d,
                    )
                )
            },
        }
    }
}

// ─── Argument struct ──────────────────────────────────────────────────────────

struct TimelockArgs {
    is_async:  bool,
    // ── decryption path ──────────────────────────────────────────────────────
    params:    Option<Expr>,
    // ── encryption path ──────────────────────────────────────────────────────
    precision: Option<Ident>,   // Hour | Quarter | Minute
    format:    Option<Ident>,   // Hour12 | Hour24
    time_h:    Option<LitInt>,
    time_m:    Option<LitInt>,
    cadence:   CadenceArg,      // defaults to None (→ TimeLockCadence::None)
    salts:     Option<Expr>,
    kdf:       Option<Expr>,
}

impl Parse for TimelockArgs {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let mut is_async  = false;
        let mut params    = None::<Expr>;
        let mut precision = None::<Ident>;
        let mut format    = None::<Ident>;
        let mut time_h    = None::<LitInt>;
        let mut time_m    = None::<LitInt>;
        let mut cadence   = CadenceArg::Default;
        let mut salts     = None::<Expr>;
        let mut kdf       = None::<Expr>;

        while !input.is_empty() {
            // `async` is a keyword in Rust — handle it via Token![async].
            if input.peek(Token![async]) {
                let _: Token![async] = input.parse()?;
                is_async = true;
            } else if input.peek(Ident) {
                let ident: Ident = input.parse()?;
                match ident.to_string().as_str() {
                    "params" => {
                        let _: Token![=] = input.parse()?;
                        params = Some(input.parse()?);
                    }
                    "precision" => {
                        let _: Token![=] = input.parse()?;
                        precision = Some(input.parse()?);
                    }
                    "format" => {
                        let _: Token![=] = input.parse()?;
                        format = Some(input.parse()?);
                    }
                    "time" => {
                        let content;
                        syn::parenthesized!(content in input);
                        time_h = Some(content.parse()?);
                        let _: Token![,] = content.parse()?;
                        time_m = Some(content.parse()?);
                    }
                    "cadence" => {
                        let _: Token![=] = input.parse()?;
                        cadence = CadenceArg::parse_value(input)?;
                    }
                    "salts" => {
                        let _: Token![=] = input.parse()?;
                        salts = Some(input.parse()?);
                    }
                    "kdf" => {
                        let _: Token![=] = input.parse()?;
                        kdf = Some(input.parse()?);
                    }
                    other => {
                        return Err(syn::Error::new(
                            ident.span(),
                            format!(
                                "#[timelock]: unknown argument `{other}`. \
                                 Expected: async, params, precision, format, \
                                 time, cadence, salts, kdf"
                            ),
                        ));
                    }
                }
            }

            // Consume trailing comma.
            if input.peek(Token![,]) {
                let _: Token![,] = input.parse()?;
            }
        }

        Ok(TimelockArgs { is_async, params, precision, format, time_h, time_m, cadence, salts, kdf })
    }
}

// ─── Validators ───────────────────────────────────────────────────────────────

fn validate_weekday(w: &Ident) -> syn::Result<()> {
    match w.to_string().as_str() {
        "Monday" | "Tuesday" | "Wednesday" | "Thursday"
            | "Friday" | "Saturday" | "Sunday" => Ok(()),
        other => Err(syn::Error::new(
            w.span(),
            format!(
                "#[timelock]: unknown weekday `{other}`. \
                 Expected: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday"
            ),
        )),
    }
}

fn validate_month(m: &Ident) -> syn::Result<()> {
    match m.to_string().as_str() {
        "January" | "February" | "March"     | "April"
            | "May"    | "June"     | "July"      | "August"
            | "September" | "October"  | "November" | "December" => Ok(()),
        other => Err(syn::Error::new(
            m.span(),
            format!(
                "#[timelock]: unknown month `{other}`. \
                 Expected: January, February, March, April, May, June, \
                 July, August, September, October, November, December"
            ),
        )),
    }
}

// ─── Expansion ───────────────────────────────────────────────────────────────

pub fn expand_timelock(attr: TokenStream, item: TokenStream) -> TokenStream {
    let args = match syn::parse::<TimelockArgs>(attr) {
        Ok(a)  => a,
        Err(e) => return e.to_compile_error().into(),
    };

    let func = match syn::parse::<ItemFn>(item) {
        Ok(f)  => f,
        Err(e) => return e.to_compile_error().into(),
    };

    let binding = &func.sig.ident;
    let tl = quote! { ::toolkit_zero::encryption::timelock };

    let call = if let Some(params_expr) = &args.params {
        // ── Decryption path ──────────────────────────────────────────────────
        // `params` is mutually exclusive with all encryption path arguments.
        if args.precision.is_some()
            || args.format.is_some()
            || args.time_h.is_some()
            || args.salts.is_some()
            || args.kdf.is_some()
        {
            return syn::Error::new_spanned(
                &func.sig.ident,
                "#[timelock]: `params` is mutually exclusive with \
                 precision, format, time, salts, and kdf",
            )
            .to_compile_error()
            .into();
        }

        if args.is_async {
            quote! {
                let #binding = #tl::timelock_async(
                    ::std::option::Option::None,
                    ::std::option::Option::None,
                    ::std::option::Option::None,
                    ::std::option::Option::None,
                    ::std::option::Option::None,
                    ::std::option::Option::None,
                    ::std::option::Option::Some(#params_expr),
                ).await?;
            }
        } else {
            quote! {
                let #binding = #tl::timelock(
                    ::std::option::Option::None,
                    ::std::option::Option::None,
                    ::std::option::Option::None,
                    ::std::option::Option::None,
                    ::std::option::Option::None,
                    ::std::option::Option::None,
                    ::std::option::Option::Some(#params_expr),
                )?;
            }
        }
    } else {
        // ── Encryption path ──────────────────────────────────────────────────
        let precision = match &args.precision {
            Some(p) => p,
            None => return syn::Error::new_spanned(
                &func.sig.ident,
                "#[timelock]: `precision = Hour|Quarter|Minute` is required",
            ).to_compile_error().into(),
        };

        let fmt = match &args.format {
            Some(f) => f,
            None => return syn::Error::new_spanned(
                &func.sig.ident,
                "#[timelock]: `format = Hour12|Hour24` is required",
            ).to_compile_error().into(),
        };

        let time_h = match &args.time_h {
            Some(h) => h,
            None => return syn::Error::new_spanned(
                &func.sig.ident,
                "#[timelock]: `time(hour, minute)` is required",
            ).to_compile_error().into(),
        };
        let time_m = args.time_m.as_ref().unwrap(); // always set with time_h

        let salts = match &args.salts {
            Some(s) => s,
            None => return syn::Error::new_spanned(
                &func.sig.ident,
                "#[timelock]: `salts = <TimeLockSalts expr>` is required",
            ).to_compile_error().into(),
        };

        let kdf = match &args.kdf {
            Some(k) => k,
            None => return syn::Error::new_spanned(
                &func.sig.ident,
                "#[timelock]: `kdf = <KdfParams expr>` is required",
            ).to_compile_error().into(),
        };

        // Validate precision ident.
        match precision.to_string().as_str() {
            "Hour" | "Quarter" | "Minute" => {}
            _ => return syn::Error::new_spanned(
                precision,
                "#[timelock]: `precision` must be Hour, Quarter, or Minute",
            ).to_compile_error().into(),
        }

        // Validate format ident.
        match fmt.to_string().as_str() {
            "Hour12" | "Hour24" => {}
            _ => return syn::Error::new_spanned(
                fmt,
                "#[timelock]: `format` must be Hour12 or Hour24",
            ).to_compile_error().into(),
        }

        let cadence_tokens = args.cadence.to_tokens();

        if args.is_async {
            quote! {
                let #binding = #tl::timelock_async(
                    #cadence_tokens,
                    ::std::option::Option::Some(
                        #tl::TimeLockTime::new(#time_h, #time_m)
                            .expect("#[timelock]: time() values out of range — hour must be 0–23, minute 0–59")
                    ),
                    ::std::option::Option::Some(#tl::TimePrecision::#precision),
                    ::std::option::Option::Some(#tl::TimeFormat::#fmt),
                    ::std::option::Option::Some(#salts),
                    ::std::option::Option::Some(#kdf),
                    ::std::option::Option::None,
                ).await?;
            }
        } else {
            quote! {
                let #binding = #tl::timelock(
                    #cadence_tokens,
                    ::std::option::Option::Some(
                        #tl::TimeLockTime::new(#time_h, #time_m)
                            .expect("#[timelock]: time() values out of range — hour must be 0–23, minute 0–59")
                    ),
                    ::std::option::Option::Some(#tl::TimePrecision::#precision),
                    ::std::option::Option::Some(#tl::TimeFormat::#fmt),
                    ::std::option::Option::Some(#salts),
                    ::std::option::Option::Some(#kdf),
                    ::std::option::Option::None,
                )?;
            }
        }
    };

    call.into()
}