vouched-derive 0.2.0

Proc macros for vouched.
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
use syn::{
    Attribute, Expr, ExprLit, ExprRange, Ident, Lit, Token, Visibility,
    parse::{Parse, ParseStream},
    punctuated::Punctuated,
};

use super::model::{CastConfig, CharPattern, DeriveArg, ErrorConfig, Marker, RangeBound};

impl Parse for CastConfig {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let mut config = Self::default();

        while !input.is_empty() {
            let ident: Ident = input.parse()?;
            match ident.to_string().as_str() {
                "try_from" => {
                    let content;
                    syn::parenthesized!(content in input);
                    let types = Punctuated::<syn::Type, Token![,]>::parse_terminated(&content)?;
                    config.try_from_types = types.into_iter().collect();
                }
                _ => {
                    return Err(syn::Error::new_spanned(
                        ident,
                        "unknown cast option. Supported: try_from(...)",
                    ));
                }
            }

            if input.peek(Token![,]) {
                input.parse::<Token![,]>()?;
            }
        }

        Ok(config)
    }
}

impl Parse for ErrorConfig {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let mut config = Self::default();

        while !input.is_empty() {
            let ident: Ident = input.parse()?;
            input.parse::<Token![=]>()?;

            match ident.to_string().as_str() {
                "name" => {
                    if config.ident.is_some() {
                        return Err(syn::Error::new_spanned(
                            ident,
                            "error name can only be specified once",
                        ));
                    }
                    config.ident = Some(input.parse()?);
                }
                "vis" => {
                    if config.visibility.is_some() {
                        return Err(syn::Error::new_spanned(
                            ident,
                            "error vis can only be specified once",
                        ));
                    }
                    if !input.peek(Token![pub]) {
                        return Err(syn::Error::new(
                            input.span(),
                            "error vis must start with `pub`, such as `pub` or `pub(crate)`",
                        ));
                    }
                    let visibility: Visibility = input.parse()?;
                    if matches!(visibility, Visibility::Inherited) {
                        return Err(syn::Error::new(
                            input.span(),
                            "error vis must be an explicit Rust visibility",
                        ));
                    }
                    config.visibility = Some(visibility);
                }
                _ => {
                    return Err(syn::Error::new_spanned(
                        ident,
                        "unknown error option. Supported: name = ..., vis = ...",
                    ));
                }
            }

            if input.peek(Token![,]) {
                input.parse::<Token![,]>()?;
            }
        }

        if config.ident.is_none() && config.visibility.is_none() {
            return Err(syn::Error::new(
                input.span(),
                "error(...) requires at least one option: name = ... or vis = ...",
            ));
        }

        Ok(config)
    }
}

impl Parse for Marker {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let ident: Ident = input.parse()?;
        match ident.to_string().as_str() {
            "len" => {
                let content;
                syn::parenthesized!(content in input);
                parse_len_bounds(&content)
            }
            "range" => {
                let content;
                syn::parenthesized!(content in input);
                parse_range_bounds(&content)
            }
            "chars" => {
                let content;
                syn::parenthesized!(content in input);
                parse_chars_marker(&content)
            }
            _ => Err(syn::Error::new_spanned(
                ident,
                "unknown vouched marker. Supported markers: len(<range expr>), range(<range expr>), chars(<char-set>)",
            )),
        }
    }
}

/// Parse len expression like `N..M`, `N..=M`, `N..`, `..M`, `..=M`
fn parse_len_bounds(content: ParseStream) -> syn::Result<Marker> {
    if let Ok(expr) = content.parse::<Expr>() {
        match expr {
            Expr::Range(range) => {
                let lower = match range.start.as_ref() {
                    Some(start) => RangeBound::Inclusive((**start).clone()),
                    None => RangeBound::None,
                };
                let upper = match (&range.limits, range.end.as_ref()) {
                    (syn::RangeLimits::HalfOpen(_), Some(end)) => {
                        RangeBound::Exclusive((**end).clone())
                    }
                    (syn::RangeLimits::Closed(_), Some(end)) => {
                        RangeBound::Inclusive((**end).clone())
                    }
                    (_, None) => RangeBound::None,
                };
                reject_empty_literal_len_range(&range, &lower, &upper)?;
                return Ok(Marker::Len { lower, upper });
            }
            _ => {
                return Err(syn::Error::new_spanned(
                    expr,
                    "len marker requires range syntax like `N..M`, `N..=M`, `N..`, `..M`, or `..=M`",
                ));
            }
        }
    }

    Err(syn::Error::new(
        content.span(),
        "len marker requires range syntax like `N..M`, `N..=M`, `N..`, `..M`, or `..=M`",
    ))
}

