diff --git a/src/facet/facet_type.rs b/src/facet/facet_type.rs index 93c98316d..4fdc80798 100644 --- a/src/facet/facet_type.rs +++ b/src/facet/facet_type.rs @@ -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 { + 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 { } diff --git a/src/facet/value_encoding.rs b/src/facet/value_encoding.rs index 01fdba723..3cb012a0e 100644 --- a/src/facet/value_encoding.rs +++ b/src/facet/value_encoding.rs @@ -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; diff --git a/src/heed_codec/facet/facet_value_u64_codec.rs b/src/heed_codec/facet/facet_value_u64_codec.rs deleted file mode 100644 index 2fdc7416e..000000000 --- a/src/heed_codec/facet/facet_value_u64_codec.rs +++ /dev/null @@ -1,28 +0,0 @@ -use std::borrow::Cow; -use std::convert::TryInto; -use std::str; - -use crate::heed_codec::StrBytesCodec; -use crate::facet::value_encoding::{u64_from_bytes, u64_into_bytes}; - -pub struct FacetValueU64Codec; - -impl<'a> heed::BytesDecode<'a> for FacetValueU64Codec { - type DItem = (&'a str, u64); - - fn bytes_decode(bytes: &'a [u8]) -> Option { - let (name, bytes) = StrBytesCodec::bytes_decode(bytes)?; - let value = bytes.try_into().map(u64_from_bytes).ok()?; - Some((name, value)) - } -} - -impl<'a> heed::BytesEncode<'a> for FacetValueU64Codec { - type EItem = (&'a str, u64); - - fn bytes_encode((name, value): &Self::EItem) -> Option> { - let value = u64_into_bytes(*value); - let tuple = (*name, &value[..]); - StrBytesCodec::bytes_encode(&tuple).map(Cow::into_owned).map(Cow::Owned) - } -} diff --git a/src/heed_codec/facet/mod.rs b/src/heed_codec/facet/mod.rs index ba34f6220..abe2c1d8a 100644 --- a/src/heed_codec/facet/mod.rs +++ b/src/heed_codec/facet/mod.rs @@ -1,9 +1,7 @@ mod facet_value_f64_codec; mod facet_value_i64_codec; mod facet_value_string_codec; -mod facet_value_u64_codec; pub use self::facet_value_f64_codec::FacetValueF64Codec; pub use self::facet_value_i64_codec::FacetValueI64Codec; pub use self::facet_value_string_codec::FacetValueStringCodec; -pub use self::facet_value_u64_codec::FacetValueU64Codec;