typesensei/api/documents/
batch.rs

1use crate::Typesense;
2
3use super::{BatchResult, Documents};
4use std::{
5    future::{Future, IntoFuture},
6    marker::PhantomData,
7};
8
9#[derive(Debug)]
10pub struct DocumentBatchAction<'a, T: Typesense, Fut: 'a> {
11    api: &'a Documents<'a, T>,
12    documents: &'a [T::Partial],
13    action: Option<&'a str>,
14    dirty_values: Option<&'a str>,
15    batch_size: Option<&'a str>,
16    fut: Fut,
17    _phantom: PhantomData<Fut>,
18}
19
20impl<'a, T: Typesense, Fut: 'a> DocumentBatchAction<'a, T, Fut> {
21    pub(crate) fn new(
22        api: &'a Documents<'a, T>,
23        action: Option<&'a str>,
24        documents: &'a [T::Partial],
25        fut: Fut,
26    ) -> DocumentBatchAction<'a, T, Fut> {
27        DocumentBatchAction {
28            api,
29            documents,
30            action,
31            dirty_values: None,
32            batch_size: None,
33            fut,
34            _phantom: PhantomData,
35        }
36    }
37
38    pub fn dirty_values(
39        mut self,
40        dirty_values: &'a str,
41    ) -> DocumentBatchAction<'a, T, impl 'a + Future<Output = BatchResult>> {
42        self.dirty_values.replace(dirty_values);
43        self.reset()
44    }
45
46    pub fn batch_size(
47        mut self,
48        batch_size: &'a str,
49    ) -> DocumentBatchAction<'a, T, impl 'a + Future<Output = BatchResult>> {
50        self.batch_size.replace(batch_size);
51        self.reset()
52    }
53
54    fn reset(self) -> DocumentBatchAction<'a, T, impl 'a + Future<Output = BatchResult>> {
55        let Self {
56            api,
57            documents,
58            action,
59            dirty_values,
60            batch_size,
61            ..
62        } = self;
63
64        let query = [
65            ("action", action),
66            ("dirty_values", dirty_values),
67            ("batch_size", batch_size),
68        ];
69
70        DocumentBatchAction::new(api, action, documents, api.batch_action(query, documents))
71    }
72}
73
74impl<'a, T: Typesense, Fut: 'a + Future<Output = BatchResult>> IntoFuture
75    for DocumentBatchAction<'a, T, Fut>
76{
77    type Output = Fut::Output;
78    type IntoFuture = Fut;
79
80    fn into_future(self) -> Self::IntoFuture {
81        self.fut
82    }
83}