|
| 1 | +//! Proc macros for the `choices` crate. |
| 2 | +
|
| 3 | +#![forbid(unsafe_code)] |
| 4 | +#![deny(missing_docs)] |
| 5 | + |
| 6 | +extern crate proc_macro; |
| 7 | + |
| 8 | +use proc_macro2::TokenStream; |
| 9 | +use proc_macro_error::{abort_call_site, proc_macro_error, set_dummy}; |
| 10 | +use quote::quote; |
| 11 | +use syn::{punctuated::Punctuated, token::Comma, *}; |
| 12 | + |
| 13 | +const BASE_PATH: &str = "config"; |
| 14 | + |
| 15 | +/// Generates the `Choices` impl. |
| 16 | +#[proc_macro_derive(Choices)] |
| 17 | +#[proc_macro_error] |
| 18 | +pub fn choices(input: proc_macro::TokenStream) -> proc_macro::TokenStream { |
| 19 | + let input: DeriveInput = syn::parse(input).unwrap(); |
| 20 | + let gen = impl_choices(&input); |
| 21 | + gen.into() |
| 22 | +} |
| 23 | + |
| 24 | +fn impl_choices(input: &DeriveInput) -> TokenStream { |
| 25 | + use syn::Data::*; |
| 26 | + |
| 27 | + let struct_name = &input.ident; |
| 28 | + |
| 29 | + set_dummy(quote! { |
| 30 | + #[async_trait::async_trait] |
| 31 | + impl ::choices::Choices for #struct_name { |
| 32 | + unimplemented!(); |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + match input.data { |
| 37 | + Struct(DataStruct { |
| 38 | + fields: syn::Fields::Named(ref fields), |
| 39 | + .. |
| 40 | + }) => impl_choices_for_struct(struct_name, &fields.named, &input.attrs), |
| 41 | + _ => abort_call_site!("choices only supports non-tuple structs"), |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +fn impl_choices_for_struct( |
| 46 | + name: &Ident, |
| 47 | + fields: &Punctuated<Field, Comma>, |
| 48 | + _attrs: &[Attribute], |
| 49 | +) -> TokenStream { |
| 50 | + let (macros, implementation, choices) = gen_choices_warp(fields); |
| 51 | + |
| 52 | + quote! { |
| 53 | + #macros |
| 54 | + |
| 55 | + #[allow(unused_variables)] |
| 56 | + #[allow(unknown_lints)] |
| 57 | + #[allow( |
| 58 | + clippy::style, |
| 59 | + clippy::complexity, |
| 60 | + clippy::pedantic, |
| 61 | + clippy::restriction, |
| 62 | + clippy::perf, |
| 63 | + clippy::deprecated, |
| 64 | + clippy::nursery, |
| 65 | + clippy::cargo |
| 66 | + )] |
| 67 | + #[deny(clippy::correctness)] |
| 68 | + #[allow(dead_code, unreachable_code)] |
| 69 | + impl #name { |
| 70 | + #implementation |
| 71 | + } |
| 72 | + |
| 73 | + #[allow(unused_variables)] |
| 74 | + #[allow(unknown_lints)] |
| 75 | + #[allow( |
| 76 | + clippy::style, |
| 77 | + clippy::complexity, |
| 78 | + clippy::pedantic, |
| 79 | + clippy::restriction, |
| 80 | + clippy::perf, |
| 81 | + clippy::deprecated, |
| 82 | + clippy::nursery, |
| 83 | + clippy::cargo |
| 84 | + )] |
| 85 | + #[deny(clippy::correctness)] |
| 86 | + #[allow(dead_code, unreachable_code)] |
| 87 | + #[async_trait::async_trait] |
| 88 | + impl ::choices::Choices for #name { |
| 89 | + #choices |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +fn gen_choices_warp(fields: &Punctuated<Field, Comma>) -> (TokenStream, TokenStream, TokenStream) { |
| 95 | + let fields_populators = fields.iter().map(|field| { |
| 96 | + let field_ident = field |
| 97 | + .ident |
| 98 | + .as_ref() |
| 99 | + .expect("unnamed fields are not supported!"); |
| 100 | + let field_name = field_ident.to_string(); |
| 101 | + Some(quote! { |
| 102 | + warp::path!(#BASE_PATH / #field_name).map(move || format!("{}", $self.#field_ident.body_string()) ) |
| 103 | + }) |
| 104 | + }); |
| 105 | + |
| 106 | + let index_string = compute_index_string(fields); |
| 107 | + |
| 108 | + let macro_tk = quote! { |
| 109 | + macro_rules! create_filter { |
| 110 | + ($self:ident) => {{ |
| 111 | + use warp::Filter; |
| 112 | + #[allow(unused_imports)] |
| 113 | + use choices::ChoicesOutput; |
| 114 | + |
| 115 | + let index = warp::path(#BASE_PATH).map(|| #index_string); |
| 116 | + warp::get().and( |
| 117 | + index.and(warp::path::end()) |
| 118 | + #( .or(#fields_populators) )* |
| 119 | + ) |
| 120 | + }}; |
| 121 | + } |
| 122 | + }; |
| 123 | + |
| 124 | + let implementation_tk = quote! { |
| 125 | + /// If you want more control over the http server instance you can use this |
| 126 | + /// function to retrieve the configuration's `warp::Filter`. |
| 127 | + fn filter(&'static self) -> warp::filters::BoxedFilter<(impl warp::Reply,)> { |
| 128 | + use warp::Filter; |
| 129 | + create_filter!(self).boxed() |
| 130 | + } |
| 131 | + }; |
| 132 | + |
| 133 | + let trait_tk = quote! { |
| 134 | + async fn run<T: Into<std::net::SocketAddr> + Send>(&'static self, addr: T) { |
| 135 | + let filter = create_filter!(self); |
| 136 | + warp::serve(filter).run(addr).await |
| 137 | + } |
| 138 | + }; |
| 139 | + |
| 140 | + (macro_tk, implementation_tk, trait_tk) |
| 141 | +} |
| 142 | + |
| 143 | +fn compute_index_string(fields: &Punctuated<Field, Comma>) -> String { |
| 144 | + let mut index = "Available configuration options:\n".to_string(); |
| 145 | + fields.iter().for_each(|field| { |
| 146 | + let field_ident = field |
| 147 | + .ident |
| 148 | + .as_ref() |
| 149 | + .expect("unnamed fields are not supported!"); |
| 150 | + let type_name = compute_type_string(&field.ty); |
| 151 | + index += &format!(" - {}: {}\n", &field_ident.to_string(), type_name); |
| 152 | + }); |
| 153 | + index |
| 154 | +} |
| 155 | + |
| 156 | +fn compute_type_string(ty: &Type) -> String { |
| 157 | + match ty { |
| 158 | + Type::Path(ref typepath) if typepath.qself.is_none() => typepath |
| 159 | + .path |
| 160 | + .segments |
| 161 | + .iter() |
| 162 | + .into_iter() |
| 163 | + .fold(String::new(), |mut acc, v| { |
| 164 | + acc.push_str(&v.ident.to_string()); |
| 165 | + if let PathArguments::AngleBracketed(ref arguments) = &v.arguments { |
| 166 | + if arguments.args.len() > 1 { |
| 167 | + abort_call_site!( |
| 168 | + "generic types parameterized on more than one type are not supported" |
| 169 | + ) |
| 170 | + } |
| 171 | + if let Some(args) = arguments.args.first() { |
| 172 | + if let GenericArgument::Type(inner_type) = args { |
| 173 | + acc.push_str( |
| 174 | + &("<".to_owned() + &compute_type_string(inner_type) + ">"), |
| 175 | + ); |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + acc |
| 180 | + }), |
| 181 | + _ => abort_call_site!("choices supports only simple types (syn::Type::Path) for fields"), |
| 182 | + } |
| 183 | +} |
0 commit comments