rullst-orm-macros 13.0.0-alpha.2

Procedural macros and derive attributes for the rullst-orm Active Record pattern.
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
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use syn::{Data, DeriveInput, Fields, LitStr, Type, parse_macro_input};

mod integer;
mod primary_key;
use integer::{integer_field_kind, is_integer_type};
use primary_key::{parse_key_list, resolve_primary_key};

#[derive(Default)]
struct ModelOptions {
    table: Option<String>,
    label: Option<String>,
    icon: Option<String>,
    primary_key: Option<Vec<String>>,
    tenant_column: Option<String>,
}

#[derive(Default)]
struct FieldOptions {
    label: Option<String>,
    kind: Option<String>,
    options: Vec<String>,
    hidden: bool,
    readonly: bool,
    primary_key: bool,
    relation: bool,
    /// `#[orm(skip)]` / `#[sqlx(skip)]`: the field has no column.
    skipped: bool,
    orm_hidden: bool,
    masked: bool,
    encrypted: bool,
}

/// ORM relation declarations; such fields hold related models, not columns.
const ORM_RELATIONS: &[&str] = &[
    "has_many",
    "has_one",
    "belongs_to",
    "belongs_to_many",
    "morph_many",
    "morph_one",
    "morph_to",
];

/// Consumes the value of a shared `#[orm(...)]` option that Nexus does not
/// read, so `key = value` and `key(...)` options of `#[derive(Orm)]` parse.
pub(crate) fn skip_orm_option(meta: &syn::meta::ParseNestedMeta<'_>) -> syn::Result<()> {
    if meta.input.peek(syn::Token![=]) {
        meta.value()?.parse::<syn::Expr>()?;
    } else if meta.input.peek(syn::token::Paren) {
        let content;
        syn::parenthesized!(content in meta.input);
        content.parse::<TokenStream2>()?;
    }
    Ok(())
}

fn parse_model_options(input: &DeriveInput) -> syn::Result<ModelOptions> {
    let mut options = ModelOptions::default();
    for attribute in &input.attrs {
        if !attribute.path().is_ident("nexus") && !attribute.path().is_ident("orm") {
            continue;
        }
        attribute.parse_nested_meta(|meta| {
            // `table_name` is the ORM's alias of `table`.
            if meta.path.is_ident("table")
                || (meta.path.is_ident("table_name") && attribute.path().is_ident("orm"))
            {
                options.table = Some(meta.value()?.parse::<LitStr>()?.value());
            } else if meta.path.is_ident("label") && attribute.path().is_ident("nexus") {
                options.label = Some(meta.value()?.parse::<LitStr>()?.value());
            } else if meta.path.is_ident("icon") && attribute.path().is_ident("nexus") {
                options.icon = Some(meta.value()?.parse::<LitStr>()?.value());
            } else if meta.path.is_ident("primary_key") && attribute.path().is_ident("nexus") {
                options.primary_key = Some(parse_key_list(&meta.value()?.parse::<LitStr>()?)?);
            } else if meta.path.is_ident("tenant") {
                options.tenant_column = Some(meta.value()?.parse::<LitStr>()?.value());
            } else if attribute.path().is_ident("nexus") {
                return Err(meta.error("unsupported Nexus model option"));
            } else {
                skip_orm_option(&meta)?;
            }
            Ok(())
        })?;
    }
    Ok(options)
}

fn parse_field_options(field: &syn::Field) -> syn::Result<FieldOptions> {
    let mut options = FieldOptions::default();
    for attribute in &field.attrs {
        if attribute.path().is_ident("sqlx") {
            attribute.parse_nested_meta(|meta| {
                options.skipped |= meta.path.is_ident("skip");
                skip_orm_option(&meta)
            })?;
            continue;
        }
        if !attribute.path().is_ident("nexus") && !attribute.path().is_ident("orm") {
            continue;
        }
        attribute.parse_nested_meta(|meta| {
            let nexus_attribute = attribute.path().is_ident("nexus");
            if meta.path.is_ident("primary_key") {
                options.primary_key = true;
            } else if meta.path.is_ident("label") && nexus_attribute {
                options.label = Some(meta.value()?.parse::<LitStr>()?.value());
            } else if meta.path.is_ident("kind") && nexus_attribute {
                options.kind = Some(meta.value()?.parse::<LitStr>()?.value());
            } else if meta.path.is_ident("options") && nexus_attribute {
                let raw = meta.value()?.parse::<LitStr>()?.value();
                options.options = raw
                    .split(',')
                    .map(str::trim)
                    .filter(|value| !value.is_empty())
                    .map(str::to_string)
                    .collect();
            } else if meta.path.is_ident("hidden") && nexus_attribute {
                options.hidden = true;
            } else if meta.path.is_ident("readonly") && nexus_attribute {
                options.readonly = true;
            } else if nexus_attribute {
                return Err(meta.error("unsupported Nexus field option"));
            } else if meta.path.is_ident("skip") {
                options.skipped = true;
            } else if meta.path.is_ident("hidden") {
                options.orm_hidden = true;
            } else if meta.path.is_ident("masked") {
                options.masked = true;
            } else if meta.path.is_ident("encrypted") {
                options.encrypted = true;
            } else if meta.path.is_ident("db_default") {
                // Generated writes leave the column to the database.
                options.readonly = true;
            } else {
                options.relation |= ORM_RELATIONS.iter().any(|key| meta.path.is_ident(key));
                skip_orm_option(&meta)?;
            }
            Ok(())
        })?;
    }
    Ok(options)
}

