mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-12-25 06:00:08 +01:00
Apply suggestions from code review
Co-authored-by: Clément Renault <clement@meilisearch.com>
This commit is contained in:
parent
11dfe38761
commit
b65aa7b5ac
@ -12,8 +12,8 @@ use crate::{CriterionError, Error, UserError};
|
|||||||
/// You must always cast it to a sort error or a criterion error.
|
/// You must always cast it to a sort error or a criterion error.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum AscDescError {
|
pub enum AscDescError {
|
||||||
BadLat,
|
InvalidLatitude,
|
||||||
BadLng,
|
InvalidLongitude,
|
||||||
InvalidSyntax { name: String },
|
InvalidSyntax { name: String },
|
||||||
ReservedKeyword { name: String },
|
ReservedKeyword { name: String },
|
||||||
}
|
}
|
||||||
@ -21,10 +21,10 @@ pub enum AscDescError {
|
|||||||
impl fmt::Display for AscDescError {
|
impl fmt::Display for AscDescError {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Self::BadLat => {
|
Self::InvalidLatitude => {
|
||||||
write!(f, "Latitude must be contained between -90 and 90 degrees.",)
|
write!(f, "Latitude must be contained between -90 and 90 degrees.",)
|
||||||
}
|
}
|
||||||
Self::BadLng => {
|
Self::InvalidLongitude => {
|
||||||
write!(f, "Longitude must be contained between -180 and 180 degrees.",)
|
write!(f, "Longitude must be contained between -180 and 180 degrees.",)
|
||||||
}
|
}
|
||||||
Self::InvalidSyntax { name } => {
|
Self::InvalidSyntax { name } => {
|
||||||
@ -44,7 +44,7 @@ impl fmt::Display for AscDescError {
|
|||||||
impl From<AscDescError> for CriterionError {
|
impl From<AscDescError> for CriterionError {
|
||||||
fn from(error: AscDescError) -> Self {
|
fn from(error: AscDescError) -> Self {
|
||||||
match error {
|
match error {
|
||||||
AscDescError::BadLat | AscDescError::BadLng => {
|
AscDescError::InvalidLatitude | AscDescError::InvalidLongitude => {
|
||||||
CriterionError::ReservedNameForSort { name: "_geoPoint".to_string() }
|
CriterionError::ReservedNameForSort { name: "_geoPoint".to_string() }
|
||||||
}
|
}
|
||||||
AscDescError::InvalidSyntax { name } => CriterionError::InvalidName { name },
|
AscDescError::InvalidSyntax { name } => CriterionError::InvalidName { name },
|
||||||
@ -81,9 +81,9 @@ impl FromStr for Member {
|
|||||||
.map_err(|_| AscDescError::ReservedKeyword { name: text.to_string() })
|
.map_err(|_| AscDescError::ReservedKeyword { name: text.to_string() })
|
||||||
})?;
|
})?;
|
||||||
if !(-90.0..=90.0).contains(&lat) {
|
if !(-90.0..=90.0).contains(&lat) {
|
||||||
return Err(AscDescError::BadLat)?;
|
return Err(AscDescError::InvalidLatitude)?;
|
||||||
} else if !(-180.0..=180.0).contains(&lng) {
|
} else if !(-180.0..=180.0).contains(&lng) {
|
||||||
return Err(AscDescError::BadLng)?;
|
return Err(AscDescError::InvalidLongitude)?;
|
||||||
}
|
}
|
||||||
Ok(Member::Geo([lat, lng]))
|
Ok(Member::Geo([lat, lng]))
|
||||||
}
|
}
|
||||||
@ -155,8 +155,8 @@ impl FromStr for AscDesc {
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum SortError {
|
pub enum SortError {
|
||||||
BadLat,
|
InvalidLatitude,
|
||||||
BadLng,
|
InvalidLongitude,
|
||||||
BadGeoPointUsage { name: String },
|
BadGeoPointUsage { name: String },
|
||||||
InvalidName { name: String },
|
InvalidName { name: String },
|
||||||
ReservedName { name: String },
|
ReservedName { name: String },
|
||||||
@ -167,8 +167,8 @@ pub enum SortError {
|
|||||||
impl From<AscDescError> for SortError {
|
impl From<AscDescError> for SortError {
|
||||||
fn from(error: AscDescError) -> Self {
|
fn from(error: AscDescError) -> Self {
|
||||||
match error {
|
match error {
|
||||||
AscDescError::BadLat => SortError::BadLat,
|
AscDescError::InvalidLatitude => SortError::InvalidLatitude,
|
||||||
AscDescError::BadLng => SortError::BadLng,
|
AscDescError::InvalidLongitude => SortError::InvalidLongitude,
|
||||||
AscDescError::InvalidSyntax { name } => SortError::InvalidName { name },
|
AscDescError::InvalidSyntax { name } => SortError::InvalidName { name },
|
||||||
AscDescError::ReservedKeyword { name } if name.starts_with("_geoPoint") => {
|
AscDescError::ReservedKeyword { name } if name.starts_with("_geoPoint") => {
|
||||||
SortError::BadGeoPointUsage { name }
|
SortError::BadGeoPointUsage { name }
|
||||||
@ -187,12 +187,8 @@ impl From<AscDescError> for SortError {
|
|||||||
impl fmt::Display for SortError {
|
impl fmt::Display for SortError {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Self::BadLat => {
|
Self::InvalidLatitude => write!(f, "{}", AscDescError::InvalidLatitude),
|
||||||
write!(f, "Latitude must be contained between -90 and 90 degrees.",)
|
Self::InvalidLongitude => write!(f, "{}", AscDescError::InvalidLongitude),
|
||||||
}
|
|
||||||
Self::BadLng => {
|
|
||||||
write!(f, "Longitude must be contained between -180 and 180 degrees.",)
|
|
||||||
}
|
|
||||||
Self::BadGeoPointUsage { name } => {
|
Self::BadGeoPointUsage { name } => {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
@ -296,11 +292,11 @@ mod tests {
|
|||||||
),
|
),
|
||||||
("_geoPoint(35, 85, 75):asc", ReservedKeyword { name: S("_geoPoint(35, 85, 75)") }),
|
("_geoPoint(35, 85, 75):asc", ReservedKeyword { name: S("_geoPoint(35, 85, 75)") }),
|
||||||
("_geoPoint(18):asc", ReservedKeyword { name: S("_geoPoint(18)") }),
|
("_geoPoint(18):asc", ReservedKeyword { name: S("_geoPoint(18)") }),
|
||||||
("_geoPoint(200, 200):asc", BadLat),
|
("_geoPoint(200, 200):asc", InvalidLatitude),
|
||||||
("_geoPoint(90.000001, 0):asc", BadLat),
|
("_geoPoint(90.000001, 0):asc", InvalidLatitude),
|
||||||
("_geoPoint(0, -180.000001):desc", BadLng),
|
("_geoPoint(0, -180.000001):desc", InvalidLongitude),
|
||||||
("_geoPoint(159.256, 130):asc", BadLat),
|
("_geoPoint(159.256, 130):asc", InvalidLatitude),
|
||||||
("_geoPoint(12, -2021):desc", BadLng),
|
("_geoPoint(12, -2021):desc", InvalidLongitude),
|
||||||
];
|
];
|
||||||
|
|
||||||
for (req, expected_error) in invalid_req {
|
for (req, expected_error) in invalid_req {
|
||||||
@ -347,11 +343,11 @@ mod tests {
|
|||||||
S("`_geoRadius` is a reserved keyword and thus can't be used as a sort expression. Use the `_geoPoint(latitude, longitude)` built-in rule to sort on `_geo` field coordinates."),
|
S("`_geoRadius` is a reserved keyword and thus can't be used as a sort expression. Use the `_geoPoint(latitude, longitude)` built-in rule to sort on `_geo` field coordinates."),
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
AscDescError::BadLat,
|
AscDescError::InvalidLatitude,
|
||||||
S("Latitude must be contained between -90 and 90 degrees."),
|
S("Latitude must be contained between -90 and 90 degrees."),
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
AscDescError::BadLng,
|
AscDescError::InvalidLongitude,
|
||||||
S("Longitude must be contained between -180 and 180 degrees."),
|
S("Longitude must be contained between -180 and 180 degrees."),
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user