mirror of
https://github.com/meilisearch/meilisearch.git
synced 2025-10-28 22:46:28 +00:00
Simplify the the facet types
This commit is contained in:
@@ -1,17 +1,50 @@
|
||||
use std::cmp;
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
use std::str::FromStr;
|
||||
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub enum FacetType {
|
||||
String,
|
||||
F64,
|
||||
I64,
|
||||
U64,
|
||||
Float,
|
||||
Integer,
|
||||
}
|
||||
|
||||
impl FacetType {
|
||||
pub fn merge(a: FacetType, b: FacetType) -> FacetType {
|
||||
cmp::min(a, b)
|
||||
impl fmt::Display for FacetType {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
FacetType::String => f.write_str("string"),
|
||||
FacetType::Float => f.write_str("float"),
|
||||
FacetType::Integer => f.write_str("integer"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for FacetType {
|
||||
type Err = InvalidFacetType;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
if s.eq_ignore_ascii_case("string") {
|
||||
Ok(FacetType::String)
|
||||
} else if s.eq_ignore_ascii_case("float") {
|
||||
Ok(FacetType::Float)
|
||||
} else if s.eq_ignore_ascii_case("integer") {
|
||||
Ok(FacetType::Integer)
|
||||
} else {
|
||||
Err(InvalidFacetType)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[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", "float" or "integer""#)
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for InvalidFacetType { }
|
||||
|
||||
@@ -13,16 +13,6 @@ pub fn f64_into_bytes(float: f64) -> Option<[u8; 8]> {
|
||||
None
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn u64_into_bytes(int: u64) -> [u8; 8] {
|
||||
int.to_be_bytes()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn u64_from_bytes(bytes: [u8; 8]) -> u64 {
|
||||
u64::from_be_bytes(bytes)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn i64_into_bytes(int: i64) -> [u8; 8] {
|
||||
xor_first_bit(int.to_be_bytes())
|
||||
@@ -66,16 +56,6 @@ mod tests {
|
||||
assert!(is_sorted(&vec), "{:?}", vec);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ordered_u64_bytes() {
|
||||
let a = 0_u64;
|
||||
let b = 1_u64;
|
||||
let c = 43_u64;
|
||||
|
||||
let vec: Vec<_> = [a, b, c].iter().cloned().map(u64_into_bytes).collect();
|
||||
assert!(is_sorted(&vec), "{:?}", vec);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ordered_i64_bytes() {
|
||||
let a = -10_i64;
|
||||
|
||||
Reference in New Issue
Block a user