onlyargs_derive 0.2.0

Obsessively tiny argument parsing derive macro
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
use myn::prelude::*;
use proc_macro::{Delimiter, Ident, Literal, Span, TokenStream};

#[derive(Debug)]
pub(crate) struct ArgumentStruct {
    pub(crate) name: Ident,
    pub(crate) flags: Vec<ArgFlag>,
    pub(crate) options: Vec<ArgOption>,
    pub(crate) positional: Option<ArgOption>,
    pub(crate) doc: Vec<String>,
    pub(crate) footer: Vec<String>,
}

#[derive(Debug)]
pub(crate) enum Argument {
    Flag(ArgFlag),
    Option(ArgOption),
}

#[derive(Debug)]
pub(crate) struct ArgFlag {
    pub(crate) name: Ident,
    pub(crate) short: Option<char>,
    pub(crate) doc: Vec<String>,
    pub(crate) default: bool,
    pub(crate) output: bool,
}

#[derive(Debug)]
pub(crate) struct ArgOption {
    pub(crate) name: Ident,
    pub(crate) short: Option<char>,
    pub(crate) ty_help: ArgType,
    pub(crate) doc: Vec<String>,
    pub(crate) default: Option<Literal>,
    pub(crate) property: ArgProperty,
}

#[derive(Copy, Clone, Debug)]
pub(crate) struct ArgView<'a> {
    pub(crate) name: &'a Ident,
    pub(crate) short: Option<char>,
    pub(crate) ty_help: Option<ArgType>,
    pub(crate) doc: &'a [String],
}

#[derive(Copy, Clone, Debug)]
pub(crate) enum ArgType {
    Float,
    Integer,
    OsString,
    Path,
    String,
}

#[derive(Copy, Clone, Debug)]
pub(crate) enum ArgProperty {
    Required,
    Optional,
    MultiValue { required: bool },
    Positional { required: bool },
}

impl ArgumentStruct {
    pub(crate) fn parse(input: TokenStream) -> Result<Self, TokenStream> {
        let mut input = input.into_token_iter();
        let attrs = input.parse_attributes()?;
        input.parse_visibility()?;
        input.expect_ident("struct")?;

        let name = input.try_ident()?;
        let content = input.expect_group(Delimiter::Brace)?;
        let fields = Argument::parse(content)?;

        let mut flags = vec![];
        let mut options = vec![];
        let mut positional = None;

        for field in fields {
            match field {
                Argument::Flag(flag) => flags.push(flag),
                Argument::Option(opt) => match (opt.property, &positional) {
                    (ArgProperty::Positional { .. }, None) => positional = Some(opt),
                    (ArgProperty::Positional { .. }, Some(_)) => {
                        return Err(spanned_error(
                            "Positional arguments can only be specified once.",
                            opt.name.span(),
                        ));
                    }
                    _ => options.push(opt),
                },
            }
        }

        let doc = get_doc_comment(&attrs)
            .into_iter()
            .map(trim_with_indent)
            .collect();

        let footer = get_attr_strings(&attrs, "footer")
            .into_iter()
            .map(|line| line.trim_end().to_string())
            .collect();

        match input.next() {
            None => Ok(Self {
                name,
                flags,
                options,
                positional,
                doc,
                footer,
            }),
            tree => Err(spanned_error("Unexpected token", tree.as_span())),
        }
    }
}