fn type_name(field_type: &Type) -> String {
    quote!(#field_type).to_string().replace(' ', "")
}

fn unwrapped_type_name(field_type: &Type) -> String {
    let mut type_name = type_name(field_type);
    if type_name.starts_with("Option<") && type_name.ends_with('>') {
        type_name = type_name[7..type_name.len() - 1].to_string();
    }
    type_name
}

/// The chrono date/time widget of a field, by path segment rather than by
/// spelling: `DateTime<Utc>` under any `chrono::` prefix is a UTC instant,
/// `NaiveDateTime` a date-time, `NaiveDate` a date and `NaiveTime` a time.
fn chrono_field_kind(field_type: &Type) -> Option<TokenStream2> {
    let mut field_type = field_type;
    if let Some(inner) = option_inner(field_type) {
        field_type = inner;
    }
    let Type::Path(path) = field_type else {
        return None;
    };
    let segment = path.path.segments.last()?;
    match segment.ident.to_string().as_str() {
        "NaiveDateTime" => Some(quote!(::rullst::nexus::FieldKind::DateTime)),
        "NaiveDate" => Some(quote!(::rullst::nexus::FieldKind::Date)),
        "NaiveTime" => Some(quote!(::rullst::nexus::FieldKind::Time)),
        "DateTime" => {
            let syn::PathArguments::AngleBracketed(arguments) = &segment.arguments else {
                return None;
            };
            let utc = matches!(
                arguments.args.first(),
                Some(syn::GenericArgument::Type(Type::Path(zone)))
                    if zone.path.segments.last().is_some_and(|zone| zone.ident == "Utc")
            );
            utc.then(|| quote!(::rullst::nexus::FieldKind::DateTimeUtc))
        }
        _ => None,
    }
}

/// `T` of an `Option<T>` field type.
fn option_inner(field_type: &Type) -> Option<&Type> {
    let Type::Path(path) = field_type else {
        return None;
    };
    let segment = path.path.segments.last()?;
    let syn::PathArguments::AngleBracketed(arguments) = &segment.arguments else {
        return None;
    };
    match arguments.args.first() {
        Some(syn::GenericArgument::Type(inner)) if segment.ident == "Option" => Some(inner),
        _ => None,
    }
}

fn inferred_field_kind(field_type: &Type) -> TokenStream2 {
    if let Some(kind) = chrono_field_kind(field_type).or_else(|| integer_field_kind(field_type)) {
        return kind;
    }
    match unwrapped_type_name(field_type).as_str() {
        "String" | "&str" => quote!(::rullst::nexus::FieldKind::Text),
        "bool" => quote!(::rullst::nexus::FieldKind::Boolean),
        "f32" | "f64" => quote!(::rullst::nexus::FieldKind::Number),
        _ => quote!(::rullst::nexus::FieldKind::Text),
    }
}

/// How the ORM's confidentiality markers constrain a field in Nexus.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Protection {
    None,
    /// `#[orm(masked)]`: a `Password` widget by default (never listed,
    /// searched, sorted or pre-filled); an explicit `kind` may relax it.
    Masked,
    /// `#[orm(hidden)]`: hidden and read-only by default; an explicit
    /// `kind = "password"` makes it a write-only `Password` field.
    Hidden,
    /// `#[orm(encrypted)]` or `SecretString`: the column holds an envelope that
    /// Nexus must never list, search or overwrite with plaintext.
    Sealed,
}

