implement deref &str on the tokens

This commit is contained in:
Irevoire 2021-11-09 11:34:10 +01:00
parent a211a9cdcd
commit 0ea0146e04
No known key found for this signature in database
GPG Key ID: 7A6A970C96104F1B
2 changed files with 17 additions and 8 deletions

View File

@ -40,6 +40,7 @@ mod error;
mod value; mod value;
use std::fmt::Debug; use std::fmt::Debug;
use std::ops::Deref;
use std::str::FromStr; use std::str::FromStr;
pub use condition::{parse_condition, parse_to, Condition}; pub use condition::{parse_condition, parse_to, Condition};
@ -63,6 +64,14 @@ type IResult<'a, Ret> = nom::IResult<Span<'a>, Ret, Error<'a>>;
#[derive(Debug, Clone, Eq)] #[derive(Debug, Clone, Eq)]
pub struct Token<'a>(Span<'a>); pub struct Token<'a>(Span<'a>);
impl<'a> Deref for Token<'a> {
type Target = &'a str;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'a> PartialEq for Token<'a> { impl<'a> PartialEq for Token<'a> {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.0.fragment() == other.0.fragment() self.0.fragment() == other.0.fragment()

View File

@ -1,5 +1,6 @@
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 either::Either; use either::Either;
pub use filter_parser::{Condition, Error as FPError, FilterCondition, Span, Token}; pub use filter_parser::{Condition, Error as FPError, FilterCondition, Span, Token};
@ -247,10 +248,9 @@ impl<'a> Filter<'a> {
Condition::LowerThanOrEqual(val) => (Included(f64::MIN), Included(val.parse()?)), Condition::LowerThanOrEqual(val) => (Included(f64::MIN), Included(val.parse()?)),
Condition::Between { from, to } => (Included(from.parse()?), Included(to.parse()?)), Condition::Between { from, to } => (Included(from.parse()?), Included(to.parse()?)),
Condition::Equal(val) => { Condition::Equal(val) => {
let (_original_value, string_docids) = strings_db let (_original_value, string_docids) =
.get(rtxn, &(field_id, &val.inner.to_lowercase()))? strings_db.get(rtxn, &(field_id, &val.to_lowercase()))?.unwrap_or_default();
.unwrap_or_default(); let number = val.parse::<f64>().ok();
let number = val.inner.parse::<f64>().ok();
let number_docids = match number { let number_docids = match number {
Some(n) => { Some(n) => {
let n = Included(n); let n = Included(n);
@ -271,7 +271,7 @@ impl<'a> Filter<'a> {
return Ok(string_docids | number_docids); return Ok(string_docids | number_docids);
} }
Condition::NotEqual(val) => { Condition::NotEqual(val) => {
let number = val.inner.parse::<f64>().ok(); let number = val.parse::<f64>().ok();
let all_numbers_ids = if number.is_some() { let all_numbers_ids = if number.is_some() {
index.number_faceted_documents_ids(rtxn, field_id)? index.number_faceted_documents_ids(rtxn, field_id)?
} else { } else {
@ -318,15 +318,15 @@ impl<'a> Filter<'a> {
match &self.condition { match &self.condition {
FilterCondition::Condition { fid, op } => { FilterCondition::Condition { fid, op } => {
let filterable_fields = index.filterable_fields(rtxn)?; let filterable_fields = index.filterable_fields(rtxn)?;
if filterable_fields.contains(&fid.inner.to_lowercase()) { if filterable_fields.contains(&fid.to_lowercase()) {
let field_ids_map = index.fields_ids_map(rtxn)?; let field_ids_map = index.fields_ids_map(rtxn)?;
if let Some(fid) = field_ids_map.id(fid.inner) { if let Some(fid) = field_ids_map.id(&fid) {
Self::evaluate_operator(rtxn, index, numbers_db, strings_db, fid, &op) Self::evaluate_operator(rtxn, index, numbers_db, strings_db, fid, &op)
} else { } else {
return Err(fid.as_external_error(FilterError::InternalError))?; return Err(fid.as_external_error(FilterError::InternalError))?;
} }
} else { } else {
match fid.inner { match *fid.deref() {
attribute @ "_geo" => { attribute @ "_geo" => {
return Err(fid.as_external_error(FilterError::BadGeo(attribute)))?; return Err(fid.as_external_error(FilterError::BadGeo(attribute)))?;
} }