|
| 1 | +use proc_macro::TokenStream; |
| 2 | +use quote::quote; |
| 3 | +use syn::{Data, DataStruct, DeriveInput, parse_macro_input}; |
1 | 4 |
|
| 5 | +#[proc_macro_derive(Parser, attributes(arg, command))] |
| 6 | +pub fn derive_parser(input: TokenStream) -> TokenStream { |
| 7 | + let input = parse_macro_input!(input as DeriveInput); |
| 8 | + let ident = input.ident; |
| 9 | + |
| 10 | + match input.data { |
| 11 | + Data::Struct(DataStruct { fields, .. }) => { |
| 12 | + let mut field_declarations = Vec::new(); |
| 13 | + let mut field_setters = Vec::new(); |
| 14 | + let mut field_getters = Vec::new(); |
| 15 | + |
| 16 | + for field in fields { |
| 17 | + let ident = field.ident.unwrap(); |
| 18 | + let ty = field.ty; |
| 19 | + let option = ident.to_string(); |
| 20 | + |
| 21 | + field_declarations.push(quote! { |
| 22 | + let mut #ident: Option<#ty> = None; |
| 23 | + }); |
| 24 | + |
| 25 | + field_setters.push(quote! { |
| 26 | + sap::Argument::Long(#option) => { #ident = Some(true) } |
| 27 | + }); |
| 28 | + |
| 29 | + field_getters.push(quote! { |
| 30 | + #ident: #ident.unwrap() |
| 31 | + }); |
| 32 | + } |
| 33 | + |
| 34 | + quote! { |
| 35 | + impl #ident { |
| 36 | + const HELP: &str = ""; |
| 37 | + |
| 38 | + pub fn parse() -> ::sap::Result<Self> { |
| 39 | + use std::io::Write as _; |
| 40 | + |
| 41 | + let mut arg_parser = sap::Parser::from_env()?; |
| 42 | + |
| 43 | + #(#field_declarations)* |
| 44 | + |
| 45 | + while let Some(arg) = arg_parser.forward()? { |
| 46 | + |
| 47 | + match arg { |
| 48 | + // Long("version") => { |
| 49 | + // stdout |
| 50 | + // .write_all( |
| 51 | + // "wc (puppyutils) 0.0.1\nLicensed under the European Union Public Licence (EUPL) <https://eupl.eu/>\n" |
| 52 | + // .as_bytes(), |
| 53 | + // )?; |
| 54 | + // stdout.flush()?; |
| 55 | + // std::process::exit(0); |
| 56 | + // } |
| 57 | + // Long("help") => { |
| 58 | + // stdout |
| 59 | + // .write_all( |
| 60 | + // "Usage: wc [OPTION]... [FILE]...\nPrint newline, word, and byte counts for each FILE. A word is a nonempty \nsequence of non white space delimited by white space characters or by start \nor end of input.\n\n -c, --bytes print the byte counts\n -m, --chars print the character counts\n -l, --lines print the newline counts\n -w, --words print the word counts\n --help display this help and exit\n --version output version information and exit\n\nWith no FILE, or when FILE is -, read standard input.\n" |
| 61 | + // .as_bytes(), |
| 62 | + // )?; |
| 63 | + // stdout.flush()?; |
| 64 | + // std::process::exit(0); |
| 65 | + // } |
| 66 | + #(#field_setters),* |
| 67 | + arg => return Err(arg.into_error(None)), |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + Ok(Self { #(#field_getters),* }) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + } |
| 76 | + .into() |
| 77 | + } |
| 78 | + Data::Enum(_data_enum) => todo!(), |
| 79 | + Data::Union(_data_union) => todo!(), |
| 80 | + } |
| 81 | +} |
0 commit comments