fn protection(field: &syn::Field, options: &FieldOptions) -> syn::Result<Protection> {
    let sealed = options.encrypted || crate::parser::is_secret_string_type(&field.ty);
    let explicit_widget = options.kind.is_some() || !options.options.is_empty();
    let password_widget = options.kind.as_deref() == Some("password") && options.options.is_empty();
    if sealed {
        if explicit_widget && !password_widget {
            return Err(syn::Error::new_spanned(
                field,
                "`#[orm(encrypted)]` and `SecretString` fields are hidden and read-only in Nexus; remove the #[nexus(kind/options)] override",
            ));
        }
        return Ok(Protection::Sealed);
    }
    if options.orm_hidden {
        if explicit_widget && !password_widget {
            return Err(syn::Error::new_spanned(
                field,
                "`#[orm(hidden)]` fields are hidden in Nexus; only #[nexus(kind = \"password\")] may expose them as a write-only field",
            ));
        }
        return Ok(Protection::Hidden);
    }
    if options.masked {
        return Ok(Protection::Masked);
    }
    Ok(Protection::None)
}

fn configured_field_kind(field: &syn::Field, options: &FieldOptions) -> syn::Result<TokenStream2> {
    let Some(kind) = options.kind.as_deref() else {
        if options.options.is_empty() {
            return Ok(inferred_field_kind(&field.ty));
        }
        return enum_field_kind(field, &options.options);
    };

    let tokens = match kind {
        "text" => quote!(::rullst::nexus::FieldKind::Text),
        "textarea" => quote!(::rullst::nexus::FieldKind::Textarea),
        "email" => quote!(::rullst::nexus::FieldKind::Email),
        "url" => quote!(::rullst::nexus::FieldKind::Url),
        // An integer field keeps its range under an explicit number widget.
        "number" => integer_field_kind(&field.ty)
            .unwrap_or_else(|| quote!(::rullst::nexus::FieldKind::Number)),
        "boolean" => quote!(::rullst::nexus::FieldKind::Boolean),
        "date" => quote!(::rullst::nexus::FieldKind::Date),
        "datetime" => quote!(::rullst::nexus::FieldKind::DateTime),
        "time" => quote!(::rullst::nexus::FieldKind::Time),
        "datetime_utc" => quote!(::rullst::nexus::FieldKind::DateTimeUtc),
        "password" => quote!(::rullst::nexus::FieldKind::Password),
        "json" => quote!(::rullst::nexus::FieldKind::Json),
        "enum" => return enum_field_kind(field, &options.options),
        unsupported => {
            return Err(syn::Error::new_spanned(
                field,
                format!("unsupported Nexus field kind `{unsupported}`"),
            ));
        }
    };
    if !options.options.is_empty() {
        return Err(syn::Error::new_spanned(
            field,
            "Nexus `options` can only be used with kind = \"enum\"",
        ));
    }
    Ok(tokens)
}

