1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#[macro_use]
extern crate serde;

mod documents;
mod facets;
mod indices;
mod results;
mod search;

/// Most user-facing facilities can be imported through this
pub mod prelude {
  pub use crate::{
    facets::FacetBuilder,
    results::Results,
    search::{Crop, Query},
    MeiliMelo,
  };
}

use reqwest::{Client, Method, RequestBuilder};
use serde::{Deserialize, Serialize};
use thiserror::Error;

use self::search::QueryError;

pub use self::{
  documents::Update,
  facets::FacetBuilder,
  indices::Index,
  search::{Crop, Query},
};
pub use meilimelo_macros::schema;

/// Pseudo-marker trait for MeiliSearch schemas
pub trait Schema: Default + Serialize + for<'de> Deserialize<'de> {}

/// Descriptor to a MeiliSearch instance
#[derive(Debug, Default)]
pub struct MeiliMelo<'m> {
  /// Base hostname and port to the instance, including the scheme
  host: &'m str,
  /// Secret key to be used with the requests to MeiliSearch
  secret_key: Option<&'m str>,
}

/// Errors emitted by the library
#[derive(Debug, Error)]
pub enum Error {
  /// Error originating from the communication with the instance, either upstream or when parsing its responses
  #[error("upstream error")]
  UpstreamError(#[from] reqwest::Error),
  /// The crafted query was refused by the instance
  #[error("meilisearch query error")]
  InvalidQuery(QueryError),
}

impl<'m> MeiliMelo<'m> {
  /// Creates a new descriptor to a MeiliSearch instance
  ///
  /// # Arguments
  ///
  /// * `host` - Scheme, hostname and port to the MeiliSearch instance
  pub fn new(host: &str) -> MeiliMelo {
    MeiliMelo {
      host,
      ..Default::default()
    }
  }

  pub(crate) fn request(&self, method: Method, path: &str) -> RequestBuilder {
    let url = format!("{}{}", self.host, path);

    match self.secret_key {
      Some(key) => Client::new().request(method, &url).header("X-Meili-API-Key", key),
      None => Client::new().request(method, &url),
    }
  }

  /// Adds the secret key to be used to authenticate against MeiliSearch
  ///
  /// # Arguments
  ///
  /// * `key` - The string representation of the secret key
  ///
  /// # Examples
  ///
  /// ```
  /// use meilimelo::prelude::*;
  ///
  /// let m = MeiliMelo::new("https://meilisearch.example.com:7700")
  ///   .with_secret_key("abcdef");
  /// ```
  pub fn with_secret_key(mut self, key: &'m str) -> MeiliMelo<'m> {
    self.secret_key = Some(key);
    self
  }

  /// Initialize a search query
  ///
  /// The returned struct implements the builder pattern and allows to
  /// construct the query incrementally. Please see
  /// [`Query`](search/struct.Query.html) for details on the available methods.
  ///
  /// # Arguments
  ///
  /// * `index` - The name of the index to search
  pub fn search(&'m self, index: &'m str) -> Query<'_> {
    Query::new(self, index)
  }

  /// List all available indices
  ///
  /// # Examples
  ///
  /// ```no_run
  /// # use meilimelo::prelude::*;
  /// #
  /// # #[tokio::main]
  /// # async fn main() {
  /// let meili = MeiliMelo::new("host");
  ///
  /// for index in meili.indices().await.unwrap() {
  ///   println!("{}", index.name);
  /// }
  /// # }
  /// ```
  pub async fn indices(&'m self) -> Result<Vec<Index>, Error> {
    indices::list(self).await
  }

  /// Create a new index
  ///
  /// # Arguments
  ///
  /// * `uid` - unique ID for the new index
  /// * `name` - human-readable name for the index
  ///
  /// # Examples
  ///
  /// ```
  /// # use meilimelo::prelude::*;
  /// #
  /// # #[tokio::main]
  /// # async fn main() {
  /// MeiliMelo::new("host")
  ///   .create_index("employees", "Employees")
  ///   .await;
  /// # }
  /// ```
  pub async fn create_index<'a>(&'m self, uid: &str, name: &str) -> Result<Index, Error> {
    indices::create(self, uid, name).await
  }