fn reject_empty_literal_len_range(
    range: &ExprRange,
    lower: &RangeBound,
    upper: &RangeBound,
) -> syn::Result<()> {
    if is_empty_literal_len_range(lower, upper) {
        return Err(syn::Error::new_spanned(
            range,
            "len marker range is empty; use a non-empty range such as `0..=0` or adjust the bounds",
        ));
    }
    Ok(())
}

fn is_empty_literal_len_range(lower: &RangeBound, upper: &RangeBound) -> bool {
    match literal_len_upper_inclusive(upper) {
        Some(LiteralLenUpper::Empty) => true,
        Some(LiteralLenUpper::Inclusive(max)) => {
            literal_len_lower_inclusive(lower).is_some_and(|min| min > max)
        }
        Some(LiteralLenUpper::Unbounded) | None => false,
    }
}

enum LiteralLenUpper {
    Unbounded,
    Inclusive(usize),
    Empty,
}

fn literal_len_lower_inclusive(lower: &RangeBound) -> Option<usize> {
    match lower {
        RangeBound::None => Some(0),
        RangeBound::Inclusive(expr) => literal_usize(expr),
        RangeBound::Exclusive(expr) => literal_usize(expr).and_then(|value| value.checked_add(1)),
    }
}

fn literal_len_upper_inclusive(upper: &RangeBound) -> Option<LiteralLenUpper> {
    match upper {
        RangeBound::None => Some(LiteralLenUpper::Unbounded),
        RangeBound::Inclusive(expr) => literal_usize(expr).map(LiteralLenUpper::Inclusive),
        RangeBound::Exclusive(expr) => literal_usize(expr).map(|value| {
            value
                .checked_sub(1)
                .map_or(LiteralLenUpper::Empty, LiteralLenUpper::Inclusive)
        }),
    }
}

fn literal_usize(expr: &Expr) -> Option<usize> {
    match expr {
        Expr::Lit(ExprLit {
            lit: Lit::Int(lit), ..
        }) => lit.base10_parse::<usize>().ok(),
        _ => None,
    }
}

/// Parse range expression like `N..M`, `N..=M`, `N..`, `..M`, `..=M`
fn parse_range_bounds(content: ParseStream) -> syn::Result<Marker> {
    // Try to parse as a full range expression first
    // syn parses `0..100` as an Expr::Range, so we handle this case
    if let Ok(expr) = content.parse::<Expr>() {
        match expr {
            Expr::Range(range) => {
                let lower = match range.start {
                    Some(start) => RangeBound::Inclusive(*start),
                    None => RangeBound::None,
                };
                let upper = match (range.limits, range.end) {
                    (syn::RangeLimits::HalfOpen(_), Some(end)) => RangeBound::Exclusive(*end),
                    (syn::RangeLimits::Closed(_), Some(end)) => RangeBound::Inclusive(*end),
                    (_, None) => RangeBound::None,
                };
                return Ok(Marker::Range { lower, upper });
            }
            // If we got a non-range expression, it might be a single-bound case
            // But this shouldn't happen with valid range syntax
            _ => {
                return Err(syn::Error::new_spanned(
                    expr,
                    "range marker requires range syntax like `N..M`, `N..=M`, `N..`, `..M`, or `..=M`",
                ));
            }
        }
    }

    Err(syn::Error::new(
        content.span(),
        "range marker requires range syntax like `N..M`, `N..=M`, `N..`, `..M`, or `..=M`",
    ))
}

fn parse_chars_marker(content: ParseStream) -> syn::Result<Marker> {
    let exprs = Punctuated::<Expr, Token![,]>::parse_terminated(content)?;
    let mut patterns = Vec::new();

    for expr in exprs {
        match expr {
            Expr::Lit(ExprLit {
                lit: Lit::Str(lit), ..
            }) => {
                for ch in lit.value().chars() {
                    patterns.push(CharPattern::Single(ch));
                }
            }
            Expr::Lit(ExprLit {
                lit: Lit::Char(lit),
                ..
            }) => {
                patterns.push(CharPattern::Single(lit.value()));
            }
            Expr::Range(range) => {
                if !matches!(range.limits, syn::RangeLimits::Closed(_)) {
                    return Err(syn::Error::new_spanned(
                        range,
                        "chars marker only supports inclusive char ranges like `'a'..='z'`",
                    ));
                }

                let Some(start_expr) = range.start.as_ref() else {
                    return Err(syn::Error::new(
                        content.span(),
                        "chars marker range requires both start and end (e.g. `'a'..='z'`)",
                    ));
                };
                let Some(end_expr) = range.end.as_ref() else {
                    return Err(syn::Error::new(
                        content.span(),
                        "chars marker range requires both start and end (e.g. `'a'..='z'`)",
                    ));
                };

                let start = parse_char_expr(start_expr)?;
                let end = parse_char_expr(end_expr)?;
                if start > end {
                    return Err(syn::Error::new_spanned(
                        range,
                        "chars marker range start must be <= end",
                    ));
                }
                patterns.push(CharPattern::InclusiveRange(start, end));
            }
            other => {
                return Err(syn::Error::new_spanned(
                    other,
                    "chars marker supports string literal, char literal, or inclusive char range like `'a'..='z'`",
                ));
            }
        }
    }

    if patterns.is_empty() {
        return Err(syn::Error::new(
            content.span(),
            "chars marker requires at least one character source",
        ));
    }

    Ok(Marker::Chars { patterns })
}

