implement From<ParseGeoError> for FilterError

This commit is contained in:
Filip Bachul 2023-02-09 16:35:15 +01:00
parent 4c91037602
commit 83c765ce6c

View File

@ -51,6 +51,12 @@ enum FilterError<'a> {
} }
impl<'a> std::error::Error for FilterError<'a> {} impl<'a> std::error::Error for FilterError<'a> {}
impl<'a> From<ParseGeoError> for FilterError<'a> {
fn from(geo_error: ParseGeoError) -> Self {
FilterError::ParseGeoError(geo_error)
}
}
impl<'a> Display for FilterError<'a> { impl<'a> Display for FilterError<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
@ -317,13 +323,13 @@ impl<'a> Filter<'a> {
} }
} else { } else {
match fid.value() { match fid.value() {
attribute @ "_geo" => Err(fid.as_external_error( attribute @ "_geo" => {
FilterError::ParseGeoError(ParseGeoError::BadGeo(attribute.to_owned())), Err(fid.as_external_error(ParseGeoError::BadGeo(attribute.to_owned())))?
))?, }
attribute if attribute.starts_with("_geoPoint(") => Err(fid attribute if attribute.starts_with("_geoPoint(") => {
.as_external_error(FilterError::ParseGeoError( Err(fid
ParseGeoError::BadGeo("_geoPoint".to_owned()), .as_external_error(ParseGeoError::BadGeo("_geoPoint".to_owned())))?
)))?, }
attribute @ "_geoDistance" => { attribute @ "_geoDistance" => {
Err(fid.as_external_error(FilterError::Reserved(attribute)))? Err(fid.as_external_error(FilterError::Reserved(attribute)))?
} }
@ -374,14 +380,14 @@ impl<'a> Filter<'a> {
let base_point: [f64; 2] = let base_point: [f64; 2] =
[point[0].parse_finite_float()?, point[1].parse_finite_float()?]; [point[0].parse_finite_float()?, point[1].parse_finite_float()?];
if !(-90.0..=90.0).contains(&base_point[0]) { if !(-90.0..=90.0).contains(&base_point[0]) {
return Err(point[0].as_external_error(FilterError::ParseGeoError( return Err(
ParseGeoError::BadGeoLat(base_point[0]), point[0].as_external_error(ParseGeoError::BadGeoLat(base_point[0]))
)))?; )?;
} }
if !(-180.0..=180.0).contains(&base_point[1]) { if !(-180.0..=180.0).contains(&base_point[1]) {
return Err(point[1].as_external_error(FilterError::ParseGeoError( return Err(
ParseGeoError::BadGeoLng(base_point[1]), point[1].as_external_error(ParseGeoError::BadGeoLng(base_point[1]))
)))?; )?;
} }
let radius = radius.parse_finite_float()?; let radius = radius.parse_finite_float()?;
let rtree = match index.geo_rtree(rtxn)? { let rtree = match index.geo_rtree(rtxn)? {
@ -419,32 +425,26 @@ impl<'a> Filter<'a> {
bottom_right_point[1].parse_finite_float()?, bottom_right_point[1].parse_finite_float()?,
]; ];
if !(-90.0..=90.0).contains(&top_left[0]) { if !(-90.0..=90.0).contains(&top_left[0]) {
return Err(top_left_point[0].as_external_error( return Err(top_left_point[0]
FilterError::ParseGeoError(ParseGeoError::BadGeoLat(top_left[0])), .as_external_error(ParseGeoError::BadGeoLat(top_left[0])))?;
))?;
} }
if !(-180.0..=180.0).contains(&top_left[1]) { if !(-180.0..=180.0).contains(&top_left[1]) {
return Err(top_left_point[1].as_external_error( return Err(top_left_point[1]
FilterError::ParseGeoError(ParseGeoError::BadGeoLng(top_left[1])), .as_external_error(ParseGeoError::BadGeoLng(top_left[1])))?;
))?;
} }
if !(-90.0..=90.0).contains(&bottom_right[0]) { if !(-90.0..=90.0).contains(&bottom_right[0]) {
return Err(bottom_right_point[0].as_external_error( return Err(bottom_right_point[0]
FilterError::ParseGeoError(ParseGeoError::BadGeoLat(bottom_right[0])), .as_external_error(ParseGeoError::BadGeoLat(bottom_right[0])))?;
))?;
} }
if !(-180.0..=180.0).contains(&bottom_right[1]) { if !(-180.0..=180.0).contains(&bottom_right[1]) {
return Err(bottom_right_point[1].as_external_error( return Err(bottom_right_point[1]
FilterError::ParseGeoError(ParseGeoError::BadGeoLng(bottom_right[1])), .as_external_error(ParseGeoError::BadGeoLng(bottom_right[1])))?;
))?;
} }
if top_left[0] < bottom_right[0] { if top_left[0] < bottom_right[0] {
return Err(bottom_right_point[1].as_external_error( return Err(bottom_right_point[1].as_external_error(
FilterError::ParseGeoError( ParseGeoError::BadGeoBoundingBoxTopIsBelowBottom(
ParseGeoError::BadGeoBoundingBoxTopIsBelowBottom( top_left[0],
top_left[0], bottom_right[0],
bottom_right[0],
),
), ),
))?; ))?;
} }