|
| 1 | +use proc_macro::TokenStream; |
| 2 | +use proc_macro2::Ident; |
| 3 | +use quote::{quote, ToTokens}; |
| 4 | +use syn::{parse_macro_input, Data, DataStruct, DeriveInput, Fields}; |
| 5 | + |
| 6 | +/// Returns `true` if the `type_string` provided is a type of a map |
| 7 | +/// supported by the [`crate::InfoSection`]. |
| 8 | +pub fn is_supported_map(type_string: &str) -> bool { |
| 9 | + /// A list of supported maps which can be converted to a dictionary for |
| 10 | + /// the [`redis_module::InfoContext`]. |
| 11 | + const ALL: [&str; 2] = ["BTreeMap", "HashMap"]; |
| 12 | + |
| 13 | + ALL.iter().any(|m| type_string.contains(&m.to_lowercase())) |
| 14 | +} |
| 15 | + |
| 16 | +/// Generate a [`From`] implementation for this struct so that it is |
| 17 | +/// possible to generate a [`redis_module::InfoContext`] information |
| 18 | +/// from it. |
| 19 | +/// |
| 20 | +/// A struct is compatible to be used with [`crate::InfoSection`] when |
| 21 | +/// it has fields, whose types are convertible to |
| 22 | +/// [`redis_module::InfoContextBuilderFieldTopLevelValue`] and (for |
| 23 | +/// the dictionaries) if it has fields which are compatible maps of |
| 24 | +/// objects, where a key is a [`String`] and a value is any type |
| 25 | +/// convertible to |
| 26 | +/// [`redis_module::InfoContextBuilderFieldBottomLevelValue`]. |
| 27 | +fn struct_info_section(struct_name: Ident, struct_data: DataStruct) -> TokenStream { |
| 28 | + let fields = match struct_data.fields { |
| 29 | + Fields::Named(f) => f, |
| 30 | + _ => { |
| 31 | + return quote! {compile_error!("The InfoSection can only be derived for structs with named fields.")}.into() |
| 32 | + } |
| 33 | + }; |
| 34 | + |
| 35 | + let fields = fields |
| 36 | + .named |
| 37 | + .into_iter() |
| 38 | + .map(|v| { |
| 39 | + let is_dictionary = |
| 40 | + is_supported_map(&v.ty.clone().into_token_stream().to_string().to_lowercase()); |
| 41 | + let name = v.ident.ok_or( |
| 42 | + "Structs with unnamed fields are not supported by the InfoSection.".to_owned(), |
| 43 | + )?; |
| 44 | + Ok((is_dictionary, name)) |
| 45 | + }) |
| 46 | + .collect::<Result<Vec<_>, String>>(); |
| 47 | + |
| 48 | + let section_key_fields: Vec<_> = match fields { |
| 49 | + Ok(ref f) => f.iter().filter(|i| !i.0).map(|i| i.1.clone()).collect(), |
| 50 | + Err(e) => return quote! {compile_error!(#e)}.into(), |
| 51 | + }; |
| 52 | + |
| 53 | + let section_dictionary_fields: Vec<_> = match fields { |
| 54 | + Ok(f) => f.iter().filter(|i| i.0).map(|i| i.1.clone()).collect(), |
| 55 | + Err(e) => return quote! {compile_error!(#e)}.into(), |
| 56 | + }; |
| 57 | + |
| 58 | + let key_fields_names: Vec<_> = section_key_fields.iter().map(|v| v.to_string()).collect(); |
| 59 | + |
| 60 | + let dictionary_fields_names: Vec<_> = section_dictionary_fields |
| 61 | + .iter() |
| 62 | + .map(|v| v.to_string()) |
| 63 | + .collect(); |
| 64 | + |
| 65 | + quote! { |
| 66 | + impl From<#struct_name> for redis_module::OneInfoSectionData { |
| 67 | + fn from(val: #struct_name) -> redis_module::OneInfoSectionData { |
| 68 | + let section_name = stringify!(#struct_name).to_owned(); |
| 69 | + |
| 70 | + let fields = vec![ |
| 71 | + // The section's key => value pairs. |
| 72 | + #(( |
| 73 | + #key_fields_names.to_owned(), |
| 74 | + redis_module::InfoContextBuilderFieldTopLevelValue::from(val.#section_key_fields) |
| 75 | + ), )* |
| 76 | + |
| 77 | + // The dictionaries within this section. |
| 78 | + #(( |
| 79 | + #dictionary_fields_names.to_owned(), |
| 80 | + redis_module::InfoContextBuilderFieldTopLevelValue::Dictionary { |
| 81 | + name: #dictionary_fields_names.to_owned(), |
| 82 | + fields: redis_module::InfoContextFieldBottomLevelData( |
| 83 | + val.#section_dictionary_fields |
| 84 | + .into_iter() |
| 85 | + .map(|d| d.into()) |
| 86 | + .collect()), |
| 87 | + } |
| 88 | + ), )* |
| 89 | + ]; |
| 90 | + (section_name, fields) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + .into() |
| 95 | +} |
| 96 | + |
| 97 | +/// Implementation for the [`crate::info_section`] derive macro. |
| 98 | +/// Runs the relevant code generation base on the element |
| 99 | +/// the macro was used on. Currently supports `struct`s only. |
| 100 | +pub fn info_section(item: TokenStream) -> TokenStream { |
| 101 | + let input: DeriveInput = parse_macro_input!(item); |
| 102 | + let ident = input.ident; |
| 103 | + match input.data { |
| 104 | + Data::Struct(s) => struct_info_section(ident, s), |
| 105 | + _ => { |
| 106 | + quote! {compile_error!("The InfoSection derive can only be used with structs.")}.into() |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments