@@ -3,9 +3,20 @@ use proc_macro2::Ident;
33use quote:: { quote, ToTokens } ;
44use syn:: { parse_macro_input, Data , DataStruct , DeriveInput , Fields } ;
55
6- /// A list of supported maps which can be converted to a dictionary for
7- /// the [`redis_module::InfoContext`].
8- const SUPPORTED_MAPS_LIST : [ & str ; 2 ] = [ "BTreeMap" , "HashMap" ] ;
6+ struct SupportedMaps ;
7+ impl SupportedMaps {
8+ /// A list of supported maps which can be converted to a dictionary for
9+ /// the [`redis_module::InfoContext`].
10+ const ALL : [ & str ; 2 ] = [ "BTreeMap" , "HashMap" ] ;
11+
12+ /// Returns `true` if the `type_string` provided is a type of a map
13+ /// supported by the [`crate::InfoSection`].
14+ pub fn supports ( type_string : & str ) -> bool {
15+ Self :: ALL
16+ . iter ( )
17+ . any ( |m| type_string. contains ( & m. to_lowercase ( ) ) )
18+ }
19+ }
920
1021/// Generate a [`From`] implementation for this struct so that it is
1122/// possible to generate a [`redis_module::InfoContext`] information
@@ -18,19 +29,13 @@ fn struct_info_section(struct_name: Ident, struct_data: DataStruct) -> TokenStre
1829 }
1930 } ;
2031
21- let supported_maps: Vec < String > = SUPPORTED_MAPS_LIST
22- . iter ( )
23- . map ( |s| s. to_lowercase ( ) )
24- . collect ( ) ;
25-
26- let is_supported_map = |ty : & str | -> bool { supported_maps. iter ( ) . any ( |m| ty. contains ( m) ) } ;
27-
2832 let fields = fields
2933 . named
3034 . into_iter ( )
3135 . map ( |v| {
32- let is_dictionary =
33- is_supported_map ( & v. ty . clone ( ) . into_token_stream ( ) . to_string ( ) . to_lowercase ( ) ) ;
36+ let is_dictionary = SupportedMaps :: supports (
37+ & v. ty . clone ( ) . into_token_stream ( ) . to_string ( ) . to_lowercase ( ) ,
38+ ) ;
3439 let name = v. ident . ok_or (
3540 "Structs with unnamed fields are not supported by the InfoSection." . to_owned ( ) ,
3641 ) ?;
0 commit comments