Skip to main content

clap_config_fallback_derive/
lib.rs

1//! Proc-macro implementation for `clap_config_fallback` derives.
2
3mod derive;
4mod generator;
5mod syn_utils;
6
7use darling::FromDeriveInput;
8use syn::{DeriveInput, parse_macro_input};
9
10use self::{
11    derive::{ClapArg, ConfigArgs, ConfigParser, ConfigSubcommand},
12    generator::{EnumGenerator, StructGenerator},
13    syn_utils::TypeExt,
14};
15
16/// Derives `clap_config_fallback::ConfigParser` for a clap `Parser` root struct.
17#[proc_macro_derive(ConfigParser, attributes(config))]
18pub fn derive_config_parse(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
19    let input = parse_macro_input!(input as DeriveInput);
20
21    ConfigParser::from_derive_input(&input)
22        .map(|v| StructGenerator::new(v).generate())
23        .unwrap_or_else(|e| e.write_errors())
24        .into()
25}
26
27/// Derives `clap_config_fallback::ConfigArgs` for clap `Args` structs.
28#[proc_macro_derive(ConfigArgs, attributes(config))]
29pub fn derive_config_args(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
30    let input = parse_macro_input!(input as DeriveInput);
31
32    ConfigArgs::from_derive_input(&input)
33        .map(|v| StructGenerator::new(v).generate())
34        .unwrap_or_else(|e| e.write_errors())
35        .into()
36}
37
38/// Derives `clap_config_fallback::ConfigSubcommand` for clap `Subcommand` enums.
39#[proc_macro_derive(ConfigSubcommand, attributes(config))]
40pub fn derive_config_subcommand(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
41    let input = parse_macro_input!(input as DeriveInput);
42
43    ConfigSubcommand::from_derive_input(&input)
44        .map(|v| EnumGenerator::new(v).generate())
45        .unwrap_or_else(|e| e.write_errors())
46        .into()
47}