From 42a3cdca66eea60c69a5ab04c1a37a8b67a29cc4 Mon Sep 17 00:00:00 2001 From: Tamo Date: Tue, 14 Feb 2023 13:58:33 +0100 Subject: [PATCH] get rids of the unwrap_any function in favor of take_cf_content --- meilisearch-types/src/deserr/mod.rs | 4 ++-- meilisearch-types/src/deserr/query_params.rs | 5 ++--- meilisearch-types/src/error.rs | 10 ---------- meilisearch-types/src/keys.rs | 4 ++-- meilisearch-types/src/settings.rs | 3 +-- meilisearch-types/src/star_or.rs | 11 +++++------ meilisearch/src/routes/indexes/mod.rs | 4 ++-- 7 files changed, 14 insertions(+), 27 deletions(-) diff --git a/meilisearch-types/src/deserr/mod.rs b/meilisearch-types/src/deserr/mod.rs index ad6f72e2c..3e6ec8b96 100644 --- a/meilisearch-types/src/deserr/mod.rs +++ b/meilisearch-types/src/deserr/mod.rs @@ -8,7 +8,7 @@ use deserr::{take_cf_content, DeserializeError, IntoValue, MergeWithError, Value use crate::error::deserr_codes::*; use crate::error::{ - unwrap_any, Code, DeserrParseBoolError, DeserrParseIntError, ErrorCode, InvalidTaskDateError, + Code, DeserrParseBoolError, DeserrParseIntError, ErrorCode, InvalidTaskDateError, ParseOffsetDateTimeError, }; use crate::index_uid::IndexUidFormatError; @@ -135,7 +135,7 @@ macro_rules! make_missing_field_convenience_builder { ($err_code:ident, $fn_name:ident) => { impl DeserrJsonError<$err_code> { pub fn $fn_name(field: &str, location: ValuePointerRef) -> Self { - let x = unwrap_any(Self::error::( + let x = deserr::take_cf_content(Self::error::( None, deserr::ErrorKind::MissingField { field }, location, diff --git a/meilisearch-types/src/deserr/query_params.rs b/meilisearch-types/src/deserr/query_params.rs index 06d83747c..dded0ea5c 100644 --- a/meilisearch-types/src/deserr/query_params.rs +++ b/meilisearch-types/src/deserr/query_params.rs @@ -18,7 +18,6 @@ use std::str::FromStr; use deserr::{DeserializeError, Deserr, MergeWithError, ValueKind}; use super::{DeserrParseBoolError, DeserrParseIntError}; -use crate::error::unwrap_any; use crate::index_uid::IndexUid; use crate::tasks::{Kind, Status}; @@ -50,9 +49,9 @@ where match value { deserr::Value::String(s) => match T::from_query_param(&s) { Ok(x) => Ok(Param(x)), - Err(e) => Err(unwrap_any(E::merge(None, e, location))), + Err(e) => Err(deserr::take_cf_content(E::merge(None, e, location))), }, - _ => Err(unwrap_any(E::error( + _ => Err(deserr::take_cf_content(E::error( None, deserr::ErrorKind::IncorrectValueKind { actual: value, diff --git a/meilisearch-types/src/error.rs b/meilisearch-types/src/error.rs index 5c2968b39..46368c29e 100644 --- a/meilisearch-types/src/error.rs +++ b/meilisearch-types/src/error.rs @@ -381,16 +381,6 @@ impl ErrorCode for io::Error { } } -/// Unwrap a result, either its Ok or Err value. -pub fn unwrap_any(any: std::ops::ControlFlow) -> T { - use std::ops::ControlFlow::*; - - match any { - Continue(any) => any, - Break(any) => any, - } -} - /// Deserialization when `deserr` cannot parse an API key date. #[derive(Debug)] pub struct ParseOffsetDateTimeError(pub String); diff --git a/meilisearch-types/src/keys.rs b/meilisearch-types/src/keys.rs index 87ed543d6..b2389b238 100644 --- a/meilisearch-types/src/keys.rs +++ b/meilisearch-types/src/keys.rs @@ -13,7 +13,7 @@ use uuid::Uuid; use crate::deserr::{immutable_field_error, DeserrError, DeserrJsonError}; use crate::error::deserr_codes::*; -use crate::error::{unwrap_any, Code, ErrorCode, ParseOffsetDateTimeError}; +use crate::error::{Code, ErrorCode, ParseOffsetDateTimeError}; use crate::index_uid_pattern::{IndexUidPattern, IndexUidPatternFormatError}; pub type KeyId = Uuid; @@ -78,7 +78,7 @@ fn deny_immutable_fields_api_key( "expiresAt" => immutable_field_error(field, accepted, Code::ImmutableApiKeyExpiresAt), "createdAt" => immutable_field_error(field, accepted, Code::ImmutableApiKeyCreatedAt), "updatedAt" => immutable_field_error(field, accepted, Code::ImmutableApiKeyUpdatedAt), - _ => unwrap_any(DeserrJsonError::::error::( + _ => deserr::take_cf_content(DeserrJsonError::::error::( None, deserr::ErrorKind::UnknownKey { key: field, accepted }, location, diff --git a/meilisearch-types/src/settings.rs b/meilisearch-types/src/settings.rs index c7023bb10..0fc7ea6e2 100644 --- a/meilisearch-types/src/settings.rs +++ b/meilisearch-types/src/settings.rs @@ -14,7 +14,6 @@ use serde::{Deserialize, Serialize, Serializer}; use crate::deserr::DeserrJsonError; use crate::error::deserr_codes::*; -use crate::error::unwrap_any; /// The maximimum number of results that the engine /// will be able to return in one search call. @@ -60,7 +59,7 @@ fn validate_min_word_size_for_typo_setting( ) -> Result { if let (Setting::Set(one), Setting::Set(two)) = (s.one_typo, s.two_typos) { if one > two { - return Err(unwrap_any(E::error::(None, ErrorKind::Unexpected { msg: format!("`minWordSizeForTypos` setting is invalid. `oneTypo` and `twoTypos` fields should be between `0` and `255`, and `twoTypos` should be greater or equals to `oneTypo` but found `oneTypo: {one}` and twoTypos: {two}`.") }, location))); + return Err(deserr::take_cf_content(E::error::(None, ErrorKind::Unexpected { msg: format!("`minWordSizeForTypos` setting is invalid. `oneTypo` and `twoTypos` fields should be between `0` and `255`, and `twoTypos` should be greater or equals to `oneTypo` but found `oneTypo: {one}` and twoTypos: {two}`.") }, location))); } } Ok(s) diff --git a/meilisearch-types/src/star_or.rs b/meilisearch-types/src/star_or.rs index 0f3ef10fb..cd26a1fb0 100644 --- a/meilisearch-types/src/star_or.rs +++ b/meilisearch-types/src/star_or.rs @@ -8,7 +8,6 @@ use serde::de::Visitor; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use crate::deserr::query_params::FromQueryParameter; -use crate::error::unwrap_any; /// A type that tries to match either a star (*) or /// any other thing that implements `FromStr`. @@ -128,11 +127,11 @@ where } else { match T::from_str(&v) { Ok(parsed) => Ok(StarOr::Other(parsed)), - Err(e) => Err(unwrap_any(E::merge(None, e, location))), + Err(e) => Err(deserr::take_cf_content(E::merge(None, e, location))), } } } - _ => Err(unwrap_any(E::error::( + _ => Err(deserr::take_cf_content(E::error::( None, deserr::ErrorKind::IncorrectValueKind { actual: value, @@ -206,10 +205,10 @@ where "*" => Ok(OptionStarOr::Star), s => match T::from_query_param(s) { Ok(x) => Ok(OptionStarOr::Other(x)), - Err(e) => Err(unwrap_any(E::merge(None, e, location))), + Err(e) => Err(deserr::take_cf_content(E::merge(None, e, location))), }, }, - _ => Err(unwrap_any(E::error::( + _ => Err(deserr::take_cf_content(E::error::( None, deserr::ErrorKind::IncorrectValueKind { actual: value, @@ -318,7 +317,7 @@ where Ok(OptionStarOrList::List(els)) } } - _ => Err(unwrap_any(E::error::( + _ => Err(deserr::take_cf_content(E::error::( None, deserr::ErrorKind::IncorrectValueKind { actual: value, diff --git a/meilisearch/src/routes/indexes/mod.rs b/meilisearch/src/routes/indexes/mod.rs index 2e13d782d..b58bef0e9 100644 --- a/meilisearch/src/routes/indexes/mod.rs +++ b/meilisearch/src/routes/indexes/mod.rs @@ -9,7 +9,7 @@ use log::debug; use meilisearch_types::deserr::query_params::Param; use meilisearch_types::deserr::{immutable_field_error, DeserrJsonError, DeserrQueryParamError}; use meilisearch_types::error::deserr_codes::*; -use meilisearch_types::error::{unwrap_any, Code, ResponseError}; +use meilisearch_types::error::{Code, ResponseError}; use meilisearch_types::index_uid::IndexUid; use meilisearch_types::milli::{self, FieldDistribution, Index}; use meilisearch_types::tasks::KindWithContent; @@ -147,7 +147,7 @@ fn deny_immutable_fields_index( "uid" => immutable_field_error(field, accepted, Code::ImmutableIndexUid), "createdAt" => immutable_field_error(field, accepted, Code::ImmutableIndexCreatedAt), "updatedAt" => immutable_field_error(field, accepted, Code::ImmutableIndexUpdatedAt), - _ => unwrap_any(DeserrJsonError::::error::( + _ => deserr::take_cf_content(DeserrJsonError::::error::( None, deserr::ErrorKind::UnknownKey { key: field, accepted }, location,