Change the Asc/Desc criterion syntax to use a colon (:)

This commit is contained in:
Kerollmops 2021-08-17 14:03:21 +02:00
parent e9ada44509
commit fcedff95e8
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4
2 changed files with 5 additions and 22 deletions

View File

@ -25,7 +25,6 @@ obkv = "0.2.0"
once_cell = "1.5.2"
ordered-float = "2.1.1"
rayon = "1.5.0"
regex = "1.4.3"
roaring = "0.6.6"
serde = { version = "1.0.123", features = ["derive"] }
serde_json = { version = "1.0.62", features = ["preserve_order"] }

View File

@ -1,15 +1,10 @@
use std::fmt;
use std::str::FromStr;
use once_cell::sync::Lazy;
use regex::Regex;
use serde::{Deserialize, Serialize};
use crate::error::{Error, UserError};
static ASC_DESC_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r#"(asc|desc)\(([\w_-]+)\)"#).unwrap());
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub enum Criterion {
/// Sorted by decreasing number of matched query terms.
@ -50,22 +45,11 @@ impl FromStr for Criterion {
"proximity" => Ok(Criterion::Proximity),
"attribute" => Ok(Criterion::Attribute),
"exactness" => Ok(Criterion::Exactness),
text => {
let caps = ASC_DESC_REGEX
.captures(text)
.ok_or_else(|| UserError::InvalidCriterionName { name: text.to_string() })?;
let order = caps.get(1).unwrap().as_str();
let field_name = caps.get(2).unwrap().as_str();
match order {
"asc" => Ok(Criterion::Asc(field_name.to_string())),
"desc" => Ok(Criterion::Desc(field_name.to_string())),
text => {
return Err(
UserError::InvalidCriterionName { name: text.to_string() }.into()
)
}
}
}
text => match text.rsplit_once(':') {
Some((field_name, "asc")) => Ok(Criterion::Asc(field_name.to_string())),
Some((field_name, "desc")) => Ok(Criterion::Desc(field_name.to_string())),
_ => Err(UserError::InvalidCriterionName { name: text.to_string() }.into()),
},
}
}
}