fn enum_field_kind(field: &syn::Field, options: &[String]) -> syn::Result<TokenStream2> {
    if options.is_empty() {
        return Err(syn::Error::new_spanned(
            field,
            "Nexus enum fields require a non-empty comma-separated `options` value",
        ));
    }
    Ok(quote!(::rullst::nexus::FieldKind::Enum {
        options: vec![#(#options),*]
    }))
}

fn humanize_field_name(field_name: &str) -> String {
    let label = field_name.replace('_', " ");
    let mut characters = label.chars();
    match characters.next() {
        Some(first) => first.to_uppercase().collect::<String>() + characters.as_str(),
        None => String::new(),
    }
}

fn expand_nexus(input: &DeriveInput) -> syn::Result<TokenStream2> {
    let name = &input.ident;
    let model_options = parse_model_options(input)?;
    let fields = match &input.data {
        Data::Struct(data) => match &data.fields {
            Fields::Named(fields) => &fields.named,
            _ => {
                return Err(syn::Error::new_spanned(
                    input,
                    "Nexus macro only supports structs with named fields",
                ));
            }
        },
        _ => {
            return Err(syn::Error::new_spanned(
                input,
                "Nexus macro can only be used on structs",
            ));
        }
    };

    let table = model_options
        .table
        .unwrap_or_else(|| format!("{}s", name.to_string().to_lowercase()));
    let label = model_options.label.unwrap_or_else(|| format!("{name}s"));
    let icon = model_options.icon.unwrap_or_else(|| "📄".to_string());
    let tenant_column = model_options.tenant_column;
    let mut columns = Vec::with_capacity(fields.len());
    for field in fields {
        let field_ident = field.ident.as_ref().ok_or_else(|| {
            syn::Error::new_spanned(field, "Nexus fields must have an identifier")
        })?;
        let raw_field_name = field_ident.to_string();
        let field_name = raw_field_name
            .strip_prefix("r#")
            .unwrap_or(&raw_field_name)
            .to_string();
        let options = parse_field_options(field)?;
        // Relations hold related models and skipped fields have no column.
        if !options.relation && !options.skipped {
            columns.push((field, field_name, options));
        }
    }
    let primary_key = resolve_primary_key(model_options.primary_key, &columns)?;
    let mut field_metas = Vec::with_capacity(columns.len());

    for (field, field_name, options) in columns {
        let protection = protection(field, &options)?;
        if tenant_column.as_deref() == Some(field_name.as_str())
            && (type_name(&field.ty) != "String"
                || options.kind.as_deref().is_some_and(|kind| kind != "text")
                || !options.options.is_empty())
        {
            return Err(syn::Error::new_spanned(
                field,
                "Nexus tenant columns must use a non-optional `String` with text metadata",
            ));
        }
        let password = quote!(::rullst::nexus::FieldKind::Password);
        let kind = match protection {
            Protection::Sealed | Protection::Hidden => password,
            Protection::Masked if options.kind.is_none() && options.options.is_empty() => password,
            Protection::Masked | Protection::None => configured_field_kind(field, &options)?,
        };
        // Sealed fields, and hidden ones without an explicit write-only
        // widget, are never listed, searched, rendered or written.
        let concealed = protection == Protection::Sealed
            || (protection == Protection::Hidden && options.kind.is_none());
        let label = options
            .label
            .unwrap_or_else(|| humanize_field_name(&field_name));
        // A single integer key is taken to be database-generated and stays
        // out of the forms. Any other key (a UUID, a code, the columns of a
        // composite key) is entered on create; the runtime still keeps every
        // key column read-only on edit.
        let generated_key = matches!(primary_key.as_slice(), [key] if *key == field_name)
            && is_integer_type(&field.ty);
        let hidden = options.hidden
            || concealed
            || generated_key
            || matches!(field_name.as_str(), "password_hash" | "deleted_at");
        let readonly = options.readonly
            || concealed
            || generated_key
            || tenant_column.as_deref() == Some(field_name.as_str())
            || matches!(field_name.as_str(), "created_at" | "updated_at");
        let hidden = hidden || tenant_column.as_deref() == Some(field_name.as_str());

        field_metas.push(quote! {
            ::rullst::nexus::FieldMeta {
                name: #field_name,
                label: #label,
                kind: #kind,
                hidden: #hidden,
                readonly: #readonly,
            }
        });
    }

    for column in &primary_key {
        if !fields
            .iter()
            .filter_map(|field| field.ident.as_ref())
            .any(|field| field == column.as_str())
        {
            return Err(syn::Error::new_spanned(
                input,
                format!("Nexus primary key `{column}` is not a field on `{name}`"),
            ));
        }
    }
    if let Some(tenant_column) = tenant_column.as_deref()
        && !fields
            .iter()
            .filter_map(|field| field.ident.as_ref())
            .any(|field| field == tenant_column)
    {
        return Err(syn::Error::new_spanned(
            input,
            format!("Nexus tenant column `{tenant_column}` is not a field on `{name}`"),
        ));
    }

    let tenant_method = tenant_column
        .as_deref()
        .map(|column| quote!(fn nexus_tenant_column() -> Option<&'static str> { Some(#column) }));
    let first_key = primary_key.first().map_or("id", String::as_str);
    let key_columns_method = (primary_key.len() > 1).then(|| {
        quote!(
            fn nexus_pk_columns() -> Vec<&'static str> {
                vec![#(#primary_key),*]
            }
        )
    });

    Ok(quote! {
        impl ::rullst::nexus::NexusModel for #name {
            fn nexus_table() -> &'static str { #table }
            fn nexus_label() -> &'static str { #label }
            fn nexus_icon() -> &'static str { #icon }
            fn nexus_fields() -> Vec<::rullst::nexus::FieldMeta> {
                vec![#(#field_metas),*]
            }
            fn nexus_pk() -> &'static str { #first_key }
            #key_columns_method
            #tenant_method
        }
    })
}

#[cfg_attr(mutants, mutants::skip)]
pub fn derive_nexus_impl(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    match expand_nexus(&input) {
        Ok(expanded) => expanded.into(),
        Err(error) => error.to_compile_error().into(),
    }
}

#[cfg(test)]
mod tests;