Skip to content

Commit 9787c57

Browse files
Merge #445
445: Rename `Document` trait to `IndexConfig` and extend with function to retrieve index r=bidoubiwa a=amaihoefner # Pull Request ## Related issue Fixes #444 ## What does this PR do? - rename `Document` trait to `IndexConfig` - add constant `IndexConfig::INDEX_STR` to store name of the index - add `IndexConfig::index` function to provide easier and safer access to index ## PR checklist Please check if your PR fulfills the following requirements: - [ x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [ x] Have you read the contributing guidelines? - [ x] Have you made sure that the title is accurate and descriptive of the changes? Co-authored-by: amaihoefner <[email protected]>
2 parents f454b02 + ca5ade2 commit 9787c57

File tree

5 files changed

+44
-37
lines changed

5 files changed

+44
-37
lines changed

examples/web_app/src/document.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ pub struct Crate {
1313
version: String,
1414
}
1515

16-
// Implement the Document trait so that we can use our struct with Meilisearch
1716
fn get_readable_download_count(this: &Map<String, Value>) -> String {
1817
if let Some(downloads) = this["downloads"].as_f64() {
1918
if downloads < 1000.0 {

meilisearch-index-setting-macro/src/lib.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use quote::quote;
66
use syn::parse_macro_input;
77
use syn::spanned::Spanned;
88

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

@@ -21,13 +21,13 @@ pub fn generate_index_settings(input: proc_macro::TokenStream) -> proc_macro::To
2121

2222
let struct_ident = &ast.ident;
2323

24-
let document_implementation = get_document_implementation(struct_ident, fields);
24+
let index_config_implementation = get_index_config_implementation(struct_ident, fields);
2525
proc_macro::TokenStream::from(quote! {
26-
#document_implementation
26+
#index_config_implementation
2727
})
2828
}
2929

30-
fn get_document_implementation(
30+
fn get_index_config_implementation(
3131
struct_ident: &syn::Ident,
3232
fields: &syn::Fields,
3333
) -> proc_macro2::TokenStream {
@@ -111,7 +111,9 @@ fn get_document_implementation(
111111

112112
quote! {
113113
#[::meilisearch_sdk::macro_helper::async_trait]
114-
impl ::meilisearch_sdk::documents::Document for #struct_ident {
114+
impl ::meilisearch_sdk::documents::IndexConfig for #struct_ident {
115+
const INDEX_STR: &'static str = #index_name;
116+
115117
fn generate_settings() -> ::meilisearch_sdk::settings::Settings {
116118
::meilisearch_sdk::settings::Settings::new()
117119
#display_attr_tokens
@@ -142,7 +144,7 @@ fn extract_all_attr_values(
142144
for attr in attrs {
143145
match attr.parse_meta() {
144146
std::result::Result::Ok(syn::Meta::List(list)) => {
145-
if !list.path.is_ident("document") {
147+
if !list.path.is_ident("index_config") {
146148
continue;
147149
}
148150
for token_stream in attr.tokens.clone().into_iter() {
@@ -187,7 +189,7 @@ fn extract_all_attr_values(
187189
syn::Error::new(
188190
ident.span(),
189191
format!(
190-
"Property `{ident}` does not exist for type `document`"
192+
"Property `{ident}` does not exist for type `index_config`"
191193
),
192194
)
193195
.to_compile_error(),

src/documents.rs

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use async_trait::async_trait;
22
use serde::{de::DeserializeOwned, Deserialize, Serialize};
33

4-
/// Derive the [`Document`](crate::documents::Document) trait.
4+
/// Derive the [`IndexConfig`](crate::documents::IndexConfig) trait.
55
///
66
/// ## Field attribute
7-
/// Use the `#[document(..)]` field attribute to generate the correct settings
7+
/// Use the `#[index_config(..)]` field attribute to generate the correct settings
88
/// for each field. The available parameters are:
99
/// - `primary_key` (can only be used once)
1010
/// - `distinct` (can only be used once)
@@ -19,22 +19,22 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize};
1919
/// ## Sample usage:
2020
/// ```
2121
/// use serde::{Serialize, Deserialize};
22-
/// use meilisearch_sdk::documents::Document;
22+
/// use meilisearch_sdk::documents::IndexConfig;
2323
/// use meilisearch_sdk::settings::Settings;
2424
/// use meilisearch_sdk::indexes::Index;
2525
/// use meilisearch_sdk::client::Client;
2626
///
27-
/// #[derive(Serialize, Deserialize, Document)]
27+
/// #[derive(Serialize, Deserialize, IndexConfig)]
2828
/// struct Movie {
29-
/// #[document(primary_key)]
29+
/// #[index_config(primary_key)]
3030
/// movie_id: u64,
31-
/// #[document(displayed, searchable)]
31+
/// #[index_config(displayed, searchable)]
3232
/// title: String,
33-
/// #[document(displayed)]
33+
/// #[index_config(displayed)]
3434
/// description: String,
35-
/// #[document(filterable, sortable, displayed)]
35+
/// #[index_config(filterable, sortable, displayed)]
3636
/// release_date: String,
37-
/// #[document(filterable, displayed)]
37+
/// #[index_config(filterable, displayed)]
3838
/// genres: Vec<String>,
3939
/// }
4040
///
@@ -45,16 +45,22 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize};
4545
/// let index: Index = Movie::generate_index(&client).await.unwrap();
4646
/// }
4747
/// ```
48-
pub use meilisearch_index_setting_macro::Document;
48+
pub use meilisearch_index_setting_macro::IndexConfig;
4949

5050
use crate::settings::Settings;
5151
use crate::tasks::Task;
52+
use crate::Client;
5253
use crate::{errors::Error, indexes::Index};
5354

5455
#[async_trait]
55-
pub trait Document {
56+
pub trait IndexConfig {
57+
const INDEX_STR: &'static str;
58+
59+
fn index(client: &Client) -> Index {
60+
client.index(Self::INDEX_STR)
61+
}
5662
fn generate_settings() -> Settings;
57-
async fn generate_index(client: &crate::client::Client) -> Result<Index, Task>;
63+
async fn generate_index(client: &Client) -> Result<Index, Task>;
5864
}
5965

6066
#[derive(Debug, Clone, Deserialize)]
@@ -296,7 +302,7 @@ impl<'a> DocumentsQuery<'a> {
296302
mod tests {
297303
use super::*;
298304
use crate::{client::*, indexes::*};
299-
use ::meilisearch_sdk::documents::Document;
305+
use ::meilisearch_sdk::documents::IndexConfig;
300306
use meilisearch_test_macro::meilisearch_test;
301307
use serde::{Deserialize, Serialize};
302308

@@ -307,24 +313,24 @@ mod tests {
307313
}
308314

309315
#[allow(unused)]
310-
#[derive(Document)]
316+
#[derive(IndexConfig)]
311317
struct MovieClips {
312-
#[document(primary_key)]
318+
#[index_config(primary_key)]
313319
movie_id: u64,
314-
#[document(distinct)]
320+
#[index_config(distinct)]
315321
owner: String,
316-
#[document(displayed, searchable)]
322+
#[index_config(displayed, searchable)]
317323
title: String,
318-
#[document(displayed)]
324+
#[index_config(displayed)]
319325
description: String,
320-
#[document(filterable, sortable, displayed)]
326+
#[index_config(filterable, sortable, displayed)]
321327
release_date: String,
322-
#[document(filterable, displayed)]
328+
#[index_config(filterable, displayed)]
323329
genres: Vec<String>,
324330
}
325331

326332
#[allow(unused)]
327-
#[derive(Document)]
333+
#[derive(IndexConfig)]
328334
struct VideoClips {
329335
video_id: u64,
330336
}
@@ -443,17 +449,17 @@ mod tests {
443449

444450
Ok(())
445451
}
446-
#[derive(Serialize, Deserialize, Document)]
452+
#[derive(Serialize, Deserialize, IndexConfig)]
447453
struct Movie {
448-
#[document(primary_key)]
454+
#[index_config(primary_key)]
449455
movie_id: u64,
450-
#[document(displayed, searchable)]
456+
#[index_config(displayed, searchable)]
451457
title: String,
452-
#[document(displayed)]
458+
#[index_config(displayed)]
453459
description: String,
454-
#[document(filterable, sortable, displayed)]
460+
#[index_config(filterable, sortable, displayed)]
455461
release_date: String,
456-
#[document(filterable, displayed)]
462+
#[index_config(filterable, displayed)]
457463
genres: Vec<String>,
458464
}
459465
}

src/dumps.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//!
77
//! During a [dump export](Client::create_dump), all [indexes](crate::indexes::Index) of the current instance are exported—together with their documents and settings—and saved as a single `.dump` file.
88
//!
9-
//! During a dump import, all indexes contained in the indicated `.dump` file are imported along with their associated [documents](crate::documents::Document) and [settings](crate::settings::Settings).
9+
//! During a dump import, all indexes contained in the indicated `.dump` file are imported along with their associated documents and [settings](crate::settings::Settings).
1010
//! Any existing [index](crate::indexes::Index) with the same uid as an index in the dump file will be overwritten.
1111
//!
1212
//! Dump imports are [performed at launch](https://docs.meilisearch.com/reference/features/configuration.html#import-dump) using an option.

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ mod utils;
252252
pub use client::*;
253253

254254
#[cfg(test)]
255-
/// Support for the `Document` derive proc macro in the crate's tests
255+
/// Support for the `IndexConfig` derive proc macro in the crate's tests
256256
extern crate self as meilisearch_sdk;
257257
/// Can't assume that the user of proc_macro will have access to `async_trait` crate. So exporting the `async-trait` crate from `meilisearch_sdk` in a hidden module.
258258
#[doc(hidden)]

0 commit comments

Comments
 (0)