  /// Delete an existing index
  ///
  /// # Arguments
  ///
  /// * `uid` - unique ID to the index to be deleted
  ///
  /// # Examples
  ///
  /// ```
  /// # use meilimelo::prelude::*;
  /// #
  /// # #[tokio::main]
  /// # async fn main() {
  /// MeiliMelo::new("host")
  ///   .delete_index("employees")
  ///   .await;
  /// # }
  /// ```
  pub async fn delete_index(&'m self, uid: &str) -> Result<(), Error> {
    indices::delete(self, uid).await
  }

  /// Index a collection of documents into MeiliSearch
  ///
  /// # Arguments
  ///
  /// * index - Name of the index into which documents are to be inserted
  /// * documents - Collection of `Serialize`-able structs to insert
  ///
  /// # Examples
  ///
  /// ```
  /// # use meilimelo::prelude::*;
  /// #
  /// # #[derive(serde::Serialize)]
  /// # struct Employee { firstname: String, lastname: String }
  /// #
  /// let docs = vec![
  ///   Employee { firstname: "Luke".to_string(), lastname: "Skywalker".to_string() }
  /// ];
  ///
  /// MeiliMelo::new("host")
  ///   .insert("employees", &docs);
  /// ```
  pub async fn insert<T>(&'m self, index: &str, documents: &Vec<T>) -> Result<Update, Error>
  where
    T: Serialize,
  {
    documents::insert(self, index, documents).await
  }

  /// List documents in order
  ///
  /// # Arguments
  ///
  /// * `index` - name of the index to browse
  /// * `limit` - number of documents to return
  /// * `offset` - offset to the first document to return
  ///
  /// # Examples
  ///
  /// ```no_run
  /// # use meilimelo::prelude::*;
  /// #
  /// # #[derive(serde::Deserialize)]
  /// # struct Employee { firstname: String, lastname: String };
  /// #
  /// # #[tokio::main]
  /// # async fn main() {
  /// let meili = MeiliMelo::new("host");
  ///
  /// for document in &meili.list_documents::<Employee>("employees", 10, 0).await.unwrap() {
  ///   println!("{} {}", document.firstname, document.lastname);
  /// }
  /// # }
  /// ```
  pub async fn list_documents<R>(&'m self, index: &str, limit: i64, offset: i64) -> Result<Vec<R>, Error>
  where
    for<'de> R: Deserialize<'de>,
  {
    documents::list(self, index, limit, offset).await
  }

  /// List documents in order
  ///
  /// # Arguments
  ///
  /// * `index` - name of the index to browse
  /// * `uid` - Unique ID of the document to return
  ///
  /// # Examples
  ///
  /// ```no_run
  /// # use meilimelo::{prelude::*, Error};
  /// #
  /// # #[derive(serde::Deserialize)]
  /// # struct Employee;
  /// #
  /// # #[tokio::main]
  /// # async fn main() {
  /// MeiliMelo::new("")
  ///   .get_document::<Employee>("employees", "lskywalker")
  ///   .await;
  /// # }
  /// ```
  pub async fn get_document<R>(&'m self, index: &str, uid: &str) -> Result<R, Error>
  where
    for<'de> R: Deserialize<'de>,
  {
    documents::get(self, index, uid).await
  }

  /// Delete a document
  ///
  /// # Arguments
  ///
  /// * `uid` - Unique ID of the document to delete
  ///
  /// # Examples
  ///
  /// ```no_run
  /// # use meilimelo::prelude::*;
  /// #
  /// # #[tokio::main]
  /// # async fn main() {
  /// MeiliMelo::new("host")
  ///   .delete_document("employees", "lskywalker")
  ///   .await;
  /// # }
  /// ```
  pub async fn delete_document(&'m self, index: &str, uid: &str) -> Result<Update, Error> {
    documents::delete(self, index, uid).await
  }
}