Skip to content

Commit

Permalink
Merge #445
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
bors[bot] and amaihoefner authored Mar 28, 2023
2 parents f454b02 + ca5ade2 commit 9787c57
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 37 deletions.
1 change: 0 additions & 1 deletion examples/web_app/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub struct Crate {
version: String,
}

// Implement the Document trait so that we can use our struct with Meilisearch
fn get_readable_download_count(this: &Map<String, Value>) -> String {
if let Some(downloads) = this["downloads"].as_f64() {
if downloads < 1000.0 {
Expand Down
16 changes: 9 additions & 7 deletions meilisearch-index-setting-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use quote::quote;
use syn::parse_macro_input;
use syn::spanned::Spanned;

#[proc_macro_derive(Document, attributes(document))]
#[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);

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

let struct_ident = &ast.ident;

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

fn get_document_implementation(
fn get_index_config_implementation(
struct_ident: &syn::Ident,
fields: &syn::Fields,
) -> proc_macro2::TokenStream {
Expand Down Expand Up @@ -111,7 +111,9 @@ fn get_document_implementation(

quote! {
#[::meilisearch_sdk::macro_helper::async_trait]
impl ::meilisearch_sdk::documents::Document for #struct_ident {
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
Expand Down Expand Up @@ -142,7 +144,7 @@ fn extract_all_attr_values(
for attr in attrs {
match attr.parse_meta() {
std::result::Result::Ok(syn::Meta::List(list)) => {
if !list.path.is_ident("document") {
if !list.path.is_ident("index_config") {
continue;
}
for token_stream in attr.tokens.clone().into_iter() {
Expand Down Expand Up @@ -187,7 +189,7 @@ fn extract_all_attr_values(
syn::Error::new(
ident.span(),
format!(
"Property `{ident}` does not exist for type `document`"
"Property `{ident}` does not exist for type `index_config`"
),
)
.to_compile_error(),
Expand Down
60 changes: 33 additions & 27 deletions src/documents.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use async_trait::async_trait;
use serde::{de::DeserializeOwned, Deserialize, Serialize};

/// Derive the [`Document`](crate::documents::Document) trait.
/// Derive the [`IndexConfig`](crate::documents::IndexConfig) trait.
///
/// ## Field attribute
/// Use the `#[document(..)]` field attribute to generate the correct settings
/// Use the `#[index_config(..)]` field attribute to generate the correct settings
/// for each field. The available parameters are:
/// - `primary_key` (can only be used once)
/// - `distinct` (can only be used once)
Expand All @@ -19,22 +19,22 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize};
/// ## Sample usage:
/// ```
/// use serde::{Serialize, Deserialize};
/// use meilisearch_sdk::documents::Document;
/// use meilisearch_sdk::documents::IndexConfig;
/// use meilisearch_sdk::settings::Settings;
/// use meilisearch_sdk::indexes::Index;
/// use meilisearch_sdk::client::Client;
///
/// #[derive(Serialize, Deserialize, Document)]
/// #[derive(Serialize, Deserialize, IndexConfig)]
/// struct Movie {
/// #[document(primary_key)]
/// #[index_config(primary_key)]
/// movie_id: u64,
/// #[document(displayed, searchable)]
/// #[index_config(displayed, searchable)]
/// title: String,
/// #[document(displayed)]
/// #[index_config(displayed)]
/// description: String,
/// #[document(filterable, sortable, displayed)]
/// #[index_config(filterable, sortable, displayed)]
/// release_date: String,
/// #[document(filterable, displayed)]
/// #[index_config(filterable, displayed)]
/// genres: Vec<String>,
/// }
///
Expand All @@ -45,16 +45,22 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize};
/// let index: Index = Movie::generate_index(&client).await.unwrap();
/// }
/// ```
pub use meilisearch_index_setting_macro::Document;
pub use meilisearch_index_setting_macro::IndexConfig;

use crate::settings::Settings;
use crate::tasks::Task;
use crate::Client;
use crate::{errors::Error, indexes::Index};

#[async_trait]
pub trait Document {
pub trait IndexConfig {
const INDEX_STR: &'static str;

fn index(client: &Client) -> Index {
client.index(Self::INDEX_STR)
}
fn generate_settings() -> Settings;
async fn generate_index(client: &crate::client::Client) -> Result<Index, Task>;
async fn generate_index(client: &Client) -> Result<Index, Task>;
}

#[derive(Debug, Clone, Deserialize)]
Expand Down Expand Up @@ -296,7 +302,7 @@ impl<'a> DocumentsQuery<'a> {
mod tests {
use super::*;
use crate::{client::*, indexes::*};
use ::meilisearch_sdk::documents::Document;
use ::meilisearch_sdk::documents::IndexConfig;
use meilisearch_test_macro::meilisearch_test;
use serde::{Deserialize, Serialize};

Expand All @@ -307,24 +313,24 @@ mod tests {
}

#[allow(unused)]
#[derive(Document)]
#[derive(IndexConfig)]
struct MovieClips {
#[document(primary_key)]
#[index_config(primary_key)]
movie_id: u64,
#[document(distinct)]
#[index_config(distinct)]
owner: String,
#[document(displayed, searchable)]
#[index_config(displayed, searchable)]
title: String,
#[document(displayed)]
#[index_config(displayed)]
description: String,
#[document(filterable, sortable, displayed)]
#[index_config(filterable, sortable, displayed)]
release_date: String,
#[document(filterable, displayed)]
#[index_config(filterable, displayed)]
genres: Vec<String>,
}

#[allow(unused)]
#[derive(Document)]
#[derive(IndexConfig)]
struct VideoClips {
video_id: u64,
}
Expand Down Expand Up @@ -443,17 +449,17 @@ mod tests {

Ok(())
}
#[derive(Serialize, Deserialize, Document)]
#[derive(Serialize, Deserialize, IndexConfig)]
struct Movie {
#[document(primary_key)]
#[index_config(primary_key)]
movie_id: u64,
#[document(displayed, searchable)]
#[index_config(displayed, searchable)]
title: String,
#[document(displayed)]
#[index_config(displayed)]
description: String,
#[document(filterable, sortable, displayed)]
#[index_config(filterable, sortable, displayed)]
release_date: String,
#[document(filterable, displayed)]
#[index_config(filterable, displayed)]
genres: Vec<String>,
}
}
2 changes: 1 addition & 1 deletion src/dumps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//!
//! 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.
//!
//! 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).
//! During a dump import, all indexes contained in the indicated `.dump` file are imported along with their associated documents and [settings](crate::settings::Settings).
//! Any existing [index](crate::indexes::Index) with the same uid as an index in the dump file will be overwritten.
//!
//! Dump imports are [performed at launch](https://docs.meilisearch.com/reference/features/configuration.html#import-dump) using an option.
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ mod utils;
pub use client::*;

#[cfg(test)]
/// Support for the `Document` derive proc macro in the crate's tests
/// Support for the `IndexConfig` derive proc macro in the crate's tests
extern crate self as meilisearch_sdk;
/// 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.
#[doc(hidden)]
Expand Down

0 comments on commit 9787c57

Please sign in to comment.