From b6c5a57932eb4d0fecab559b78dfb47dbbd89508 Mon Sep 17 00:00:00 2001 From: "F. Levi" <55688616+flevi29@users.noreply.github.com> Date: Tue, 17 Sep 2024 23:10:59 +0300 Subject: [PATCH] Add Sequence impl, other changes/adjustments --- meilisearch-auth/src/store.rs | 2 +- meilisearch-types/Cargo.toml | 2 +- meilisearch-types/src/keys.rs | 61 ++++++++++++++++++++++++++--------- 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/meilisearch-auth/src/store.rs b/meilisearch-auth/src/store.rs index 237570522..20198e497 100644 --- a/meilisearch-auth/src/store.rs +++ b/meilisearch-auth/src/store.rs @@ -144,7 +144,7 @@ impl HeedAuthStore { actions.insert(Action::MetricsGet); } other => { - actions.insert(*other); + actions.insert(other); } } } diff --git a/meilisearch-types/Cargo.toml b/meilisearch-types/Cargo.toml index 351aa9468..3a905f7f9 100644 --- a/meilisearch-types/Cargo.toml +++ b/meilisearch-types/Cargo.toml @@ -38,7 +38,7 @@ time = { version = "0.3.36", features = [ ] } tokio = "1.38" uuid = { version = "1.10.0", features = ["serde", "v4"] } -bitflags = {version = "2.6.0", features = ["serde"] } +bitflags = {version = "2.6.0" } [dev-dependencies] insta = "1.39.0" diff --git a/meilisearch-types/src/keys.rs b/meilisearch-types/src/keys.rs index 537567e5c..fcbb8de05 100644 --- a/meilisearch-types/src/keys.rs +++ b/meilisearch-types/src/keys.rs @@ -222,7 +222,8 @@ bitflags! { } impl Action { - const SERIALIZATION_MAP: [(&'static str, Self); 34] = [ + // @TODO: Consider using https://github.com/rust-phf/rust-phf + const SERDE_MAP_ARR: [(&'static str, Self); 34] = [ ("*", Self::All), ("search", Self::Search), ("documents.*", Self::DocumentsAll), @@ -311,7 +312,7 @@ impl Deserr for Action { ) -> Result { match value { deserr::Value::String(s) => { - match Self::SERIALIZATION_MAP.iter().find(|(serialized, _)| s == *serialized) { + match Self::SERDE_MAP_ARR.iter().find(|(serialized, _)| s == *serialized) { Some((_, action)) => Ok(*action), None => Err(deserr::take_cf_content(E::error::( None, @@ -332,6 +333,20 @@ impl Deserr for Action { } } +impl Serialize for Action { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + Self::SERDE_MAP_ARR + .iter() + .find(|(_, action)| self == action) + .map(|(serialized, _)| serializer.serialize_str(serialized)) + // should always be found, so unwrap is safe to use + .unwrap() + } +} + impl<'de> Deserialize<'de> for Action { fn deserialize(deserializer: D) -> Result where @@ -349,9 +364,8 @@ impl<'de> Deserialize<'de> for Action { where E: serde::de::Error, { - // @TODO: Make a to_serialized and to_desiralized on Action - match Self::Value::SERIALIZATION_MAP.iter().find(|(serialized, _)| s == *serialized) - { + // @TODO: Make a to_serialized and to_desiralized on Action perhaps + match Self::Value::SERDE_MAP_ARR.iter().find(|(serialized, _)| s == *serialized) { Some((_, action)) => Ok(*action), None => Err(E::invalid_value(serde::de::Unexpected::Str(s), &"a valid action")), } @@ -362,17 +376,32 @@ impl<'de> Deserialize<'de> for Action { } } -impl Serialize for Action { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - Self::SERIALIZATION_MAP - .iter() - .find(|(_, action)| self == action) - .map(|(serialized, _)| serializer.serialize_str(serialized)) - // should always be found, so unwrap is safe to use - .unwrap() +impl Sequence for Action { + const CARDINALITY: usize = Self::SERDE_MAP_ARR.len(); + + fn next(&self) -> Option { + let next_index = self.bits() as usize + 1; + if next_index == Self::CARDINALITY { + None + } else { + Some(Self::SERDE_MAP_ARR[next_index].1) + } + } + + fn previous(&self) -> Option { + if self.bits() == 0 { + None + } else { + Some(Self::SERDE_MAP_ARR[self.bits() as usize - 1].1) + } + } + + fn first() -> Option { + Some(Self::SERDE_MAP_ARR[0].1) + } + + fn last() -> Option { + Some(Self::SERDE_MAP_ARR[Self::CARDINALITY - 1].1) } }