ruststream-sqlx-macros 0.7.0

Procedural macros for ruststream-sqlx, the SQL database crate of the RustStream messaging framework.
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
548
549
550
551
552
553
554
555
556
557
558
//! `#[inbox(checked, db = ..)]`: the statements a table's struct determines, built now by the
//! dialect the broker runs at startup and wrapped in `sqlx::query!` inside a function nothing
//! calls, so `cargo sqlx prepare` and the service's own build check them against the database.
//! The run-time path stays the manual form the derive emits beside it.

mod statements;

use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::{format_ident, quote, quote_spanned};
use ruststream_sqlx_dialect::{Param, Role};
use syn::spanned::Spanned;
use syn::{DeriveInput, GenericParam, Generics, Type};

use self::statements::{Event, described, forms, statements};
use crate::check::Errors;
use crate::insert;
use crate::parse::{ColumnField, Field, Inbox, Storage};
use crate::template::KeyItem;

/// `checked` and `db = ..` as the attribute writes them, before they are judged.
#[derive(Default)]
pub(crate) struct Request {
    /// Where `checked` is written.
    pub(crate) checked: Option<Span>,
    /// The word `db = ..` names, where it is one word, and where its value is written.
    pub(crate) db: Option<(Option<String>, Span)>,
}

/// A built-in database whose dialect builds a checked struct's statements.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Db {
    Postgres,
    MySql,
    Sqlite,
}

impl Db {
    const ALL: [Self; 3] = [Self::Postgres, Self::MySql, Self::Sqlite];

    /// The word `db = ..` takes, which is also the name of the feature that builds the dialect.
    const fn word(self) -> &'static str {
        match self {
            Self::Postgres => "postgres",
            Self::MySql => "mysql",
            Self::Sqlite => "sqlite",
        }
    }

    /// The built-in databases the macros are built with.
    fn built() -> Vec<Self> {
        Self::ALL
            .into_iter()
            .filter(|db| forms(*db).is_some())
            .collect()
    }
}

/// A checked struct: the database its statements are checked against, and where `checked` is
/// written.
#[derive(Debug, Clone, Copy)]
pub(crate) struct Checked {
    pub(crate) db: Db,
    pub(crate) span: Span,
}

/// Judges `request` against the databases `built` lists: `checked` names its database, the
/// database is a built-in one, and the macros are built with its dialect.
pub(crate) fn read(request: &Request, built: &[Db]) -> syn::Result<Option<Checked>> {
    match (request.checked, &request.db) {
        (None, None) => Ok(None),
        (Some(span), None) => Err(syn::Error::new(
            span,
            "`checked` checks the table's statements against a database at compile time: name \
             it with `db = postgres`, `db = mysql` or `db = sqlite`",
        )),
        (None, Some((_, span))) => Err(syn::Error::new(
            *span,
            "`db` names the database `checked` checks the statements against: write \
             `#[inbox(checked, db = ..)]`, or drop `db`",
        )),
        (Some(checked), Some((word, span))) => {
            let Some(db) = Db::ALL
                .into_iter()
                .find(|db| word.as_deref() == Some(db.word()))
            else {
                return Err(syn::Error::new(
                    *span,
                    "`db` names no built-in dialect: expected `postgres`, `mysql` or `sqlite`; a \
                     table served by a dialect of the service's own keeps the startup check, \
                     since a procedural macro cannot run that dialect",
                ));
            };
            if !built.contains(&db) {
                let word = db.word();
                return Err(syn::Error::new(
                    *span,
                    format!(
                        "`db = {word}` builds the statements with the {word} dialect, which this \
                         build leaves out: turn on the `{word}` feature of `ruststream-sqlx`"
                    ),
                ));
            }
            Ok(Some(Checked { db, span: checked }))
        }
    }
}

/// Which struct describes the table: a flat one, or the headers struct a message is assembled
/// from, which claims ids alone since the message reads its rows in its own `Fetch`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Layout {
    Flat,
    Headers,
}

/// Whether `clock` reads the database's clock: the derive sees the clock's name alone, and a
/// constant the item emits holds that reading to the type's own answer.
fn database_clock(clock: &syn::Path) -> bool {
    clock
        .segments
        .last()
        .is_some_and(|segment| segment.ident == "DatabaseClock")
}

