auto convert AscDescError into CriterionError

This commit is contained in:
Tamo 2021-09-22 16:09:08 +02:00
parent 023446ecf3
commit 1e5e3d57e2
No known key found for this signature in database
GPG Key ID: 20CD8020AFA88D69

View File

@ -4,7 +4,7 @@ use std::str::FromStr;
use serde::{Deserialize, Serialize};
use crate::error::Error;
use crate::{AscDesc, AscDescError, Member, UserError};
use crate::{AscDesc, Member, UserError};
#[derive(Debug)]
pub enum CriterionError {
@ -41,6 +41,12 @@ impl fmt::Display for CriterionError {
}
}
impl From<CriterionError> for Error {
fn from(error: CriterionError) -> Self {
Self::UserError(UserError::CriterionError(error))
}
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub enum Criterion {
/// Sorted by decreasing number of matched query terms.
@ -85,35 +91,17 @@ impl FromStr for Criterion {
"attribute" => Ok(Criterion::Attribute),
"sort" => Ok(Criterion::Sort),
"exactness" => Ok(Criterion::Exactness),
text => match AscDesc::from_str(text) {
Ok(AscDesc::Asc(Member::Field(field))) => Ok(Criterion::Asc(field)),
Ok(AscDesc::Desc(Member::Field(field))) => Ok(Criterion::Desc(field)),
Ok(AscDesc::Asc(Member::Geo(_))) | Ok(AscDesc::Desc(Member::Geo(_))) => {
text => match AscDesc::from_str(text)? {
AscDesc::Asc(Member::Field(field)) => Ok(Criterion::Asc(field)),
AscDesc::Desc(Member::Field(field)) => Ok(Criterion::Desc(field)),
AscDesc::Asc(Member::Geo(_)) | AscDesc::Desc(Member::Geo(_)) => {
Err(CriterionError::ReservedNameForSort { name: "_geoPoint".to_string() })?
}
Err(AscDescError::InvalidSyntax { name }) => {
Err(CriterionError::InvalidName { name })?
}
Err(AscDescError::ReservedKeyword { name }) if name.starts_with("_geoPoint") => {
Err(CriterionError::ReservedNameForSort { name: "_geoPoint".to_string() })?
}
Err(AscDescError::ReservedKeyword { name }) if name.starts_with("_geoRadius") => {
Err(CriterionError::ReservedNameForFilter { name: "_geoRadius".to_string() })?
}
Err(AscDescError::ReservedKeyword { name }) => {
Err(CriterionError::ReservedName { name })?
}
},
}
}
}
impl From<CriterionError> for Error {
fn from(error: CriterionError) -> Self {
Self::UserError(UserError::CriterionError(error))
}
}
pub fn default_criteria() -> Vec<Criterion> {
vec![
Criterion::Words,