From c2b14b8f8fa490a9aa4fa9f038aad1758a80ae93 Mon Sep 17 00:00:00 2001 From: "F. Levi" <55688616+flevi29@users.noreply.github.com> Date: Wed, 13 Nov 2024 15:08:55 +0200 Subject: [PATCH] Fix erroneous Sequence impl --- crates/meilisearch-types/src/keys.rs | 29 ++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/crates/meilisearch-types/src/keys.rs b/crates/meilisearch-types/src/keys.rs index 76b675bb7..942215099 100644 --- a/crates/meilisearch-types/src/keys.rs +++ b/crates/meilisearch-types/src/keys.rs @@ -413,11 +413,9 @@ impl Sequence for Action { let mut potential_next_index = self.get_potential_index() + 1; loop { - if let Some(next_flag) = Self::FLAGS.get(potential_next_index) { - let next_flag_value = next_flag.value(); - - if next_flag_value > self { - return Some(*next_flag_value); + if let Some(next_flag) = Self::FLAGS.get(potential_next_index).map(|v| v.value()) { + if next_flag > self { + return Some(*next_flag); } potential_next_index += 1; @@ -429,18 +427,25 @@ impl Sequence for Action { fn previous(&self) -> Option { // -2 because of "all" type flags that represent a single flag, otherwise -1 would suffice - let mut potential_previous_index = self.get_potential_index() - 2; + let initial_potential_index = self.get_potential_index(); + if initial_potential_index == 0 { + return None; + } + + let mut potential_previous_index: usize = + if initial_potential_index == 1 { 0 } else { initial_potential_index - 2 }; + let mut previous_item: Option = None; + let mut pre_previous_item: Option = None; loop { - if let Some(next_flag) = Self::FLAGS.get(potential_previous_index) { - let next_flag_value = next_flag.value(); - - if next_flag_value > self { - return previous_item; + if let Some(next_flag) = Self::FLAGS.get(potential_previous_index).map(|v| v.value()) { + if next_flag > self { + return pre_previous_item; } - previous_item = Some(*next_flag_value); + pre_previous_item = previous_item; + previous_item = Some(*next_flag); potential_previous_index += 1; } else { return None;