Move string concat to the struct instead of in the calling

This commit is contained in:
Bruno Casali 2022-03-15 18:36:10 -03:00
parent 4822fe1beb
commit adc71742c8

View File

@ -1,3 +1,4 @@
use std::collections::HashSet;
use std::fmt::{Debug, Display}; use std::fmt::{Debug, Display};
use std::ops::Bound::{self, Excluded, Included}; use std::ops::Bound::{self, Excluded, Included};
use std::ops::Deref; use std::ops::Deref;
@ -27,7 +28,7 @@ pub struct Filter<'a> {
#[derive(Debug)] #[derive(Debug)]
enum FilterError<'a> { enum FilterError<'a> {
AttributeNotFilterable { attribute: &'a str, filterable: String }, AttributeNotFilterable { attribute: &'a str, filterable_fields: HashSet<String> },
BadGeo(&'a str), BadGeo(&'a str),
BadGeoLat(f64), BadGeoLat(f64),
BadGeoLng(f64), BadGeoLng(f64),
@ -39,19 +40,21 @@ impl<'a> std::error::Error for FilterError<'a> {}
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 {
Self::AttributeNotFilterable { attribute, filterable } => { Self::AttributeNotFilterable { attribute, filterable_fields } => {
if filterable.is_empty() { if filterable_fields.is_empty() {
write!( write!(
f, f,
"Attribute `{}` is not filterable. This index does not have configured filterable attributes.", "Attribute `{}` is not filterable. This index does not have configured filterable attributes.",
attribute, attribute,
) )
} else { } else {
let filterables_list = filterable_fields.iter().map(AsRef::as_ref).collect::<Vec<_>>().join(" ");
write!( write!(
f, f,
"Attribute `{}` is not filterable. Available filterable attributes are: `{}`.", "Attribute `{}` is not filterable. Available filterable attributes are: `{}`.",
attribute, attribute,
filterable, filterables_list,
) )
} }
}, },
@ -372,10 +375,7 @@ impl<'a> Filter<'a> {
return Err(fid.as_external_error( return Err(fid.as_external_error(
FilterError::AttributeNotFilterable { FilterError::AttributeNotFilterable {
attribute, attribute,
filterable: filterable_fields filterable_fields,
.into_iter()
.collect::<Vec<_>>()
.join(" "),
}, },
))?; ))?;
} }
@ -426,7 +426,7 @@ impl<'a> Filter<'a> {
} else { } else {
return Err(point[0].as_external_error(FilterError::AttributeNotFilterable { return Err(point[0].as_external_error(FilterError::AttributeNotFilterable {
attribute: "_geo", attribute: "_geo",
filterable: filterable_fields.into_iter().collect::<Vec<_>>().join(" "), filterable_fields,
}))?; }))?;
} }
} }