fn parse_char_expr(expr: &Expr) -> syn::Result<char> {
    match expr {
        Expr::Lit(ExprLit {
            lit: Lit::Char(ch), ..
        }) => Ok(ch.value()),
        _ => Err(syn::Error::new_spanned(
            expr,
            "chars marker range boundaries must be char literals",
        )),
    }
}

impl Parse for DeriveArg {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let ident: Ident = input.fork().parse()?;
        if ident == "cast" {
            input.parse::<Ident>()?; // consume "cast"
            let content;
            syn::parenthesized!(content in input);
            let config: CastConfig = content.parse()?;
            Ok(Self::Cast(config))
        } else if ident == "error" {
            input.parse::<Ident>()?; // consume "error"
            if input.peek(Token![=]) {
                input.parse::<Token![=]>()?;
                let error_ident = input.parse::<Ident>()?;
                return Err(syn::Error::new_spanned(
                    error_ident,
                    "`error = Name` is not supported; use `error(name = Name)`",
                ));
            }
            let content;
            syn::parenthesized!(content in input);
            let config: ErrorConfig = content.parse()?;
            Ok(Self::Error(config))
        } else {
            input.parse().map(|m| Self::Marker(Box::new(m)))
        }
    }
}

pub(super) fn parse_vouched_args(
    attrs: &[Attribute],
) -> syn::Result<(Vec<Marker>, Option<CastConfig>, Option<ErrorConfig>)> {
    let (markers, cast_config, error_config) = attrs
        .iter()
        .filter(|a| a.path().is_ident("vouched"))
        .try_fold((Vec::new(), None, None), fold_vouched_args)?;

    if markers.is_empty() && cast_config.is_none() {
        return Err(syn::Error::new(
            proc_macro2::Span::call_site(),
            "Vouched requires at least one validation marker (len/range/chars) or cast(...) in #[vouched(...)]",
        ));
    }

    Ok((markers, cast_config, error_config))
}

fn fold_vouched_args(
    mut acc: (Vec<Marker>, Option<CastConfig>, Option<ErrorConfig>),
    attr: &Attribute,
) -> syn::Result<(Vec<Marker>, Option<CastConfig>, Option<ErrorConfig>)> {
    let args = attr.parse_args_with(Punctuated::<DeriveArg, Token![,]>::parse_terminated)?;

    for arg in args {
        match arg {
            DeriveArg::Marker(marker) => {
                reject_duplicate_marker(&acc.0, &marker, attr)?;
                acc.0.push(*marker);
            }
            DeriveArg::Cast(config) => {
                if acc.1.is_some() {
                    return Err(syn::Error::new_spanned(
                        attr,
                        "cast(...) can only be specified once",
                    ));
                }
                acc.1 = Some(config);
            }
            DeriveArg::Error(config) => {
                if acc.2.is_some() {
                    return Err(syn::Error::new_spanned(
                        attr,
                        "error(...) can only be specified once",
                    ));
                }
                acc.2 = Some(config);
            }
        }
    }
    Ok(acc)
}

fn reject_duplicate_marker(
    markers: &[Marker],
    marker: &Marker,
    attr: &Attribute,
) -> syn::Result<()> {
    let marker_name = marker_kind_name(marker);
    if markers
        .iter()
        .any(|existing| marker_kind_name(existing) == marker_name)
    {
        return Err(syn::Error::new_spanned(
            attr,
            format!("{marker_name}(...) can only be specified once"),
        ));
    }

    Ok(())
}

fn marker_kind_name(marker: &Marker) -> &'static str {
    match marker {
        Marker::Len { .. } => "len",
        Marker::Range { .. } => "range",
        Marker::Chars { .. } => "chars",
    }
}