Skip to content

Commit 09210c3

Browse files
committed
Refactor filtering of tasks
1 parent 7e6b964 commit 09210c3

File tree

3 files changed

+35
-31
lines changed

3 files changed

+35
-31
lines changed

.code-samples.meilisearch.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,27 +109,27 @@ get_all_tasks_1: |-
109109
.await
110110
.unwrap();
111111
get_all_tasks_filtering_1: |-
112-
let mut query = TasksQuery::new(&client)
112+
let mut query = TasksSearchQuery::new(&client)
113113
.with_index_uid(["movies"])
114114
.execute()
115115
.await
116116
.unwrap();
117117
get_all_tasks_filtering_2: |-
118-
let mut query = TasksQuery::new(&client)
118+
let mut query = TasksSearchQuery::new(&client)
119119
.with_status(["succeeded", "failed"])
120120
.with_type(["documentAdditionOrUpdate"])
121121
.execute()
122122
.await
123123
.unwrap();
124124
get_all_tasks_paginating_1: |-
125-
let mut query = TasksQuery::new(&client)
125+
let mut query = TasksSearchQuery::new(&client)
126126
.with_limit(2)
127127
.with_from(10)
128128
.execute()
129129
.await
130130
.unwrap();
131131
get_all_tasks_paginating_2: |-
132-
let mut query = TasksQuery::new(&client)
132+
let mut query = TasksSearchQuery::new(&client)
133133
.with_limit(2)
134134
.from(8)
135135
.execute()

src/client.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
key::{Key, KeyBuilder, KeyUpdater, KeysQuery, KeysResults},
55
request::*,
66
task_info::TaskInfo,
7-
tasks::{Task, TasksQuery, TasksResults},
7+
tasks::{Task, TasksResults, TasksSearchQuery},
88
utils::async_sleep,
99
};
1010
use serde::Deserialize;
@@ -748,11 +748,11 @@ impl Client {
748748
/// let tasks = client.get_tasks_with(&query).await.unwrap();
749749
/// # });
750750
/// ```
751-
pub async fn get_tasks_with<T>(
751+
pub async fn get_tasks_with(
752752
&self,
753-
tasks_query: &TasksQuery<'_, T>,
753+
tasks_query: &TasksSearchQuery<'_>,
754754
) -> Result<TasksResults, Error> {
755-
let tasks = request::<&TasksQuery<T>, TasksResults>(
755+
let tasks = request::<&TasksSearchQuery, TasksResults>(
756756
&format!("{}/tasks", self.host),
757757
&self.api_key,
758758
Method::Get(tasks_query),

src/tasks.rs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use serde::{Deserialize, Deserializer, Serialize};
2-
use std::{marker::PhantomData, time::Duration};
2+
use std::time::Duration;
33
use time::OffsetDateTime;
44

55
use crate::{
@@ -400,10 +400,17 @@ impl AsRef<u32> for Task {
400400
}
401401

402402
#[derive(Debug, Serialize, Clone)]
403-
pub enum TasksPagination {}
403+
pub struct TasksPagination {
404+
// Maximum number of tasks to return
405+
#[serde(skip_serializing_if = "Option::is_none")]
406+
limit: Option<u32>,
407+
// The first task uid that should be returned
408+
#[serde(skip_serializing_if = "Option::is_none")]
409+
from: Option<u32>,
410+
}
404411

405412
#[derive(Debug, Serialize, Clone)]
406-
pub enum TasksDefault {}
413+
pub struct TasksDefault {}
407414

408415
pub type TasksSearchQuery<'a> = TasksQuery<'a, TasksPagination>;
409416
pub type TasksCancelQuery<'a> = TasksQuery<'a, TasksDefault>;
@@ -461,14 +468,9 @@ pub struct TasksQuery<'a, T> {
461468
serialize_with = "time::serde::rfc3339::option::serialize"
462469
)]
463470
after_finished_at: Option<OffsetDateTime>,
464-
// Maximum number of tasks to return
465-
#[serde(skip_serializing_if = "Option::is_none")]
466-
limit: Option<u32>,
467-
// The first task uid that should be returned
468-
#[serde(skip_serializing_if = "Option::is_none")]
469-
from: Option<u32>,
470-
#[serde(skip_serializing)]
471-
phantom_data: PhantomData<T>,
471+
472+
#[serde(flatten)]
473+
pagination: T,
472474
}
473475

474476
#[allow(missing_docs)]
@@ -543,10 +545,6 @@ impl<'a, T> TasksQuery<'a, T> {
543545
self.after_finished_at = Some(*after_finished_at);
544546
self
545547
}
546-
547-
pub async fn execute(&'a self) -> Result<TasksResults, Error> {
548-
self.client.get_tasks_with(self).await
549-
}
550548
}
551549

552550
impl<'a> TasksQuery<'a, TasksDefault> {
@@ -556,18 +554,20 @@ impl<'a> TasksQuery<'a, TasksDefault> {
556554
index_uid: None,
557555
status: None,
558556
task_type: None,
559-
limit: None,
560-
from: None,
561557
uid: None,
562558
before_enqueued_at: None,
563559
after_enqueued_at: None,
564560
before_started_at: None,
565561
after_started_at: None,
566562
before_finished_at: None,
567563
after_finished_at: None,
568-
phantom_data: PhantomData,
564+
pagination: TasksDefault {},
569565
}
570566
}
567+
568+
// pub async fn execute(&'a self) -> Result<TasksResults, Error> {
569+
// self.client.get_tasks_with(self).await
570+
// }
571571
}
572572

573573
impl<'a> TasksQuery<'a, TasksPagination> {
@@ -577,26 +577,30 @@ impl<'a> TasksQuery<'a, TasksPagination> {
577577
index_uid: None,
578578
status: None,
579579
task_type: None,
580-
limit: None,
581-
from: None,
582580
uid: None,
583581
before_enqueued_at: None,
584582
after_enqueued_at: None,
585583
before_started_at: None,
586584
after_started_at: None,
587585
before_finished_at: None,
588586
after_finished_at: None,
589-
phantom_data: PhantomData,
587+
pagination: TasksPagination {
588+
limit: None,
589+
from: None,
590+
},
590591
}
591592
}
592593
pub fn with_limit<'b>(&'b mut self, limit: u32) -> &'b mut TasksQuery<'a, TasksPagination> {
593-
self.limit = Some(limit);
594+
self.pagination.limit = Some(limit);
594595
self
595596
}
596597
pub fn with_from<'b>(&'b mut self, from: u32) -> &'b mut TasksQuery<'a, TasksPagination> {
597-
self.from = Some(from);
598+
self.pagination.from = Some(from);
598599
self
599600
}
601+
pub async fn execute(&'a self) -> Result<TasksResults, Error> {
602+
self.client.get_tasks_with(self).await
603+
}
600604
}
601605

602606
#[cfg(test)]

0 commit comments

Comments
 (0)