uxar-macros 0.1.5

Procedural macros for the uxar web 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
use darling::FromMeta;
use syn::{
    spanned::Spanned, Attribute, DeriveInput, Error, Fields, Lit, LitInt, LitStr, Meta,
    Result, Token, Type,
};
use syn::punctuated::Punctuated;

// -------------------------------------------------------------------------------------
// Namespace key allowlists (used only for better error messages; does not change acceptance)
// -------------------------------------------------------------------------------------

static VALIDATE_KEYS: &[&str] = &[
    "enum_values",
    "min_length",
    "max_length",
    "exact_length",
    "pattern",
    "email",
    "url",
    "uuid",
    "phone_e164",
    "ipv4",
    "ipv6",
    "date",
    "datetime",
    "min",
    "max",
    "exclusive_min",
    "exclusive_max",
    "multiple_of",
    "min_items",
    "max_items",
    "unique_items",
    "custom",
    "delegate",
];



static COLUMN_KEYS: &[&str] = &[
    "name",
    "primary_key",
    "serial",
    "skip",
    "flatten",
    "json",
    "reference",
    "selectable",
    "insertable",
    "updatable",
    "default",
    "index",
    "index_type",
    "unique",
    "unique_group",
];

// -------------------------------------------------------------------------------------
// Small helpers
// -------------------------------------------------------------------------------------

fn parse_ns_list(
    attr: &Attribute,
    ns: &str,
) -> Result<Option<Vec<darling::ast::NestedMeta>>> {
    if !attr.path().is_ident(ns) {
        return Ok(None);
    }

    let nested = match &attr.meta {
        syn::Meta::List(list) => {
            darling::ast::NestedMeta::parse_meta_list(list.tokens.clone())
                .map_err(|e| augment_darling_error(e.into(), &format!("Error parsing #[{ns}(...)]")))? // preserves spans
        }
        syn::Meta::Path(_) => Vec::new(),
        _ => {
            return Err(Error::new(
                attr.span(),
                format!("expected #[{ns}] or #[{ns}(...)]"),
            ))
        }
    };

    Ok(Some(nested))
}

fn top_level_key(nm: &darling::ast::NestedMeta) -> Option<&syn::Path> {
    use darling::ast::NestedMeta;
    match nm {
        NestedMeta::Meta(syn::Meta::Path(p)) => Some(p),
        NestedMeta::Meta(syn::Meta::NameValue(nv)) => Some(&nv.path),
        NestedMeta::Meta(syn::Meta::List(ml)) => Some(&ml.path),
        NestedMeta::Lit(_) => None,
    }
}

fn enforce_namespace(ns: &str, nested: &[darling::ast::NestedMeta], allowed: &[&str]) -> Result<()> {
    for nm in nested {
        let Some(path) = top_level_key(nm) else { continue };
        let Some(ident) = path.get_ident() else { continue };
        let key = ident.to_string();

        if !allowed.iter().any(|a| *a == key) {
            // "belongs to ..." hint (best-effort)
            let hint = if VALIDATE_KEYS.contains(&key.as_str()) {
                " (belongs to #[validate(...)] )"
            } else if COLUMN_KEYS.contains(&key.as_str()) {
                " (belongs to #[column(...)] )"
            } else {
                ""
            };

            return Err(Error::new(
                ident.span(),
                format!("Unknown option '{key}' in #[{ns}(...)]{hint}"),
            ));
        }
    }
    Ok(())
}

fn parse_enum_attribute(meta: &Meta) -> darling::Result<Vec<Lit>> {
    match meta {
        Meta::List(list) => {
            let nested = list
                .parse_args_with(Punctuated::<Lit, Token![,]>::parse_terminated)
                .map_err(darling::Error::from)?;
            Ok(nested.into_iter().collect())
        }
        _ => Err(darling::Error::custom("Expected list or array for enum").with_span(meta)),
    }
}

#[derive(Debug, Clone, Default)]
pub struct EnumWrapper(pub Vec<Lit>);

impl FromMeta for EnumWrapper {
    fn from_meta(item: &Meta) -> darling::Result<Self> {
        parse_enum_attribute(item).map(EnumWrapper)
    }
}

/// Reference specification for foreign key relationships
#[derive(Debug, Clone, Default)]
pub struct ReferenceSpec {
    pub from: Option<String>,
    pub to: Option<String>,
}

impl FromMeta for ReferenceSpec {
    fn from_word() -> darling::Result<Self> {
        Ok(ReferenceSpec {
            from: None,
            to: None,
        })
    }
    
    fn from_list(items: &[darling::ast::NestedMeta]) -> darling::Result<Self> {
        #[derive(FromMeta)]
        struct RefParams {
            #[darling(default)]
            from: Option<LitStr>,
            #[darling(default)]
            to: Option<LitStr>,
        }
        
        let params = RefParams::from_list(items)?;
        Ok(ReferenceSpec {
            from: params.from.map(|lit| lit.value()),
            to: params.to.map(|lit| lit.value()),
        })
    }
}



// -------------------------------------------------------------------------------------
// Attributes
// -------------------------------------------------------------------------------------

/// Container-level schema attributes
#[derive(Debug, Default, Clone, FromMeta)]
pub struct ContainerAttrs {
    #[darling(default)]
    pub table: Option<LitStr>,
}

/// Field-level validation attributes from #[validate(...)]
#[derive(Debug, Default, Clone, FromMeta)]
pub struct ValidateAttrs {
    #[darling(default, rename = "enum_values")]
    pub enumeration: EnumWrapper,

    #[darling(default)]
    pub min_length: Option<LitInt>,
    #[darling(default)]
    pub max_length: Option<LitInt>,
    #[darling(default)]
    pub exact_length: Option<LitInt>,

    #[darling(default)]
    pub pattern: Option<LitStr>,

    #[darling(default)]
    pub email: bool,
    #[darling(default)]
    pub url: bool,
    #[darling(default)]
    pub uuid: bool,
    #[darling(default)]
    pub phone_e164: bool,
    #[darling(default)]
    pub ipv4: bool,
    #[darling(default)]
    pub ipv6: bool,
    #[darling(default)]
    pub date: bool,
    #[darling(default)]
    pub datetime: bool,

    #[darling(default)]
    pub min: Option<LitInt>,
    #[darling(default)]
    pub max: Option<LitInt>,
    #[darling(default)]
    pub exclusive_min: bool,
    #[darling(default)]
    pub exclusive_max: bool,
    #[darling(default)]
    pub multiple_of: Option<LitInt>,

    #[darling(default)]
    pub min_items: Option<LitInt>,
    #[darling(default)]
    pub max_items: Option<LitInt>,
    #[darling(default)]
    pub unique_items: bool,

    #[darling(default)]
    pub custom: Option<syn::Path>,
    #[darling(default)]
    pub delegate: bool,
}

impl ValidateAttrs {
    pub fn validate(&self) -> Result<()> {
        // Enforce mutually exclusive delegate and custom
        if self.delegate && self.custom.is_some() {
            return Err(Error::new(
                proc_macro2::Span::call_site(),
                "Cannot use both delegate and custom on the same field",
            ));
        }

        // Enforce mutually exclusive format flags
        let format_flags = [
            ("email", self.email),
            ("url", self.url),
            ("uuid", self.uuid),
            ("phone_e164", self.phone_e164),
            ("ipv4", self.ipv4),
            ("ipv6", self.ipv6),
            ("date", self.date),
            ("datetime", self.datetime),
        ];
        let active: Vec<&str> = format_flags
            .iter()
            .filter(|(_, v)| *v)
            .map(|(n, _)| *n)
            .collect();
        if active.len() > 1 {
            return Err(Error::new(
                proc_macro2::Span::call_site(),
                format!("Cannot mix format validators: found {}", active.join(", ")),
            ));
        }

        // Enforce exclusive_min requires min
        if self.exclusive_min && self.min.is_none() {
            return Err(Error::new(
                proc_macro2::Span::call_site(),
                "exclusive_min requires min to be set",
            ));
        }

        // Enforce exclusive_max requires max
        if self.exclusive_max && self.max.is_none() {
            return Err(Error::new(
                proc_macro2::Span::call_site(),
                "exclusive_max requires max to be set",
            ));
        }

        // Enforce exact_length is exclusive with min/max_length
        if self.exact_length.is_some() && (self.min_length.is_some() || self.max_length.is_some())
        {
            return Err(Error::new(
                proc_macro2::Span::call_site(),
                "exact_length cannot be used with min_length or max_length",
            ));
        }

        Ok(())
    }
}

/// Database column metadata from #[column(...)]
#[derive(Debug, Default, Clone, FromMeta)]
pub struct ColumnAttrs {
    #[darling(default)]
    pub name: Option<LitStr>,

    #[darling(default)]
    pub primary_key: bool,
    #[darling(default)]
    pub serial: bool,

