mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-11-23 21:34:27 +01:00
Refactor
This commit is contained in:
parent
b6c5a57932
commit
e54fbb0d1e
@ -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"
|
||||
|
@ -260,6 +260,21 @@ impl Action {
|
||||
("experimental.update", Self::ExperimentalFeaturesUpdate),
|
||||
];
|
||||
|
||||
fn ser_action_to_action(v: &str) -> Option<Action> {
|
||||
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<Self> {
|
||||
use actions::*;
|
||||
match repr {
|
||||
@ -311,16 +326,16 @@ impl<E: DeserializeError> Deserr<E> for Action {
|
||||
location: deserr::ValuePointerRef<'_>,
|
||||
) -> Result<Self, E> {
|
||||
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::<std::convert::Infallible>(
|
||||
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::<std::convert::Infallible>(
|
||||
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")),
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user