-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Making indexes generic over the contained document type #143
Comments
@Mubelotix Could be better to create a struct Book that impl a trait Document to retrieve the key from Meilisearch? Like this? From the library: trait Document {
fn key(self) -> &str;
} From the project: struct Book {
let foo: String
}
impl Document for Book {
fn key(self) -> &str {
return "books"
}
} It's possible to automatically do: let books = client.get_index::<Book>().await?; |
@ppamorim Interesting idea, but that means you would not be able to have multiple indexes containing the same document type. |
@Mubelotix For this case, make let books = client.get_index::<Book>("Pedro_Book").await?; Since it has potential to be a exceptional circumstance. |
Yes that would work but I think that adding this method to the Document trait is an unnecessary abstraction. It breaks the object hierarchy: an Index contains Documents, a Document cannot choose its Index. A Document should always be handled by its Index and does not need to be aware of its name. |
If this could be possible or anything similar... let books = client.get_index::<Book::with_key("Pedro_Book")>().await?; By retrieving the key of the Book above, it returns |
That would result in a compiler error. Rust expects a type but this is a function call. |
I know, that's a hypothetical case if we could do this in Rust. |
But I still think that the developer should duplicate the structs and name them correctly, it doesn't matter if the struct is similar to another struct. Later on this can be a problem. |
Hello @Mubelotix, |
I think the way this library handles
Document
types can be improved.Current implementation
The type of the
Document
must be explicitly specified at each request.That is very flexible but do we really need flexibility here? The type of the document will (and should) almost never change at runtime.
The current solution may lead to bugs where the actual type of the
Index
does not match the specified type of the request.Solution
Index
should be generic over its containedDocument
type. That would force the programmer to specify the type of the documents at the index creation.That makes the bug described above impossible and takes advantage of Rust's type checks and inference.
Note: We could also add an
execute_dynamic
method allowing the programmer to overwrite the type of the index (for this request only).The text was updated successfully, but these errors were encountered: