meilisearch / meilisearch-rust

Rust wrapper for the Meilisearch API.

Home Page:https://www.meilisearch.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sort params are hard to generalize

0xpr03 opened this issue · comments

commented

Description
Let's say you have a bunch of indices that all need pagination and sorting.
You could try to use generic structs + serdes flatten and then write a sub function that does all the sort + page query params.
Problem is you need to pass a &[&str] to the sort param. So if you have some kind of sort: Vec<String> then you have to first do sort.iter().map(|v|v.as_ref()).collect(). But this a Vec<&str>. Meaning this has to live for as long as the query exists, thus you can't do something like

fn handle_basics(params: &BaseParams, query: &mut SearchQuery) {
 let c: Vec<_> = params.sort_by.iter().map(|v|v.as_str()).collect();
 query.sort = Some(&c); // needs &[&str]
 // now c would get leaked, creating a lifetime error, c's Vec has to outlive this..
}
fn handle_search(query: BaseParam<Entity>) {
let mut query =...
handle_basics(&params, &mut query);
[..]

Basic example
see above

Other
I'm not sure what the best approach is here. Only solution would be to let SeachQuery own the sort Vec. (sort: Vec<&str>)
Maybe also something like impl Iterator<T> where T: AsRef<str>.

Edit: updated variable names that I forgot to rename correctly from my MVP.

Hey @0xpr03, indeed it's not the best usage :/ Adding an implementation impl Iterator<T> where T: AsRef<str>. is a good option for me to fix this