impl Argument {
    fn parse(mut input: TokenIter) -> Result<Vec<Self>, TokenStream> {
        let mut args = vec![];

        while input.peek().is_some() {
            let attrs = input.parse_attributes()?;

            // Parse attributes
            let doc = get_doc_comment(&attrs)
                .into_iter()
                .map(trim_with_indent)
                .collect();
            let mut default = None;
            let mut long = false;
            let mut short = None;
            let mut required = false;
            let mut positional = false;

            for mut attr in attrs {
                let name = attr.name.to_string();
                match name.as_str() {
                    "default" => {
                        let mut stream = attr.tree.expect_group(Delimiter::Parenthesis)?;

                        default = Some(stream.try_lit().or_else(|_| {
                            stream
                                .try_ident()
                                .and_then(|ident| match ident.to_string().as_str() {
                                    boolean @ ("true" | "false") => Ok(Literal::string(boolean)),
                                    _ => Err(spanned_error("Unexpected identifier", ident.span())),
                                })
                        })?);
                    }
                    "long" => long = true,
                    "positional" => positional = true,
                    "required" => required = true,
                    "short" => {
                        let mut stream = attr.tree.expect_group(Delimiter::Parenthesis)?;
                        let lit = stream.try_lit()?;

                        short = Some(lit.as_char()?);
                    }
                    _ => (),
                }
            }

            input.parse_visibility()?;
            let name = input.try_ident()?;
            input.expect_punct(':')?;
            let (path, span) = input.parse_path()?;
            let _ = input.expect_punct(',');

            let short = if long {
                None
            } else {
                short.or_else(|| {
                    // TODO: Add an attribute to disable short names
                    name.to_string().chars().find(char::is_ascii_alphabetic)
                })
            };

            if path == "bool" {
                if required {
                    return Err(spanned_error(
                        "#[required] can only be used on `Vec<T>`",
                        span,
                    ));
                }
                if positional {
                    return Err(spanned_error(
                        "#[positional] can only be used on `Vec<T>`",
                        span,
                    ));
                }

                let mut flag = ArgFlag::new(name, short, doc);
                match default {
                    Some(lit) if lit.to_string() == r#""true""# => flag.default = true,
                    _ => (),
                }
                args.push(Self::Flag(flag));
            } else {
                let mut opt = ArgOption::new(span, name, short, doc, &path)?;

                apply_default(span, &mut opt, default)?;
                apply_required(span, &mut opt, required)?;
                apply_positional(span, &mut opt, positional)?;

                if let Some(default) = opt.default.as_ref() {
                    let default = default.to_string();
                    if let Some(line) = opt.doc.last_mut() {
                        line.push_str(&format!(" [default: {default}]"));
                    } else {
                        opt.doc.push(format!("[default: {default}]"));
                    }
                } else if matches!(
                    opt.property,
                    ArgProperty::Required
                        | ArgProperty::Positional { required: true }
                        | ArgProperty::MultiValue { required: true }
                ) {
                    if let Some(line) = opt.doc.last_mut() {
                        line.push_str(" [required]");
                    } else {
                        opt.doc.push("[required]".to_string());
                    }
                }

                args.push(Self::Option(opt));
            }
        }

        Ok(args)
    }
}

fn apply_default(
    span: Span,
    opt: &mut ArgOption,
    default: Option<Literal>,
) -> Result<(), TokenStream> {
    match (default.is_some(), &opt.property) {
        (true, ArgProperty::Required) => opt.default = default,
        (true, _) => {
            return Err(spanned_error(
                "#[default(...)] can only be used on primitive types",
                span,
            ));
        }
        (false, _) => (),
    }

    Ok(())
}

fn apply_required(span: Span, opt: &mut ArgOption, required: bool) -> Result<(), TokenStream> {
    match (required, &mut opt.property) {
        (false, _) => (),
        (true, ArgProperty::MultiValue { required }) => *required = true,
        _ => {
            return Err(spanned_error(
                "#[required] can only be used on `Vec<T>`",
                span,
            ));
        }
    }

    Ok(())
}

fn apply_positional(span: Span, opt: &mut ArgOption, positional: bool) -> Result<(), TokenStream> {
    match (positional, &opt.property) {
        (true, ArgProperty::MultiValue { required }) => {
            opt.property = ArgProperty::Positional {
                required: *required,
            }
        }
        (true, _) => {
            return Err(spanned_error(
                "#[positional] can only be used on `Vec<T>`",
                span,
            ));
        }
        (false, _) => (),
    }

    Ok(())
}

impl ArgFlag {
    fn new(name: Ident, short: Option<char>, doc: Vec<String>) -> Self {
        ArgFlag {
            name,
            short,
            doc,
            default: false,
            output: true,
        }
    }

    pub(crate) fn new_priv(name: Ident, short: Option<char>, doc: Vec<String>) -> Self {
        ArgFlag {
            name,
            short,
            doc,
            default: false,
            output: false,
        }
    }

    pub(crate) fn as_view(&self) -> ArgView {
        ArgView {
            name: &self.name,
            short: self.short,
            ty_help: None,
            doc: &self.doc,
        }
    }
}

// We have to check multiple possible paths for types that are not included in
// `std::prelude`. The type system is not available here, so we need to make some educated
// guesses about field types.
const REQUIRED_PATHS: [&str; 4] = [
    "::std::path::PathBuf",
    "std::path::PathBuf",
    "path::PathBuf",
    "PathBuf",
];
const REQUIRED_OS_STRINGS: [&str; 4] = [
    "::std::ffi::OsString",
    "std::ffi::OsString",
    "ffi::OsString",
    "OsString",
];
const REQUIRED_FLOATS: [&str; 2] = ["f32", "f64"];
const REQUIRED_INTEGERS: [&str; 12] = [
    "i8", "i16", "i32", "i64", "i128", "isize", "u8", "u16", "u32", "u64", "u128", "usize",
];
const MULTI_PATHS: [&str; 4] = [
    "Vec<::std::path::PathBuf>",
    "Vec<std::path::PathBuf>",
    "Vec<path::PathBuf>",
    "Vec<PathBuf>",
];
const MULTI_OS_STRINGS: [&str; 4] = [
    "Vec<::std::ffi::OsString>",
    "Vec<std::ffi::OsString>",
    "Vec<ffi::OsString>",
    "Vec<OsString>",
];
const MULTI_FLOATS: [&str; 2] = ["Vec<f32>", "Vec<f64>"];
const MULTI_INTEGERS: [&str; 12] = [
    "Vec<i8>",
    "Vec<i16>",
    "Vec<i32>",
    "Vec<i64>",
    "Vec<i128>",
    "Vec<isize>",
    "Vec<u8>",
    "Vec<u16>",
    "Vec<u32>",
    "Vec<u64>",
    "Vec<u128>",
    "Vec<usize>",
];
const OPTIONAL_PATHS: [&str; 4] = [
    "Option<::std::path::PathBuf>",
    "Option<std::path::PathBuf>",
    "Option<path::PathBuf>",
    "Option<PathBuf>",
];
const OPTIONAL_OS_STRINGS: [&str; 4] = [
    "Option<::std::ffi::OsString>",
    "Option<std::ffi::OsString>",
    "Option<ffi::OsString>",
    "Option<OsString>",
];
const OPTIONAL_FLOATS: [&str; 2] = ["Option<f32>", "Option<f64>"];
const OPTIONAL_INTEGERS: [&str; 12] = [
    "Option<i8>",
    "Option<i16>",
    "Option<i32>",
    "Option<i64>",
    "Option<i128>",
    "Option<isize>",
    "Option<u8>",
    "Option<u16>",
    "Option<u32>",
    "Option<u64>",
    "Option<u128>",
    "Option<usize>",
];

impl ArgOption {
    fn new(
        span: Span,
        name: Ident,
        short: Option<char>,
        doc: Vec<String>,
        path: &str,
    ) -> Result<Self, TokenStream> {
        // Parse the argument type and decide what properties it should start with.
        let property = if OPTIONAL_PATHS.contains(&path)
            || OPTIONAL_OS_STRINGS.contains(&path)
            || OPTIONAL_FLOATS.contains(&path)
            || OPTIONAL_INTEGERS.contains(&path)
            || path == "Option<String>"
        {
            ArgProperty::Optional
        } else if MULTI_PATHS.contains(&path)
            || MULTI_OS_STRINGS.contains(&path)
            || MULTI_FLOATS.contains(&path)
            || MULTI_INTEGERS.contains(&path)
            || path == "Vec<String>"
        {
            ArgProperty::MultiValue { required: false }
        } else if REQUIRED_PATHS.contains(&path)
            || REQUIRED_OS_STRINGS.contains(&path)
            || REQUIRED_FLOATS.contains(&path)
            || REQUIRED_INTEGERS.contains(&path)
            || path == "String"
        {
            ArgProperty::Required
        } else {
            return Err(spanned_error(
                "Expected bool, PathBuf, String, OsString, integer, or float",
                span,
            ));
        };

        // Decide the type to show in the help message.
        let ty_help = if OPTIONAL_PATHS.contains(&path)
            || REQUIRED_PATHS.contains(&path)
            || MULTI_PATHS.contains(&path)
        {
            ArgType::Path
        } else if OPTIONAL_OS_STRINGS.contains(&path)
            || REQUIRED_OS_STRINGS.contains(&path)
            || MULTI_OS_STRINGS.contains(&path)
        {
            ArgType::OsString
        } else if path == "String" || path == "Vec<String>" || path == "Option<String>" {
            ArgType::String
        } else if OPTIONAL_FLOATS.contains(&path)
            || REQUIRED_FLOATS.contains(&path)
            || MULTI_FLOATS.contains(&path)
        {
            ArgType::Float
        } else if OPTIONAL_INTEGERS.contains(&path)
            || REQUIRED_INTEGERS.contains(&path)
            || MULTI_INTEGERS.contains(&path)
        {
            ArgType::Integer
        } else {
            unreachable!();
        };

        Ok(ArgOption {
            name,
            short,
            ty_help,
            doc,
            default: None,
            property,
        })
    }

    pub(crate) fn as_view(&self) -> ArgView {
        ArgView {
            name: &self.name,
            short: self.short,
            ty_help: Some(self.ty_help),
            doc: &self.doc,
        }
    }
}

impl ArgType {
    pub(crate) fn as_str(&self) -> &str {
        match self {
            Self::Float => " FLOAT",
            Self::Integer => " INTEGER",
            Self::OsString | Self::String => " STRING",
            Self::Path => " PATH",
        }
    }

    pub(crate) fn converter(&self) -> &str {
        match self {
            Self::Float | Self::Integer => "",
            Self::OsString | Self::Path | Self::String => ".into()",
        }
    }
}

#[allow(clippy::needless_pass_by_value)]
fn trim_with_indent(line: String) -> String {
    line.strip_prefix(' ')
        .unwrap_or(&line)
        .trim_end()
        .to_string()
}