apply code suggestion

Co-authored-by: Kerollmops <kero@meilisearch.com>
This commit is contained in:
Tamo 2022-05-04 14:11:03 +02:00
parent 3cb1f6d0a1
commit c55368ddd4
No known key found for this signature in database
GPG Key ID: 20CD8020AFA88D69
2 changed files with 22 additions and 16 deletions

View File

@ -97,6 +97,12 @@ impl From<fst::Error> for Error {
} }
} }
impl From<GeoError> for Error {
fn from(error: GeoError) -> Error {
Error::UserError(UserError::InvalidGeoField(error))
}
}
impl<E> From<grenad::Error<E>> for Error impl<E> From<grenad::Error<E>> for Error
where where
Error: From<E>, Error: From<E>,

View File

@ -7,7 +7,7 @@ use serde_json::Value;
use super::helpers::{create_writer, writer_into_reader, GrenadParameters}; use super::helpers::{create_writer, writer_into_reader, GrenadParameters};
use crate::error::GeoError; use crate::error::GeoError;
use crate::{FieldId, InternalError, Result, UserError}; use crate::{FieldId, InternalError, Result};
/// Extracts the geographical coordinates contained in each document under the `_geo` field. /// Extracts the geographical coordinates contained in each document under the `_geo` field.
/// ///
@ -35,23 +35,23 @@ pub fn extract_geo_points<R: io::Read + io::Seek>(
}; };
// first we get the two fields // first we get the two fields
let lat = obkv.get(lat_fid).ok_or_else(|| -> UserError { let lat = obkv
GeoError::MissingLatitude { document_id: primary_key() }.into() .get(lat_fid)
})?; .ok_or_else(|| GeoError::MissingLatitude { document_id: primary_key() })?;
let lng = obkv.get(lng_fid).ok_or_else(|| -> UserError { let lng = obkv
GeoError::MissingLongitude { document_id: primary_key() }.into() .get(lng_fid)
})?; .ok_or_else(|| GeoError::MissingLongitude { document_id: primary_key() })?;
// then we extract the values // then we extract the values
let lat = extract_value(serde_json::from_slice(lat).map_err(InternalError::SerdeJson)?) let lat = extract_float_from_value(
.map_err(|lat| -> UserError { serde_json::from_slice(lat).map_err(InternalError::SerdeJson)?,
GeoError::BadLatitude { document_id: primary_key(), value: lat }.into() )
})?; .map_err(|lat| GeoError::BadLatitude { document_id: primary_key(), value: lat })?;
let lng = extract_value(serde_json::from_slice(lng).map_err(InternalError::SerdeJson)?) let lng = extract_float_from_value(
.map_err(|lng| -> UserError { serde_json::from_slice(lng).map_err(InternalError::SerdeJson)?,
GeoError::BadLongitude { document_id: primary_key(), value: lng }.into() )
})?; .map_err(|lng| GeoError::BadLongitude { document_id: primary_key(), value: lng })?;
let bytes: [u8; 16] = concat_arrays![lat.to_ne_bytes(), lng.to_ne_bytes()]; let bytes: [u8; 16] = concat_arrays![lat.to_ne_bytes(), lng.to_ne_bytes()];
writer.insert(docid_bytes, bytes)?; writer.insert(docid_bytes, bytes)?;
@ -60,7 +60,7 @@ pub fn extract_geo_points<R: io::Read + io::Seek>(
Ok(writer_into_reader(writer)?) Ok(writer_into_reader(writer)?)
} }
fn extract_value(value: Value) -> StdResult<f64, Value> { fn extract_float_from_value(value: Value) -> StdResult<f64, Value> {
match value { match value {
Value::Number(ref n) => n.as_f64().ok_or(value), Value::Number(ref n) => n.as_f64().ok_or(value),
Value::String(ref s) => s.parse::<f64>().map_err(|_| value), Value::String(ref s) => s.parse::<f64>().map_err(|_| value),