From 60480a1e2ff3c641a11ba50aeaa326c45c15f367 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Wed, 27 Jan 2021 14:25:53 +0100 Subject: [PATCH] Rework the FacetCondition from_array constructor --- src/search/facet/facet_condition.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/search/facet/facet_condition.rs b/src/search/facet/facet_condition.rs index 19542a3ab..8e7d16a83 100644 --- a/src/search/facet/facet_condition.rs +++ b/src/search/facet/facet_condition.rs @@ -3,6 +3,7 @@ use std::fmt::Debug; use std::ops::Bound::{self, Included, Excluded}; use std::str::FromStr; +use anyhow::Context; use either::Either; use heed::types::{ByteSlice, DecodeIgnore}; use log::debug; @@ -154,16 +155,20 @@ impl FacetCondition { { fn facet_condition( fields_ids_map: &FieldsIdsMap, - faceted_fields: &HashMap, + faceted_fields: &HashMap, key: &str, value: &str, ) -> anyhow::Result { - let fid = fields_ids_map.id(key).unwrap(); - let ftype = faceted_fields.get(&fid).copied().unwrap(); - let (neg, value) = match value.strip_prefix('-') { - Some(value) => (true, value), - None => (false, value), + let fid = fields_ids_map.id(key).with_context(|| { + format!("{:?} must isn't part of the fields ids map", key) + })?; + let ftype = faceted_fields.get(key).copied().with_context(|| { + format!("{:?} must isn't a faceted field", key) + })?; + let (neg, value) = match value.trim().strip_prefix('-') { + Some(value) => (true, value.trim()), + None => (false, value.trim()), }; let operator = match ftype { @@ -176,7 +181,7 @@ impl FacetCondition { } let fields_ids_map = index.fields_ids_map(rtxn)?; - let faceted_fields = index.faceted_fields_ids(rtxn)?; + let faceted_fields = index.faceted_fields(rtxn)?; let mut ands = None; for either in array { @@ -185,8 +190,8 @@ impl FacetCondition { let mut ors = None; for rule in array { let mut iter = rule.as_ref().splitn(2, ':'); - let key = iter.next().unwrap(); - let value = iter.next().unwrap(); + let key = iter.next().context("missing facet condition key")?; + let value = iter.next().context("missing facet condition value")?; let condition = facet_condition(&fields_ids_map, &faceted_fields, key, value)?; ors = match ors.take() { Some(ors) => Some(Or(Box::new(ors), Box::new(condition))), @@ -203,8 +208,8 @@ impl FacetCondition { }, Either::Right(rule) => { let mut iter = rule.as_ref().splitn(2, ':'); - let key = iter.next().unwrap(); - let value = iter.next().unwrap(); + let key = iter.next().context("missing facet condition key")?; + let value = iter.next().context("missing facet condition value")?; let condition = facet_condition(&fields_ids_map, &faceted_fields, key, value)?; ands = match ands.take() { Some(ands) => Some(And(Box::new(ands), Box::new(condition))),