Skip to content

Commit a78ec53

Browse files
bors[bot]Mubelotix
andauthored
Merge #76
76: Update old code r=curquiza a=Mubelotix I copied a few data structures from the meilisearch core project when I started this SDK. It seems that these structs and enums were modified during the last months and that breaking changes were introduced. It would be useful if GitHub allowed me to subscribe to the changes on the 2 concerned files but it's not possible. ### TODO - [x] Update the outdated code - [x] Add new tests so that we can detect breaking changes faster (note that test structure is not ideal and there will be a new PR about this) Co-authored-by: Mubelotix <[email protected]>
2 parents 208c138 + c918828 commit a78ec53

File tree

3 files changed

+77
-39
lines changed

3 files changed

+77
-39
lines changed

src/indexes.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ impl<'a> Index<'a> {
338338
/// let client = Client::new("http://localhost:7700", "masterKey");
339339
/// let mut movie_index = client.get_or_create("movies_add_or_replace").await.unwrap();
340340
///
341-
/// movie_index.add_or_replace(&[
341+
/// let progress = movie_index.add_or_replace(&[
342342
/// Movie{
343343
/// name: String::from("Interstellar"),
344344
/// description: String::from("Interstellar chronicles the adventures of a group of explorers who make use of a newly discovered wormhole to surpass the limitations on human space travel and conquer the vast distances involved in an interstellar voyage.")
@@ -354,6 +354,7 @@ impl<'a> Index<'a> {
354354
/// },
355355
/// ], Some("name")).await.unwrap();
356356
/// sleep(Duration::from_secs(1)); // MeiliSearch may take some time to execute the request
357+
/// # progress.get_status().await.unwrap();
357358
///
358359
/// // retrieve movies (you have to put some movies in the index before)
359360
/// let movies = movie_index.get_documents::<Movie>(None, None, None).await.unwrap();
@@ -425,7 +426,7 @@ impl<'a> Index<'a> {
425426
/// let client = Client::new("http://localhost:7700", "masterKey");
426427
/// let mut movie_index = client.get_or_create("movies_add_or_update").await.unwrap();
427428
///
428-
/// movie_index.add_or_update(&[
429+
/// let progress = movie_index.add_or_update(&[
429430
/// Movie{
430431
/// name: String::from("Interstellar"),
431432
/// description: String::from("Interstellar chronicles the adventures of a group of explorers who make use of a newly discovered wormhole to surpass the limitations on human space travel and conquer the vast distances involved in an interstellar voyage.")
@@ -441,6 +442,7 @@ impl<'a> Index<'a> {
441442
/// },
442443
/// ], Some("name")).await.unwrap();
443444
/// sleep(Duration::from_secs(1)); // MeiliSearch may take some time to execute the request
445+
/// # progress.get_status().await.unwrap();
444446
///
445447
/// // retrieve movies (you have to put some movies in the index before)
446448
/// let movies = movie_index.get_documents::<Movie>(None, None, None).await.unwrap();
@@ -495,7 +497,9 @@ impl<'a> Index<'a> {
495497
///
496498
/// // add some documents
497499
///
498-
/// movie_index.delete_all_documents().await.unwrap();
500+
/// let progress = movie_index.delete_all_documents().await.unwrap();
501+
/// # std::thread::sleep(std::time::Duration::from_secs(1));
502+
/// # progress.get_status().await.unwrap();
499503
/// # let movies = movie_index.get_documents::<Movie>(None, None, None).await.unwrap();
500504
/// # assert_eq!(movies.len(), 0);
501505
/// # });
@@ -541,7 +545,8 @@ impl<'a> Index<'a> {
541545
/// # std::thread::sleep(std::time::Duration::from_secs(1));
542546
/// // add a document with id = Interstellar
543547
///
544-
/// movies.delete_document("Interstellar").await.unwrap();
548+
/// let progress = movies.delete_document("Interstellar").await.unwrap();
549+
/// # progress.get_status().await.unwrap();
545550
/// # });
546551
/// ```
547552
pub async fn delete_document<T: Display>(&'a self, uid: T) -> Result<Progress<'a>, Error> {
@@ -589,7 +594,8 @@ impl<'a> Index<'a> {
589594
/// # std::thread::sleep(std::time::Duration::from_secs(1));
590595
///
591596
/// // delete some documents
592-
/// movies.delete_documents(&["Interstellar", "Unknown"]).await.unwrap();
597+
/// let progress = movies.delete_documents(&["Interstellar", "Unknown"]).await.unwrap();
598+
/// # progress.get_status().await.unwrap();
593599
/// # });
594600
/// ```
595601
pub async fn delete_documents<T: Display + Serialize + std::fmt::Debug>(
@@ -659,8 +665,8 @@ impl<'a> Index<'a> {
659665
/// # client.delete_index("movies_get_all_updates").await.unwrap();
660666
/// # });
661667
/// ```
662-
pub async fn get_all_updates(&self) -> Result<Vec<ProcessedStatus>, Error> {
663-
request::<(), Vec<ProcessedStatus>>(
668+
pub async fn get_all_updates(&self) -> Result<Vec<UpdateStatus>, Error> {
669+
request::<(), Vec<UpdateStatus>>(
664670
&format!(
665671
"{}/indexes/{}/updates",
666672
self.client.host, self.uid

src/progress.rs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
use crate::{errors::Error, indexes::Index, request::*};
44
use serde::Deserialize;
5-
use serde_json::{from_value, Value};
6-
use std::collections::{BTreeMap, BTreeSet, HashSet};
5+
use std::collections::{BTreeMap, BTreeSet};
76

87
#[derive(Deserialize)]
98
#[serde(rename_all = "camelCase")]
@@ -39,8 +38,8 @@ impl<'a> Progress<'a> {
3938
/// let status = progress.get_status().await.unwrap();
4039
/// # });
4140
/// ```
42-
pub async fn get_status(&self) -> Result<Status, Error> {
43-
let value = request::<(), Value>(
41+
pub async fn get_status(&self) -> Result<UpdateStatus, Error> {
42+
request::<(), UpdateStatus>(
4443
&format!(
4544
"{}/indexes/{}/updates/{}",
4645
self.index.client.host, self.index.uid, self.id
@@ -49,17 +48,7 @@ impl<'a> Progress<'a> {
4948
Method::Get,
5049
200,
5150
)
52-
.await?;
53-
54-
if let Ok(status) = from_value::<ProcessedStatus>(value.clone()) {
55-
Ok(Status::Processed(status))
56-
} else {
57-
let result = from_value::<EnqueuedStatus>(value);
58-
match result {
59-
Ok(status) => Ok(Status::Enqueued(status)),
60-
Err(e) => Err(Error::ParseError(e)),
61-
}
62-
}
51+
.await
6352
}
6453
}
6554

@@ -72,7 +61,7 @@ pub enum RankingRule {
7261
WordsPosition,
7362
Exactness,
7463
Asc(String),
75-
Dsc(String),
64+
Desc(String),
7665
}
7766

7867
#[derive(Debug, Clone, Deserialize)]
@@ -83,53 +72,64 @@ pub enum UpdateState<T> {
8372
}
8473

8574
#[derive(Debug, Clone, Deserialize)]
86-
#[serde(rename_all = "camelCase")]
8775
pub struct SettingsUpdate {
8876
pub ranking_rules: UpdateState<Vec<RankingRule>>,
8977
pub distinct_attribute: UpdateState<String>,
90-
pub identifier: UpdateState<String>,
9178
pub searchable_attributes: UpdateState<Vec<String>>,
92-
pub displayed_attributes: UpdateState<HashSet<String>>,
79+
pub displayed_attributes: UpdateState<BTreeSet<String>>,
9380
pub stop_words: UpdateState<BTreeSet<String>>,
9481
pub synonyms: UpdateState<BTreeMap<String, Vec<String>>>,
82+
pub attributes_for_faceting: UpdateState<Vec<String>>,
9583
}
9684

9785
#[derive(Debug, Clone, Deserialize)]
9886
#[serde(tag = "name")]
99-
#[allow(clippy::large_enum_variant)] // would be great to correct but it's not my code it's from meilisearch/Meilisearch
10087
pub enum UpdateType {
10188
ClearAll,
10289
Customs,
10390
DocumentsAddition { number: usize },
10491
DocumentsPartial { number: usize },
10592
DocumentsDeletion { number: usize },
106-
Settings { settings: SettingsUpdate },
93+
Settings { settings: Box<SettingsUpdate> },
10794
}
10895

109-
#[derive(Deserialize, Debug)]
96+
#[derive(Deserialize, Debug, Clone)]
11097
#[serde(rename_all = "camelCase")]
111-
pub struct ProcessedStatus {
98+
pub struct ProcessedUpdateResult {
11299
pub update_id: u64,
113100
#[serde(rename = "type")]
114101
pub update_type: UpdateType,
115-
#[serde(skip_serializing_if = "Option::is_none")]
116102
pub error: Option<String>,
103+
pub error_type: Option<String>,
104+
pub error_code: Option<String>,
105+
pub error_link: Option<String>,
117106
pub duration: f64, // in seconds
118-
pub enqueued_at: String, // TODO deserialize to datatime
119-
pub processed_at: String, // TODO deserialize to datatime
107+
pub enqueued_at: String, // TODO deserialize to datetime
108+
pub processed_at: String, // TODO deserialize to datetime
120109
}
121110

122111
#[derive(Debug, Clone, Deserialize)]
123112
#[serde(rename_all = "camelCase")]
124-
pub struct EnqueuedStatus {
113+
pub struct EnqueuedUpdateResult {
125114
pub update_id: u64,
126115
#[serde(rename = "type")]
127116
pub update_type: UpdateType,
128-
pub enqueued_at: String, // TODO deserialize to datatime
117+
pub enqueued_at: String, // TODO deserialize to datetime
129118
}
130119

131-
#[derive(Debug)]
132-
pub enum Status {
133-
Processed(ProcessedStatus),
134-
Enqueued(EnqueuedStatus),
120+
#[derive(Debug, Clone, Deserialize)]
121+
#[serde(rename_all = "camelCase", tag = "status")]
122+
pub enum UpdateStatus {
123+
Enqueued {
124+
#[serde(flatten)]
125+
content: EnqueuedUpdateResult,
126+
},
127+
Failed {
128+
#[serde(flatten)]
129+
content: ProcessedUpdateResult,
130+
},
131+
Processed {
132+
#[serde(flatten)]
133+
content: ProcessedUpdateResult,
134+
},
135135
}

src/settings.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ impl<'a> Index<'a> {
279279
/// .with_stop_words(stop_words.clone());
280280
///
281281
/// let progress = movie_index.set_settings(&settings).await.unwrap();
282+
/// # std::thread::sleep(std::time::Duration::from_secs(2));
283+
/// # progress.get_status().await.unwrap();
282284
/// # });
283285
/// ```
284286
pub async fn set_settings(&'a self, settings: &Settings) -> Result<Progress<'a>, Error> {
@@ -307,6 +309,8 @@ impl<'a> Index<'a> {
307309
/// synonyms.insert(String::from("wow"), vec![String::from("world of warcraft")]);
308310
///
309311
/// let progress = movie_index.set_synonyms(&synonyms).await.unwrap();
312+
/// # std::thread::sleep(std::time::Duration::from_secs(2));
313+
/// # progress.get_status().await.unwrap();
310314
/// # });
311315
/// ```
312316
pub async fn set_synonyms(&'a self, synonyms: &HashMap<String, Vec<String>>) -> Result<Progress<'a>, Error> {
@@ -331,6 +335,8 @@ impl<'a> Index<'a> {
331335
///
332336
/// let stop_words = &["the", "of", "to"];
333337
/// let progress = movie_index.set_stop_words(stop_words).await.unwrap();
338+
/// # std::thread::sleep(std::time::Duration::from_secs(2));
339+
/// # progress.get_status().await.unwrap();
334340
/// # });
335341
/// ```
336342
pub async fn set_stop_words(&'a self, stop_words: &[&str]) -> Result<Progress<'a>, Error> {
@@ -364,6 +370,8 @@ impl<'a> Index<'a> {
364370
/// "desc(rank)",
365371
/// ];
366372
/// let progress = movie_index.set_ranking_rules(ranking_rules).await.unwrap();
373+
/// # std::thread::sleep(std::time::Duration::from_secs(2));
374+
/// # progress.get_status().await.unwrap();
367375
/// # });
368376
/// ```
369377
pub async fn set_ranking_rules(&'a self, ranking_rules: &[&str]) -> Result<Progress<'a>, Error> {
@@ -388,6 +396,8 @@ impl<'a> Index<'a> {
388396
///
389397
/// let attributes_for_faceting = &["genre", "director"];
390398
/// let progress = movie_index.set_attributes_for_faceting(attributes_for_faceting).await.unwrap();
399+
/// # std::thread::sleep(std::time::Duration::from_secs(2));
400+
/// # progress.get_status().await.unwrap();
391401
/// # });
392402
/// ```
393403
pub async fn set_attributes_for_faceting(&'a self, attributes_for_faceting: &[&str]) -> Result<Progress<'a>, Error> {
@@ -411,6 +421,8 @@ impl<'a> Index<'a> {
411421
/// let mut movie_index = client.get_or_create("movies").await.unwrap();
412422
///
413423
/// let progress = movie_index.set_distinct_attribute("movie_id").await.unwrap();
424+
/// # std::thread::sleep(std::time::Duration::from_secs(2));
425+
/// # progress.get_status().await.unwrap();
414426
/// # });
415427
/// ```
416428
pub async fn set_distinct_attribute(&'a self, distinct_attribute: &str) -> Result<Progress<'a>, Error> {
@@ -434,6 +446,8 @@ impl<'a> Index<'a> {
434446
/// let mut movie_index = client.get_or_create("movies").await.unwrap();
435447
///
436448
/// let progress = movie_index.set_searchable_attributes(&["title", "description", "uid"]).await.unwrap();
449+
/// # std::thread::sleep(std::time::Duration::from_secs(2));
450+
/// # progress.get_status().await.unwrap();
437451
/// # });
438452
/// ```
439453
pub async fn set_searchable_attributes(&'a self, searchable_attributes: &[&str]) -> Result<Progress<'a>, Error> {
@@ -457,6 +471,8 @@ impl<'a> Index<'a> {
457471
/// let mut movie_index = client.get_or_create("movies").await.unwrap();
458472
///
459473
/// let progress = movie_index.set_displayed_attributes(&["title", "description", "release_date", "rank", "poster"]).await.unwrap();
474+
/// # std::thread::sleep(std::time::Duration::from_secs(2));
475+
/// # progress.get_status().await.unwrap();
460476
/// # });
461477
/// ```
462478
pub async fn set_displayed_attributes(&'a self, displayed_attributes: &[&str]) -> Result<Progress<'a>, Error> {
@@ -481,6 +497,8 @@ impl<'a> Index<'a> {
481497
/// let mut movie_index = client.get_or_create("movies").await.unwrap();
482498
///
483499
/// let progress = movie_index.reset_settings().await.unwrap();
500+
/// # std::thread::sleep(std::time::Duration::from_secs(2));
501+
/// # progress.get_status().await.unwrap();
484502
/// # });
485503
/// ```
486504
pub async fn reset_settings(&'a self) -> Result<Progress<'a>, Error> {
@@ -504,6 +522,8 @@ impl<'a> Index<'a> {
504522
/// let mut movie_index = client.get_or_create("movies").await.unwrap();
505523
///
506524
/// let progress = movie_index.reset_synonyms().await.unwrap();
525+
/// # std::thread::sleep(std::time::Duration::from_secs(2));
526+
/// # progress.get_status().await.unwrap();
507527
/// # });
508528
/// ```
509529
pub async fn reset_synonyms(&'a self) -> Result<Progress<'a>, Error> {
@@ -527,6 +547,8 @@ impl<'a> Index<'a> {
527547
/// let mut movie_index = client.get_or_create("movies").await.unwrap();
528548
///
529549
/// let progress = movie_index.reset_stop_words().await.unwrap();
550+
/// # std::thread::sleep(std::time::Duration::from_secs(2));
551+
/// # progress.get_status().await.unwrap();
530552
/// # });
531553
/// ```
532554
pub async fn reset_stop_words(&'a self) -> Result<Progress<'a>, Error> {
@@ -551,6 +573,8 @@ impl<'a> Index<'a> {
551573
/// let mut movie_index = client.get_or_create("movies").await.unwrap();
552574
///
553575
/// let progress = movie_index.reset_ranking_rules().await.unwrap();
576+
/// # std::thread::sleep(std::time::Duration::from_secs(2));
577+
/// # progress.get_status().await.unwrap();
554578
/// # });
555579
/// ```
556580
pub async fn reset_ranking_rules(&'a self) -> Result<Progress<'a>, Error> {
@@ -574,6 +598,8 @@ impl<'a> Index<'a> {
574598
/// let mut movie_index = client.get_or_create("movies").await.unwrap();
575599
///
576600
/// let progress = movie_index.reset_attributes_for_faceting().await.unwrap();
601+
/// # std::thread::sleep(std::time::Duration::from_secs(2));
602+
/// # progress.get_status().await.unwrap();
577603
/// # });
578604
/// ```
579605
pub async fn reset_attributes_for_faceting(&'a self) -> Result<Progress<'a>, Error> {
@@ -597,6 +623,8 @@ impl<'a> Index<'a> {
597623
/// let mut movie_index = client.get_or_create("movies").await.unwrap();
598624
///
599625
/// let progress = movie_index.reset_distinct_attribute().await.unwrap();
626+
/// # std::thread::sleep(std::time::Duration::from_secs(2));
627+
/// # progress.get_status().await.unwrap();
600628
/// # });
601629
/// ```
602630
pub async fn reset_distinct_attribute(&'a self) -> Result<Progress<'a>, Error> {
@@ -620,6 +648,8 @@ impl<'a> Index<'a> {
620648
/// let mut movie_index = client.get_or_create("movies").await.unwrap();
621649
///
622650
/// let progress = movie_index.reset_searchable_attributes().await.unwrap();
651+
/// # std::thread::sleep(std::time::Duration::from_secs(2));
652+
/// # progress.get_status().await.unwrap();
623653
/// # });
624654
/// ```
625655
pub async fn reset_searchable_attributes(&'a self) -> Result<Progress<'a>, Error> {
@@ -643,6 +673,8 @@ impl<'a> Index<'a> {
643673
/// let mut movie_index = client.get_or_create("movies").await.unwrap();
644674
///
645675
/// let progress = movie_index.reset_displayed_attributes().await.unwrap();
676+
/// # std::thread::sleep(std::time::Duration::from_secs(2));
677+
/// # progress.get_status().await.unwrap();
646678
/// # });
647679
/// ```
648680
pub async fn reset_displayed_attributes(&'a self) -> Result<Progress<'a>, Error> {

0 commit comments

Comments
 (0)