MeiliSearch/milli/src/facet/facet_type.rs

46 lines
1.1 KiB
Rust
Raw Normal View History

2020-11-13 11:45:37 +01:00
use std::error::Error;
use std::fmt;
use std::str::FromStr;
2021-06-16 18:33:33 +02:00
use serde::{Deserialize, Serialize};
2021-06-16 18:33:33 +02:00
#[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum FacetType {
String,
Number,
2020-11-13 11:45:37 +01:00
}
impl fmt::Display for FacetType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
FacetType::String => f.write_str("string"),
FacetType::Number => f.write_str("number"),
2020-11-13 11:45:37 +01:00
}
}
}
2020-11-13 11:45:37 +01:00
impl FromStr for FacetType {
type Err = InvalidFacetType;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.trim().eq_ignore_ascii_case("string") {
2020-11-13 11:45:37 +01:00
Ok(FacetType::String)
} else if s.trim().eq_ignore_ascii_case("number") {
Ok(FacetType::Number)
2020-11-13 11:45:37 +01:00
} else {
Err(InvalidFacetType)
}
}
}
2020-11-13 11:45:37 +01:00
#[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct InvalidFacetType;
impl fmt::Display for InvalidFacetType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(r#"Invalid facet type, must be "string" or "number""#)
2020-11-13 11:45:37 +01:00
}
}
2021-06-16 18:33:33 +02:00
impl Error for InvalidFacetType {}