mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-22 10:07:40 +08:00
Refactor
This commit is contained in:
parent
b6c5a57932
commit
e54fbb0d1e
@ -38,7 +38,7 @@ time = { version = "0.3.36", features = [
|
|||||||
] }
|
] }
|
||||||
tokio = "1.38"
|
tokio = "1.38"
|
||||||
uuid = { version = "1.10.0", features = ["serde", "v4"] }
|
uuid = { version = "1.10.0", features = ["serde", "v4"] }
|
||||||
bitflags = {version = "2.6.0" }
|
bitflags = "2.6.0"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
insta = "1.39.0"
|
insta = "1.39.0"
|
||||||
|
@ -260,6 +260,21 @@ impl Action {
|
|||||||
("experimental.update", Self::ExperimentalFeaturesUpdate),
|
("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> {
|
pub const fn from_repr(repr: u8) -> Option<Self> {
|
||||||
use actions::*;
|
use actions::*;
|
||||||
match repr {
|
match repr {
|
||||||
@ -311,16 +326,16 @@ impl<E: DeserializeError> Deserr<E> for Action {
|
|||||||
location: deserr::ValuePointerRef<'_>,
|
location: deserr::ValuePointerRef<'_>,
|
||||||
) -> Result<Self, E> {
|
) -> Result<Self, E> {
|
||||||
match value {
|
match value {
|
||||||
deserr::Value::String(s) => {
|
deserr::Value::String(s) => match Self::ser_action_to_action(&s) {
|
||||||
match Self::SERDE_MAP_ARR.iter().find(|(serialized, _)| s == *serialized) {
|
Some(action) => Ok(action),
|
||||||
Some((_, action)) => Ok(*action),
|
|
||||||
None => Err(deserr::take_cf_content(E::error::<std::convert::Infallible>(
|
None => Err(deserr::take_cf_content(E::error::<std::convert::Infallible>(
|
||||||
None,
|
None,
|
||||||
deserr::ErrorKind::Unexpected { msg: format!("TODO {}", s) },
|
deserr::ErrorKind::Unexpected {
|
||||||
|
msg: format!("string must be a valid action, got {}", s),
|
||||||
|
},
|
||||||
location,
|
location,
|
||||||
))),
|
))),
|
||||||
}
|
},
|
||||||
}
|
|
||||||
_ => Err(take_cf_content(E::error(
|
_ => Err(take_cf_content(E::error(
|
||||||
None,
|
None,
|
||||||
deserr::ErrorKind::IncorrectValueKind {
|
deserr::ErrorKind::IncorrectValueKind {
|
||||||
@ -338,12 +353,7 @@ impl Serialize for Action {
|
|||||||
where
|
where
|
||||||
S: Serializer,
|
S: Serializer,
|
||||||
{
|
{
|
||||||
Self::SERDE_MAP_ARR
|
serializer.serialize_str(Self::action_to_ser_action(self))
|
||||||
.iter()
|
|
||||||
.find(|(_, action)| self == action)
|
|
||||||
.map(|(serialized, _)| serializer.serialize_str(serialized))
|
|
||||||
// should always be found, so unwrap is safe to use
|
|
||||||
.unwrap()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -364,9 +374,8 @@ impl<'de> Deserialize<'de> for Action {
|
|||||||
where
|
where
|
||||||
E: serde::de::Error,
|
E: serde::de::Error,
|
||||||
{
|
{
|
||||||
// @TODO: Make a to_serialized and to_desiralized on Action perhaps
|
match Self::Value::ser_action_to_action(s) {
|
||||||
match Self::Value::SERDE_MAP_ARR.iter().find(|(serialized, _)| s == *serialized) {
|
Some(action) => Ok(action),
|
||||||
Some((_, action)) => Ok(*action),
|
|
||||||
None => Err(E::invalid_value(serde::de::Unexpected::Str(s), &"a valid action")),
|
None => Err(E::invalid_value(serde::de::Unexpected::Str(s), &"a valid action")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user