    #[darling(default)]
    pub skip: bool,
    #[darling(default)]
    pub flatten: bool,
    #[darling(default)]
    pub json: bool,
    #[darling(default)]
    pub reference: Option<ReferenceSpec>,

    #[darling(default)]
    pub selectable: Option<bool>,
    #[darling(default)]
    pub insertable: Option<bool>,
    #[darling(default)]
    pub updatable: Option<bool>,

    #[darling(default)]
    pub default: Option<LitStr>,

    #[darling(default)]
    pub index: bool,
    #[darling(default)]
    pub index_type: Option<LitStr>,

    #[darling(default)]
    pub unique: bool,

    #[darling(default, multiple, rename = "unique_group")]
    pub unique_groups: Vec<LitStr>,
}

/// Combined field attributes across all namespaces
#[derive(Debug, Clone)]
pub struct FieldMeta {
    pub ident: Option<syn::Ident>,
    pub ty: Type,
    pub validate: ValidateAttrs,
    pub column: ColumnAttrs,
}

fn augment_darling_error(e: darling::Error, context: &str) -> syn::Error {
    let mut combined_err: Option<syn::Error> = None;

    for single_err in e {
        let syn_err: syn::Error = single_err.into();
        let msg = format!("{}: {}", context, syn_err);
        let enriched = Error::new(syn_err.span(), msg);
        match combined_err {
            Some(ref mut existing) => existing.combine(enriched),
            None => combined_err = Some(enriched),
        }
    }

    combined_err.unwrap_or_else(|| {
        Error::new(
            proc_macro2::Span::call_site(),
            format!("{}: Unknown error", context),
        )
    })
}

impl FieldMeta {
    pub fn from_field(field: &syn::Field) -> Result<Self> {
        let mut validate = ValidateAttrs::default();
        let mut column = ColumnAttrs::default();

        let field_name = field
            .ident
            .as_ref()
            .map(|i| i.to_string())
            .unwrap_or_else(|| "<unnamed>".to_string());

        for attr in &field.attrs {
            if let Some(nested) = parse_ns_list(attr, "validate")? {
                enforce_namespace("validate", &nested, VALIDATE_KEYS)?;
                validate = ValidateAttrs::from_list(&nested).map_err(|e| {
                    augment_darling_error(e, &format!("Error decoding #[validate] on field '{field_name}'"))
                })?;
                validate
                    .validate()
                    .map_err(|e| Error::new(attr.span(), e.to_string()))?;
            }

            if let Some(nested) = parse_ns_list(attr, "column")? {
                enforce_namespace("column", &nested, COLUMN_KEYS)?;
                column = ColumnAttrs::from_list(&nested).map_err(|e| {
                    augment_darling_error(e, &format!("Error decoding #[column] on field '{field_name}'"))
                })?;
            }
        }

        Ok(Self {
            ident: field.ident.clone(),
            ty: field.ty.clone(),
            validate,
            column,
        })
    }
}

impl ContainerAttrs {
    pub fn from_attrs(attrs: &[Attribute]) -> Result<Self> {
        for attr in attrs {
            if let Some(nested) = parse_ns_list(attr, "schema")? {
                // no allowlist here because schema is tiny; keep behavior and avoid extra maintenance
                return ContainerAttrs::from_list(&nested)
                    .map_err(|e| augment_darling_error(e, "Error decoding #[schema]"));
            }
        }
        Ok(Self::default())
    }
}

/// Parsed struct information ready for codegen
#[derive(Debug)]
pub struct ParsedStruct {
    pub ident: syn::Ident,
    pub generics: syn::Generics,
    pub container: ContainerAttrs,
    pub fields: Vec<FieldMeta>,
}

impl ParsedStruct {
    pub fn from_derive_input(input: DeriveInput) -> Result<Self> {
        let container = ContainerAttrs::from_attrs(&input.attrs)?;
        let ident = input.ident;
        let generics = input.generics;

        let fields = match input.data {
            syn::Data::Struct(data) => match data.fields {
                Fields::Named(fields) => fields
                    .named
                    .iter()
                    .map(FieldMeta::from_field)
                    .collect::<Result<Vec<_>>>()?,
                _ => {
                    return Err(Error::new_spanned(
                        ident,
                        "only structs with named fields are supported",
                    ))
                }
            },
            _ => return Err(Error::new_spanned(ident, "only structs are supported")),
        };

        Ok(Self {
            ident,
            generics,
            container,
            fields,
        })
    }
}