/// The values the checked statements bind, each a parameter of the function that holds them,
/// typed as the run-time path binds it.
struct Binds<'i, 'a> {
    inbox: &'i Inbox<'a>,
    id: &'i Field<'a>,
    /// The parameters the statements use, in first use.
    used: Vec<(&'static str, TokenStream2)>,
}

impl Binds<'_, '_> {
    fn time(&self, role: Role) -> Option<TokenStream2> {
        self.inbox
            .columns()
            .find(|(_, slot)| slot.role == Some(role))
            .map(|(field, _)| {
                let ty = field.ty;
                quote!(<#ty as ::ruststream_sqlx::TimeColumn>::Time)
            })
    }

    /// Declares the parameter `name` of type `ty` once, and names it.
    fn take(&mut self, name: &'static str, ty: TokenStream2) -> TokenStream2 {
        if !self.used.iter().any(|(used, _)| *used == name) {
            self.used.push((name, ty));
        }
        let ident = format_ident!("{name}");
        quote!(#ident)
    }

    /// What binds `param` in a statement serving `event`.
    fn bind(
        &mut self,
        param: Param,
        event: Event,
        inserted: &[(&Field<'_>, &ColumnField)],
    ) -> Result<TokenStream2, String> {
        let unbound =
            || format!("the {event:?} statement binds {param:?}, which the table has no value for");
        Ok(match (param, event) {
            (Param::Id, _) => {
                let row = self.take("row", quote!(&Self));
                let ident = self.id.ident;
                quote!(#row.#ident)
            }
            (Param::Ids, _) => {
                let ty = self.id.ty;
                self.take("ids", quote!(&[#ty]))
            }
            (Param::Group, _) => self.take("group", quote!(&str)),
            (Param::Destination, _) => self.take("destination", quote!(&str)),
            (Param::Key, _) => self.take("key", quote!(&str)),
            (Param::Limit, _) => self.take("limit", quote!(i64)),
            (Param::Delay, _) => self.take("delay", quote!(i64)),
            (Param::Now, Event::Claim | Event::Guard | Event::Take | Event::Stamp)
            | (Param::RetryAfter, _) => {
                let ty = self.time(Role::RetryAfter).ok_or_else(unbound)?;
                self.take("retry_at", ty)
            }
            (Param::Now, Event::Ack | Event::Discard) => {
                let ty = self.time(Role::ProcessedAt).ok_or_else(unbound)?;
                self.take("processed_at", ty)
            }
            (Param::Lease | Param::LeaseNow | Param::Held, _) => {
                let ty = self.time(Role::LockedUntil).ok_or_else(unbound)?;
                self.take("lease", ty)
            }
            (Param::Column(position), Event::Insert) => {
                let (field, slot) = inserted.get(position).ok_or_else(unbound)?;
                let row = self.take("row", quote!(&Self));
                let ident = field.ident;
                // A JSON column binds through `Json` at run time; its type is sqlx's to judge.
                if slot.json {
                    quote!(::sqlx::types::Json(&#row.#ident) as _)
                } else {
                    quote!(#row.#ident)
                }
            }
            _ => return Err(unbound()),
        })
    }
}

/// The refusals of checked mode the struct's fields and settings show: a flattened field, whose
/// columns the derive cannot see, and a clock chosen where the struct is used.
fn refuse(input: &DeriveInput, inbox: &Inbox<'_>) -> syn::Result<()> {
    let name = &input.ident;
    let mut errors = Errors::default();
    for field in &inbox.fields {
        if matches!(field.storage, Storage::Flattened | Storage::Headers) {
            let ident = field.ident;
            errors.push(syn::Error::new(
                ident.span(),
                format!(
                    "`{ident}` flattens a struct whose columns `checked` cannot see: list its \
                     columns as fields of `{name}`, or drop `checked`"
                ),
            ));
        }
    }
    if let Some(clock) = &inbox.table.clock
        && let Some(ident) = clock.get_ident()
        && input
            .generics
            .params
            .iter()
            .any(|param| matches!(param, GenericParam::Type(ty) if ty.ident == *ident))
    {
        errors.push(syn::Error::new(
            clock.span(),
            format!(
                "`clock = {ident}` is chosen where `{name}` is used, and `checked` builds the \
                 statements now: name the clock's type, or drop `checked`"
            ),
        ));
    }
    errors.finish()
}

/// The checked item of a struct: one `sqlx::query!` per statement it determines, in a function
/// nothing calls; for a headers struct, also the constant that refuses a message reading its rows
/// with the crate's fetch. None for a struct without `checked`.
pub(crate) fn item(
    input: &DeriveInput,
    generics: &Generics,
    inbox: &Inbox<'_>,
    id: (&Field<'_>, &ColumnField),
    key: Option<&[KeyItem]>,
    layout: Layout,
) -> syn::Result<Option<TokenStream2>> {
    let Some(checked) = read(&inbox.table.checked, &Db::built())? else {
        return Ok(None);
    };
    refuse(input, inbox)?;
    let Some(forms) = forms(checked.db) else {
        return Ok(None);
    };
    let table_span = inbox.table.name.span();
    let fail = |message: String| syn::Error::new(table_span, message);
    let statements = described(inbox, id.1, key, |spec| {
        statements(forms, spec, inbox.table.custom, layout)
    })
    .map_err(fail)?;
    let inserted = insert::ordered(&inbox.columns().collect::<Vec<_>>());
    let mut binds = Binds {
        inbox,
        id: id.0,
        used: Vec::new(),
    };
    let mut queries = Vec::new();
    for (event, statement) in &statements {
        let args = statement
            .params()
            .iter()
            .map(|param| binds.bind(*param, *event, &inserted))
            .collect::<Result<Vec<_>, _>>()
            .map_err(fail)?;
        let sql = statement.sql();
        // A lock's outcome is one unnamed value, which a record cannot name a field after on
        // every database (MySQL names the column after its expression).
        let query = if matches!(event, Event::Guard | Event::Lock | Event::Unlock) {
            quote!(query_scalar)
        } else {
            quote!(query)
        };
        queries.push(quote!(let _ = ::sqlx::#query!(#sql #(, #args)*);));
    }
    let params = binds.used.iter().map(|(name, ty)| {
        let ident = format_ident!("{name}");
        quote!(#ident: #ty)
    });
    let name = &input.ident;
    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
    let clock = clock_check(inbox, generics);
    let fetch = (layout == Layout::Headers).then(|| fetch_refusal(name, checked.span));
    Ok(Some(quote! {
        const _: () = {
            #[automatically_derived]
            impl #impl_generics #name #ty_generics #where_clause {
                #[allow(dead_code, clippy::too_many_arguments)]
                fn __ruststream_sqlx_checked(#(#params),*) {
                    #(#queries)*
                }
                #fetch
            }
            #clock
        };
    }))
}

/// Holds the derive's reading of the clock to the clock's own answer: the statements read the
/// database's clock where it is `DatabaseClock`, and a renamed import would read otherwise.
fn clock_check(inbox: &Inbox<'_>, generics: &Generics) -> Option<TokenStream2> {
    let clock = inbox.table.clock.as_ref()?;
    if !generics.params.is_empty() {
        return None;
    }
    let database = database_clock(clock);
    let message = format!(
        "`checked` read `clock = {}` as {} clock: name `DatabaseClock` by that name",
        quote!(#clock).to_string().replace(' ', ""),
        if database {
            "the database's"
        } else {
            "a clock on the host, not the database's"
        },
    );
    Some(quote_spanned! {clock.span()=>
        const _: () = ::core::assert!(
            <#clock as ::ruststream_sqlx::TimeSource>::DATABASE == #database,
            #message,
        );
    })
}

/// The constant a checked headers struct holds in place of the default every other one reads:
/// it stops the build of a message that reads the struct's rows with the crate's own fetch,
/// whose claim of whole rows `checked` cannot build.
fn fetch_refusal(name: &syn::Ident, span: Span) -> TokenStream2 {
    let message = format!(
        "`{name}` is `checked`, and its statements claim ids alone: list `fetch` in \
         `#[inbox(custom(..))]` on the message that flattens `{name}`, and read its rows in the \
         service's own `Fetch`"
    );
    quote_spanned! {span=>
        #[doc(hidden)]
        pub const __RUSTSTREAM_SQLX_FETCH: () = ::core::panic!(#message);
    }
}

/// What a message that reads its headers struct's rows with the crate's own fetch evaluates: the
/// headers struct's constant where it is `checked`, and a unit every other one gets from a trait.
///
/// Why a constant evaluated at build time: the message's derive cannot see whether its headers
/// struct is `checked`, so the rule is held where both meet, in the type `holder` names.
pub(crate) fn default_fetch(holder: &Type) -> TokenStream2 {
    quote_spanned! {holder.span()=>
        trait __DefaultFetch {
            const __RUSTSTREAM_SQLX_FETCH: () = ();
        }
        impl<__T: ?::core::marker::Sized> __DefaultFetch for __T {}
        let () = <#holder>::__RUSTSTREAM_SQLX_FETCH;
    }
}

#[cfg(test)]
mod tests {
    use proc_macro2::Span;
    use ruststream_sqlx_dialect::{
        ClaimShape, Column, Dialect, Form, Postgres, RowLock, TableSpec,
    };
    use syn::{DeriveInput, parse_quote};

    use super::statements::tests::checked_texts;
    use super::{Checked, Db, Request, read};
    use crate::headers::expand;
    use crate::inbox::tests::{errors, expanded};

    #[test]
    fn a_checked_headers_struct_claims_ids_and_refuses_the_crates_fetch()
    -> Result<(), Box<dyn std::error::Error>> {
        let input: DeriveInput = parse_quote! {
            #[inbox(table = "jobs", checked, db = postgres)]
            struct Head {
                #[field(id)] id: i64,
                #[field(processed_at)] done: Option<DateTime<Utc>>,
                trace: String,
            }
        };
        let impls = expand(&input)?.to_string().replace(' ', "");
        let data = [Column::new("trace")];
        let spec = TableSpec::new("jobs", Column::new("id"), Form::RowLock)
            .processed_at(Column::new("done"))
            .data(&data);
        let mut expected: Vec<String> = [
            Postgres.lock_claim(&spec, ClaimShape::Ids)?,
            Postgres.ack(&spec)?,
            Postgres.discard(&spec)?,
            Postgres.insert(&spec)?,
        ]
        .iter()
        .chain(Postgres.retry(&spec)?.iter())
        .map(|statement| statement.sql().replace(' ', ""))
        .collect();
        let mut found = checked_texts(&impls);
        found.sort();
        expected.sort();
        expected.dedup();
        assert_eq!(found, expected, "{impls}");
        assert!(
            impls.contains("pubconst__RUSTSTREAM_SQLX_FETCH:()=::core::panic!(\"`Head`is`checked`"),
            "{impls}"
        );
        // A message that reads the rows with the crate's fetch evaluates that constant; one with
        // its own fetch does not.
        let message: DeriveInput = parse_quote! {
            struct Job { #[field(headers)] #[sqlx(flatten)] headers: Head, note: String }
        };
        let fetched = "let()=<Head>::__RUSTSTREAM_SQLX_FETCH;";
        assert!(expanded(&message)?.contains(fetched));
        let own: DeriveInput = parse_quote! {
            #[inbox(custom(fetch))]
            struct Job { #[field(headers)] #[sqlx(flatten)] headers: Head, note: String }
        };
        assert!(!expanded(&own)?.contains(fetched));
        Ok(())
    }

    #[test]
    fn the_clocks_reading_is_held_to_the_clocks_own_answer() -> syn::Result<()> {
        let database: DeriveInput = parse_quote! {
            #[inbox(table = "jobs", clock = ruststream_sqlx::DatabaseClock, checked, db = postgres)]
            struct Job { #[field(id)] id: i64, #[field(retry_after)] at: DateTime<Utc> }
        };
        let impls = expanded(&database)?;
        assert!(
            impls.contains(
                "<ruststream_sqlx::DatabaseClockas::ruststream_sqlx::TimeSource>::DATABASE==true"
            ),
            "{impls}"
        );
        // The database's clock binds no time: the statements read it themselves.
        assert!(!impls.contains("retry_at:"), "{impls}");
        let host: DeriveInput = parse_quote! {
            #[inbox(table = "jobs", clock = Frozen, checked, db = postgres)]
            struct Job { #[field(id)] id: i64, #[field(retry_after)] at: DateTime<Utc> }
        };
        let impls = expanded(&host)?;
        assert!(
            impls.contains("<Frozenas::ruststream_sqlx::TimeSource>::DATABASE==false"),
            "{impls}"
        );
        assert!(impls.contains("retry_at:"), "{impls}");
        Ok(())
    }

    #[test]
    fn a_struct_without_checked_emits_no_query() -> syn::Result<()> {
        let input: DeriveInput = parse_quote! {
            #[inbox(table = "jobs")]
            struct Job { #[field(id)] id: i64, #[field(payload)] payload: Vec<u8> }
        };
        let impls = expanded(&input)?;
        assert!(!impls.contains("query!"), "{impls}");
        assert!(!impls.contains("__ruststream_sqlx_checked"), "{impls}");
        Ok(())
    }

    #[test]
    fn misuse_of_checked_is_reported() {
        let cases: [(DeriveInput, &str); 10] = [
            (
                parse_quote! { #[inbox(table = "jobs", checked = true, db = postgres)] struct Job { #[field(id)] id: i64 } },
                "`checked` takes no value: `#[inbox(checked, db = ..)]`",
            ),
            (
                parse_quote! { #[inbox(table = "jobs", checked, checked, db = postgres)] struct Job { #[field(id)] id: i64 } },
                "`checked` is given twice",
            ),
            (
                parse_quote! { #[inbox(table = "jobs", checked, db = postgres, db = mysql)] struct Job { #[field(id)] id: i64 } },
                "`db` is given twice",
            ),
            (
                parse_quote! { #[inbox(table = "jobs", checked)] struct Job { #[field(id)] id: i64 } },
                "`checked` checks the table's statements against a database at compile time: \
                 name it with `db = postgres`, `db = mysql` or `db = sqlite`",
            ),
            (
                parse_quote! { #[inbox(table = "jobs", db = postgres)] struct Job { #[field(id)] id: i64 } },
                "`db` names the database `checked` checks the statements against: write \
                 `#[inbox(checked, db = ..)]`, or drop `db`",
            ),
            (
                parse_quote! { #[inbox(table = "jobs", checked, db = mssql)] struct Job { #[field(id)] id: i64 } },
                "`db` names no built-in dialect: expected `postgres`, `mysql` or `sqlite`; a \
                 table served by a dialect of the service's own keeps the startup check, since a \
                 procedural macro cannot run that dialect",
            ),
            (
                parse_quote! { #[inbox(table = "jobs", checked, db = "postgres")] struct Job { #[field(id)] id: i64 } },
                "`db` names no built-in dialect: expected `postgres`, `mysql` or `sqlite`; a \
                 table served by a dialect of the service's own keeps the startup check, since a \
                 procedural macro cannot run that dialect",
            ),
            (
                parse_quote! {
                    #[inbox(table = "jobs", checked, db = postgres)]
                    struct Job { #[field(id)] id: i64, #[sqlx(flatten)] body: Body }
                },
                "`body` flattens a struct whose columns `checked` cannot see: list its columns as \
                 fields of `Job`, or drop `checked`",
            ),
            (
                parse_quote! {
                    #[inbox(table = "jobs", clock = Source, checked, db = postgres)]
                    struct Job<Source> { #[field(id)] id: i64, #[sqlx(skip)] source: PhantomData<Source> }
                },
                "`clock = Source` is chosen where `Job` is used, and `checked` builds the \
                 statements now: name the clock's type, or drop `checked`",
            ),
            (
                parse_quote! { #[inbox(table = "jobs", checked, db = sqlite)] struct Job { #[field(id)] id: i64 } },
                "the sqlite dialect claims no rows in the row lock form, which this table takes: \
                 claim them by lease, with a `#[field(locked_until)]` field, or by advisory lock, \
                 with `advisory_lock = \"..\"`",
            ),
        ];
        for (input, expected) in cases {
            assert_eq!(errors(&input), [expected], "{}", input.ident);
        }
    }

    #[test]
    fn a_database_the_build_leaves_out_names_its_feature() {
        let request = Request {
            checked: Some(Span::call_site()),
            db: Some((Some("mysql".to_owned()), Span::call_site())),
        };
        let refused = read(&request, &[Db::Postgres, Db::Sqlite])
            .err()
            .map(|error| error.to_string());
        assert_eq!(
            refused.as_deref(),
            Some(
                "`db = mysql` builds the statements with the mysql dialect, which this build \
                 leaves out: turn on the `mysql` feature of `ruststream-sqlx`"
            )
        );
        assert!(matches!(
            read(&request, &[Db::MySql]),
            Ok(Some(Checked { db: Db::MySql, .. }))
        ));
    }
}