diff --git a/meilisearch-types/Cargo.toml b/meilisearch-types/Cargo.toml index 3a905f7f9..3aae58b2b 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" } +bitflags = "2.6.0" [dev-dependencies] insta = "1.39.0" diff --git a/meilisearch-types/src/keys.rs b/meilisearch-types/src/keys.rs index fcbb8de05..bfa48ffcb 100644 --- a/meilisearch-types/src/keys.rs +++ b/meilisearch-types/src/keys.rs @@ -260,6 +260,21 @@ impl Action { ("experimental.update", Self::ExperimentalFeaturesUpdate), ]; + fn ser_action_to_action(v: &str) -> Option { + Self::SERDE_MAP_ARR + .iter() + .find(|(ser_action, _)| &v == ser_action) + .map(|(_, action)| *action) + } + + fn action_to_ser_action(v: &Action) -> &'static str { + Self::SERDE_MAP_ARR + .iter() + .find(|(_, ref action)| v == action) + .map(|(ser_action, _)| ser_action) + .expect("`action_wanted` should always have a matching serialized value") + } + pub const fn from_repr(repr: u8) -> Option { use actions::*; match repr { @@ -311,16 +326,16 @@ impl Deserr for Action { location: deserr::ValuePointerRef<'_>, ) -> Result { match value { - deserr::Value::String(s) => { - match Self::SERDE_MAP_ARR.iter().find(|(serialized, _)| s == *serialized) { - Some((_, action)) => Ok(*action), - None => Err(deserr::take_cf_content(E::error::( - None, - deserr::ErrorKind::Unexpected { msg: format!("TODO {}", s) }, - location, - ))), - } - } + deserr::Value::String(s) => match Self::ser_action_to_action(&s) { + Some(action) => Ok(action), + None => Err(deserr::take_cf_content(E::error::( + None, + deserr::ErrorKind::Unexpected { + msg: format!("string must be a valid action, got {}", s), + }, + location, + ))), + }, _ => Err(take_cf_content(E::error( None, deserr::ErrorKind::IncorrectValueKind { @@ -338,12 +353,7 @@ impl Serialize for Action { 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() + serializer.serialize_str(Self::action_to_ser_action(self)) } } @@ -364,9 +374,8 @@ impl<'de> Deserialize<'de> for Action { where E: serde::de::Error, { - // @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), + match Self::Value::ser_action_to_action(s) { + Some(action) => Ok(action), None => Err(E::invalid_value(serde::de::Unexpected::Str(s), &"a valid action")), } }