mirror of
https://github.com/meilisearch/meilisearch.git
synced 2025-07-26 16:21:07 +00:00
Use the Error enum everywhere in the project
This commit is contained in:
@ -1,11 +1,12 @@
|
||||
use std::fmt;
|
||||
use std::str::FromStr;
|
||||
|
||||
use anyhow::{Context, bail};
|
||||
use regex::Regex;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
use crate::error::{Error, UserError};
|
||||
|
||||
static ASC_DESC_REGEX: Lazy<Regex> = Lazy::new(|| {
|
||||
Regex::new(r#"(asc|desc)\(([\w_-]+)\)"#).unwrap()
|
||||
});
|
||||
@ -41,7 +42,7 @@ impl Criterion {
|
||||
}
|
||||
|
||||
impl FromStr for Criterion {
|
||||
type Err = anyhow::Error;
|
||||
type Err = Error;
|
||||
|
||||
fn from_str(txt: &str) -> Result<Criterion, Self::Err> {
|
||||
match txt {
|
||||
@ -51,13 +52,15 @@ impl FromStr for Criterion {
|
||||
"attribute" => Ok(Criterion::Attribute),
|
||||
"exactness" => Ok(Criterion::Exactness),
|
||||
text => {
|
||||
let caps = ASC_DESC_REGEX.captures(text).with_context(|| format!("unknown criterion name: {}", 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())),
|
||||
otherwise => bail!("unknown criterion name: {}", otherwise),
|
||||
text => return Err(UserError::InvalidCriterionName { name: text.to_string() }.into()),
|
||||
}
|
||||
},
|
||||
}
|
||||
|
Reference in New Issue
Block a user