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
// #![allow(warnings)]
#![allow(clippy::missing_panics_doc)]

mod ident;
mod manifest;

use manifest::Manifest;
use proc_macro2::TokenStream;
use quote::quote;
use std::path::{Path, PathBuf};

fn resolve_path(path: impl AsRef<Path>) -> PathBuf {
    let root = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".into()));
    if root.join(path.as_ref()).exists() {
        root.join(path.as_ref())
    } else {
        root.join("src/").join(path.as_ref())
    }
}

fn quote_option<T: quote::ToTokens>(value: &Option<T>) -> TokenStream {
    match value {
        Some(v) => quote! { Some(#v) },
        None => quote! { None },
    }
}

fn get_attribute(attr: &syn::Attribute) -> String {
    match &attr.parse_meta().unwrap() {
        syn::Meta::NameValue(syn::MetaNameValue { lit, .. }) => match lit {
            syn::Lit::Str(s) => s.value(),
            _ => panic!("action attribute must be a string"),
        },
        _ => panic!("action attribute must be of the form `action = \"...\"`"),
    }
}

fn parse_derive(ast: &syn::DeriveInput) -> (&syn::Ident, &syn::Generics, PathBuf) {
    let name = &ast.ident;
    let generics = &ast.generics;

    let manifests: Vec<_> = ast
        .attrs
        .iter()
        .filter(|attr| attr.path.is_ident("action"))
        .map(get_attribute)
        .map(resolve_path)
        .collect();

    let manifest = manifests.into_iter().next().expect("a path to an action manifest (action.yml) file needs to be provided with the #[action = \"PATH\"] attribute");
    (name, generics, manifest)
}

#[proc_macro_derive(Action, attributes(action))]
pub fn action_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let ast: syn::DeriveInput = syn::parse2(input.into()).unwrap();
    let (struct_name, generics, manifest_path) = parse_derive(&ast);
    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

    let manifest = Manifest::from_action_yml(manifest_path);
    // dbg!(&manifest);

    let input_enum_variants: Vec<_> = manifest
        .inputs
        .keys()
        .map(|name| {
            let variant = ident::str_to_enum_variant(name);
            quote! { #variant }
        })
        .collect();

    let input_enum_matches: Vec<_> = manifest
        .inputs
        .keys()
        .map(|name| {
            let variant = ident::str_to_enum_variant(name);
            quote! { #name => Ok(Self::#variant) }
        })
        .collect();

    let input_enum_ident = quote::format_ident!("{}Input", struct_name);
    let input_enum = quote! {
        #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
        pub enum #input_enum_ident {
            #(#input_enum_variants,)*
        }

        impl std::str::FromStr for #input_enum_ident {
            type Err = ();
            fn from_str(input: &str) -> Result<Self, Self::Err> {
                match input {
                    #(#input_enum_matches,)*
                    _  => Err(()),
                }
            }
        }
    };
    // eprintln!("{}", pretty_print(&quote! { #input_enum }));

    let parse_impl = quote! {
        #[allow(clippy::all)]
        impl #impl_generics ::actions::Parse for #struct_name #ty_generics #where_clause {
            type Input = #input_enum_ident;

            fn parse<E: ::actions::ReadEnv>(env: &E) -> std::collections::HashMap<Self::Input, Option<String>> {
                Self::inputs().iter().filter_map(|(name, input)| {
                    let value = ::actions::get_input_from::<String>(env, name);
                    let default = input.default.map(|s| s.to_string());
                    match std::str::FromStr::from_str(&name) {
                        Ok(variant) => Some((variant, value.unwrap().or(default))),
                        Err(_) => None,
                    }
                }).collect()
            }
        }
    };

    let input_impl_methods = input_impl_methods(&manifest);
    let input_impl = quote! {
        #[allow(clippy::all)]
        impl #impl_generics #struct_name #ty_generics #where_clause {
            #input_impl_methods
        }
    };

    let tokens = quote! {
        #input_enum
        #input_impl
        #parse_impl
    };
    // eprintln!("{}", pretty_print(&tokens));
    tokens.into()
}

fn input_impl_methods(manifest: &Manifest) -> TokenStream {
    let Manifest {
        name,
        description,
        author,
        ..
    } = manifest;

    let derived_methods: TokenStream = manifest
        .inputs
        .keys()
        .map(|name| {
            let fn_name = ident::parse_str(name);
            quote! {
                pub fn #fn_name<T>() -> Result<Option<T>, <T as ::actions::ParseInput>::Error>
                where T: ::actions::ParseInput {
                    ::actions::get_input::<T>(#name)
                }
            }
        })
        .collect();

    let inputs: Vec<_> = manifest
        .inputs
        .iter()
        .map(|(name, input)| {
            let description = quote_option(&input.description);
            let deprecation_message = quote_option(&input.deprecation_message);
            let r#default = quote_option(&input.default);
            let required = quote_option(&input.required);
            quote! {
                (#name, ::actions::Input {
                    description: #description,
                    deprecation_message: #deprecation_message,
                    default: #r#default,
                    required: #required,
                })
            }
        })
        .collect();
    // eprintln!("{}", pretty_print(&quote! { vec![#(#inputs,)*]; }));

    quote! {
        /// Inputs of this action.
        pub fn inputs() -> ::std::collections::HashMap<
            &'static str, ::actions::Input<'static>
        > {
            static inputs: &'static [(&'static str, ::actions::Input<'static>)] = &[
                #(#inputs,)*
            ];
            inputs.iter().cloned().collect()
        }

        /// Description of this action.
        pub fn description() -> &'static str {
            #description
        }

        /// Name of this action.
        pub fn name() -> &'static str {
            #name
        }

        /// Author of this trait.
        pub fn author() -> &'static str {
            #author
        }

        #derived_methods
    }
}

#[allow(dead_code)]
fn pretty_print(tokens: &TokenStream) -> String {
    let _file = syn::parse_file(&tokens.to_string()).unwrap();
    // TODO: this will not work until prettyplease updates to syn 2+
    // prettyplease::unparse(&file);
    tokens.to_string()
}