mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-23 02:27:40 +08:00
implement From<ParseGeoError> for AscDescError
This commit is contained in:
parent
c0b77773ba
commit
c810af3ebf
@ -14,19 +14,21 @@ 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 {
|
||||||
InvalidLatitude(ParseGeoError),
|
GeoError(ParseGeoError),
|
||||||
InvalidLongitude(ParseGeoError),
|
|
||||||
InvalidSyntax { name: String },
|
InvalidSyntax { name: String },
|
||||||
ReservedKeyword { name: String },
|
ReservedKeyword { name: String },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<ParseGeoError> for AscDescError {
|
||||||
|
fn from(geo_error: ParseGeoError) -> Self {
|
||||||
|
AscDescError::GeoError(geo_error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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::InvalidLatitude(error) => {
|
Self::GeoError(error) => {
|
||||||
write!(f, "{error}",)
|
|
||||||
}
|
|
||||||
Self::InvalidLongitude(error) => {
|
|
||||||
write!(f, "{error}",)
|
write!(f, "{error}",)
|
||||||
}
|
}
|
||||||
Self::InvalidSyntax { name } => {
|
Self::InvalidSyntax { name } => {
|
||||||
@ -46,7 +48,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::InvalidLatitude(_) | AscDescError::InvalidLongitude(_) => {
|
AscDescError::GeoError(_) => {
|
||||||
CriterionError::ReservedNameForSort { name: "_geoPoint".to_string() }
|
CriterionError::ReservedNameForSort { name: "_geoPoint".to_string() }
|
||||||
}
|
}
|
||||||
AscDescError::InvalidSyntax { name } => CriterionError::InvalidName { name },
|
AscDescError::InvalidSyntax { name } => CriterionError::InvalidName { name },
|
||||||
@ -86,9 +88,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::InvalidLatitude(ParseGeoError::BadGeoLat(lat)))?;
|
return Err(ParseGeoError::BadGeoLat(lat))?;
|
||||||
} else if !(-180.0..=180.0).contains(&lng) {
|
} else if !(-180.0..=180.0).contains(&lng) {
|
||||||
return Err(AscDescError::InvalidLongitude(ParseGeoError::BadGeoLng(lng)))?;
|
return Err(ParseGeoError::BadGeoLng(lng))?;
|
||||||
}
|
}
|
||||||
Ok(Member::Geo([lat, lng]))
|
Ok(Member::Geo([lat, lng]))
|
||||||
}
|
}
|
||||||
@ -164,9 +166,7 @@ impl FromStr for AscDesc {
|
|||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
pub enum SortError {
|
pub enum SortError {
|
||||||
#[error("{}", error)]
|
#[error("{}", error)]
|
||||||
InvalidLatitude { error: ParseGeoError },
|
ParseGeoError { error: ParseGeoError },
|
||||||
#[error("{}", error)]
|
|
||||||
InvalidLongitude { error: ParseGeoError },
|
|
||||||
#[error("Invalid syntax for the geo parameter: expected expression formated like \
|
#[error("Invalid syntax for the geo parameter: expected expression formated like \
|
||||||
`_geoPoint(latitude, longitude)` and ending by `:asc` or `:desc`, found `{name}`.")]
|
`_geoPoint(latitude, longitude)` and ending by `:asc` or `:desc`, found `{name}`.")]
|
||||||
BadGeoPointUsage { name: String },
|
BadGeoPointUsage { name: String },
|
||||||
@ -185,8 +185,7 @@ 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::InvalidLatitude(error) => SortError::InvalidLatitude { error },
|
AscDescError::GeoError(error) => SortError::ParseGeoError { error },
|
||||||
AscDescError::InvalidLongitude(error) => SortError::InvalidLongitude { error },
|
|
||||||
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 }
|
||||||
@ -278,14 +277,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", InvalidLatitude(ParseGeoError::BadGeoLat(200.))),
|
("_geoPoint(200, 200):asc", GeoError(ParseGeoError::BadGeoLat(200.))),
|
||||||
("_geoPoint(90.000001, 0):asc", InvalidLatitude(ParseGeoError::BadGeoLat(90.000001))),
|
("_geoPoint(90.000001, 0):asc", GeoError(ParseGeoError::BadGeoLat(90.000001))),
|
||||||
(
|
("_geoPoint(0, -180.000001):desc", GeoError(ParseGeoError::BadGeoLng(-180.000001))),
|
||||||
"_geoPoint(0, -180.000001):desc",
|
("_geoPoint(159.256, 130):asc", GeoError(ParseGeoError::BadGeoLat(159.256))),
|
||||||
InvalidLongitude(ParseGeoError::BadGeoLng(-180.000001)),
|
("_geoPoint(12, -2021):desc", GeoError(ParseGeoError::BadGeoLng(-2021.))),
|
||||||
),
|
|
||||||
("_geoPoint(159.256, 130):asc", InvalidLatitude(ParseGeoError::BadGeoLat(159.256))),
|
|
||||||
("_geoPoint(12, -2021):desc", InvalidLongitude(ParseGeoError::BadGeoLng(-2021.))),
|
|
||||||
];
|
];
|
||||||
|
|
||||||
for (req, expected_error) in invalid_req {
|
for (req, expected_error) in invalid_req {
|
||||||
|
Loading…
Reference in New Issue
Block a user