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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
use std::collections::HashSet;

use convert_case::{Case, Casing};
use proc_macro2::TokenTree;
use quote::quote;
use syn::parse_macro_input;
use syn::spanned::Spanned;

#[proc_macro_derive(IndexConfig, attributes(index_config))]
pub fn generate_index_settings(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let ast = parse_macro_input!(input as syn::DeriveInput);

    let fields: &syn::Fields = match ast.data {
        syn::Data::Struct(ref data) => &data.fields,
        _ => {
            return proc_macro::TokenStream::from(
                syn::Error::new(ast.ident.span(), "Applicable only to struct").to_compile_error(),
            );
        }
    };

    let struct_ident = &ast.ident;

    let index_config_implementation = get_index_config_implementation(struct_ident, fields);
    proc_macro::TokenStream::from(quote! {
        #index_config_implementation
    })
}

fn get_index_config_implementation(
    struct_ident: &syn::Ident,
    fields: &syn::Fields,
) -> proc_macro2::TokenStream {
    let mut attribute_set: std::collections::HashSet<String> = std::collections::HashSet::new();
    let mut primary_key_attribute: String = "".to_string();
    let mut distinct_key_attribute: String = "".to_string();
    let mut displayed_attributes: Vec<String> = vec![];
    let mut searchable_attributes: Vec<String> = vec![];
    let mut filterable_attributes: Vec<String> = vec![];
    let mut sortable_attributes: Vec<String> = vec![];
    let valid_attribute_names = std::collections::HashSet::from([
        "displayed",
        "searchable",
        "filterable",
        "sortable",
        "primary_key",
        "distinct",
    ]);

    let index_name = struct_ident
        .to_string()
        .from_case(Case::UpperCamel)
        .to_case(Case::Snake);

    for field in fields {
        let attribute_list_result =
            extract_all_attr_values(&field.attrs, &mut attribute_set, &valid_attribute_names);

        match attribute_list_result {
            Ok(attribute_list) => {
                for attribute in attribute_list {
                    match attribute.as_str() {
                        "displayed" => {
                            displayed_attributes.push(field.ident.clone().unwrap().to_string())
                        }
                        "searchable" => {
                            searchable_attributes.push(field.ident.clone().unwrap().to_string())
                        }
                        "filterable" => {
                            filterable_attributes.push(field.ident.clone().unwrap().to_string())
                        }
                        "sortable" => {
                            sortable_attributes.push(field.ident.clone().unwrap().to_string())
                        }
                        "primary_key" => {
                            primary_key_attribute = field.ident.clone().unwrap().to_string()
                        }
                        "distinct" => {
                            distinct_key_attribute = field.ident.clone().unwrap().to_string()
                        }
                        _ => {}
                    }
                }
            }
            Err(e) => {
                return e;
            }
        }
    }

    let primary_key_token: proc_macro2::TokenStream = if primary_key_attribute.is_empty() {
        quote! {
            ::std::option::Option::None
        }
    } else {
        quote! {
            ::std::option::Option::Some(#primary_key_attribute)
        }
    };

    let display_attr_tokens =
        get_settings_token_for_list(&displayed_attributes, "with_displayed_attributes");
    let sortable_attr_tokens =
        get_settings_token_for_list(&sortable_attributes, "with_sortable_attributes");
    let filterable_attr_tokens =
        get_settings_token_for_list(&filterable_attributes, "with_filterable_attributes");
    let searchable_attr_tokens =
        get_settings_token_for_list(&searchable_attributes, "with_searchable_attributes");
    let distinct_attr_token =
        get_settings_token_for_string(&distinct_key_attribute, "with_distinct_attribute");

    quote! {
        #[::meilisearch_sdk::macro_helper::async_trait]
        impl ::meilisearch_sdk::documents::IndexConfig for #struct_ident {
            const INDEX_STR: &'static str = #index_name;

            fn generate_settings() -> ::meilisearch_sdk::settings::Settings {
            ::meilisearch_sdk::settings::Settings::new()
            #display_attr_tokens
            #sortable_attr_tokens
            #filterable_attr_tokens
            #searchable_attr_tokens
            #distinct_attr_token
        }

         async fn generate_index(client: &::meilisearch_sdk::client::Client) -> std::result::Result<::meilisearch_sdk::indexes::Index, ::meilisearch_sdk::tasks::Task> {
            return client.create_index(#index_name, #primary_key_token)
                .await.unwrap()
                .wait_for_completion(&client, ::std::option::Option::None, ::std::option::Option::None)
                .await.unwrap()
                .try_make_index(&client);
            }
        }
    }
}

fn extract_all_attr_values(
    attrs: &[syn::Attribute],
    attribute_set: &mut std::collections::HashSet<String>,
    valid_attribute_names: &std::collections::HashSet<&str>,
) -> std::result::Result<Vec<String>, proc_macro2::TokenStream> {
    let mut attribute_names: Vec<String> = vec![];
    let mut local_attribute_set: std::collections::HashSet<String> = HashSet::new();
    for attr in attrs {
        match attr.parse_meta() {
            std::result::Result::Ok(syn::Meta::List(list)) => {
                if !list.path.is_ident("index_config") {
                    continue;
                }
                for token_stream in attr.tokens.clone().into_iter() {
                    if let TokenTree::Group(group) = token_stream {
                        for token in group.stream() {
                            match token {
                                TokenTree::Punct(punct) => validate_punct(&punct)?,
                                TokenTree::Ident(ident) => {
                                    if ident == "primary_key"
                                        && attribute_set.contains("primary_key")
                                    {
                                        return std::result::Result::Err(
                                            syn::Error::new(
                                                ident.span(),
                                                "`primary_key` already exists",
                                            )
                                            .to_compile_error(),
                                        );
                                    }
                                    if ident == "distinct" && attribute_set.contains("distinct") {
                                        return std::result::Result::Err(
                                            syn::Error::new(
                                                ident.span(),
                                                "`distinct` already exists",
                                            )
                                            .to_compile_error(),
                                        );
                                    }

                                    if local_attribute_set.contains(ident.to_string().as_str()) {
                                        return std::result::Result::Err(
                                            syn::Error::new(
                                                ident.span(),
                                                format!("`{ident}` already exists for this field"),
                                            )
                                            .to_compile_error(),
                                        );
                                    }

                                    if !valid_attribute_names.contains(ident.to_string().as_str()) {
                                        return std::result::Result::Err(
                                                syn::Error::new(
                                                    ident.span(),
                                                    format!(
                                                        "Property `{ident}` does not exist for type `index_config`"
                                                    ),
                                                )
                                                    .to_compile_error(),
                                            );
                                    }
                                    attribute_names.push(ident.to_string());
                                    attribute_set.insert(ident.to_string());
                                    local_attribute_set.insert(ident.to_string());
                                }
                                _ => {
                                    return std::result::Result::Err(
                                        syn::Error::new(attr.span(), "Invalid parsing".to_string())
                                            .to_compile_error(),
                                    );
                                }
                            }
                        }
                    }
                }
            }
            std::result::Result::Err(e) => {
                for token_stream in attr.tokens.clone().into_iter() {
                    if let TokenTree::Group(group) = token_stream {
                        for token in group.stream() {
                            if let TokenTree::Punct(punct) = token {
                                validate_punct(&punct)?
                            }
                        }
                    }
                }
                return std::result::Result::Err(
                    syn::Error::new(attr.span(), e.to_string()).to_compile_error(),
                );
            }
            _ => {}
        }
    }
    std::result::Result::Ok(attribute_names)
}

fn validate_punct(punct: &proc_macro2::Punct) -> std::result::Result<(), proc_macro2::TokenStream> {
    if punct.as_char() == ',' && punct.spacing() == proc_macro2::Spacing::Alone {
        return std::result::Result::Ok(());
    }
    std::result::Result::Err(
        syn::Error::new(punct.span(), "`,` expected".to_string()).to_compile_error(),
    )
}

fn get_settings_token_for_list(
    field_name_list: &Vec<String>,
    method_name: &str,
) -> proc_macro2::TokenStream {
    let string_attributes = field_name_list.iter().map(|attr| {
        quote! {
            #attr
        }
    });
    let method_ident = syn::Ident::new(method_name, proc_macro2::Span::call_site());

    if !field_name_list.is_empty() {
        quote! {
            .#method_ident([#(#string_attributes),*])
        }
    } else {
        quote! {
            .#method_ident(::std::iter::empty::<&str>())
        }
    }
}

fn get_settings_token_for_string(
    field_name: &String,
    method_name: &str,
) -> proc_macro2::TokenStream {
    let method_ident = syn::Ident::new(method_name, proc_macro2::Span::call_site());

    if !field_name.is_empty() {
        quote! {
            .#method_ident(#field_name)
        }
    } else {
        proc_macro2::TokenStream::new()
    }
}