Change the project to become a workspace with milli as a default-member

This commit is contained in:
Clément Renault 2021-02-12 16:15:09 +01:00
parent d450b971f9
commit e8639517da
No known key found for this signature in database
GPG key ID: 92ADA4E935E71FA4
56 changed files with 1053 additions and 2617 deletions

View file

@ -0,0 +1,50 @@
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,
Float,
Integer,
}
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 { }

View file

@ -0,0 +1,60 @@
use ordered_float::OrderedFloat;
use serde::{Serialize, Serializer};
#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub enum FacetValue {
String(String),
Float(OrderedFloat<f64>),
Integer(i64),
}
impl From<String> for FacetValue {
fn from(string: String) -> FacetValue {
FacetValue::String(string)
}
}
impl From<&str> for FacetValue {
fn from(string: &str) -> FacetValue {
FacetValue::String(string.to_owned())
}
}
impl From<f64> for FacetValue {
fn from(float: f64) -> FacetValue {
FacetValue::Float(OrderedFloat(float))
}
}
impl From<OrderedFloat<f64>> for FacetValue {
fn from(float: OrderedFloat<f64>) -> FacetValue {
FacetValue::Float(float)
}
}
impl From<i64> for FacetValue {
fn from(integer: i64) -> FacetValue {
FacetValue::Integer(integer)
}
}
/// We implement Serialize ourselves because we need to always serialize it as a string,
/// JSON object keys must be strings not numbers.
impl Serialize for FacetValue {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
FacetValue::String(string) => serializer.serialize_str(string),
FacetValue::Float(float) => {
let string = float.to_string();
serializer.serialize_str(&string)
},
FacetValue::Integer(integer) => {
let string = integer.to_string();
serializer.serialize_str(&string)
},
}
}
}

6
milli/src/facet/mod.rs Normal file
View file

@ -0,0 +1,6 @@
mod facet_type;
mod facet_value;
pub mod value_encoding;
pub use self::facet_type::FacetType;
pub use self::facet_value::FacetValue;

View file

@ -0,0 +1,69 @@
// https://stackoverflow.com/a/43305015/1941280
#[inline]
pub fn f64_into_bytes(float: f64) -> Option<[u8; 8]> {
if float.is_finite() {
if float == 0.0 || float == -0.0 {
return Some(xor_first_bit(0.0_f64.to_be_bytes()));
} else if float.is_sign_negative() {
return Some(xor_all_bits(float.to_be_bytes()));
} else if float.is_sign_positive() {
return Some(xor_first_bit(float.to_be_bytes()));
}
}
None
}
#[inline]
pub fn i64_into_bytes(int: i64) -> [u8; 8] {
xor_first_bit(int.to_be_bytes())
}
#[inline]
pub fn i64_from_bytes(bytes: [u8; 8]) -> i64 {
i64::from_be_bytes(xor_first_bit(bytes))
}
#[inline]
fn xor_first_bit(mut x: [u8; 8]) -> [u8; 8] {
x[0] ^= 0x80;
x
}
#[inline]
fn xor_all_bits(mut x: [u8; 8]) -> [u8; 8] {
x.iter_mut().for_each(|b| *b ^= 0xff);
x
}
#[cfg(test)]
mod tests {
use std::cmp::Ordering::Less;
use super::*;
fn is_sorted<T: Ord>(x: &[T]) -> bool {
x.windows(2).map(|x| x[0].cmp(&x[1])).all(|o| o == Less)
}
#[test]
fn ordered_f64_bytes() {
let a = -13_f64;
let b = -10.0;
let c = -0.0;
let d = 1.0;
let e = 43.0;
let vec: Vec<_> = [a, b, c, d, e].iter().cloned().map(f64_into_bytes).collect();
assert!(is_sorted(&vec), "{:?}", vec);
}
#[test]
fn ordered_i64_bytes() {
let a = -10_i64;
let b = -0_i64;
let c = 1_i64;
let d = 43_i64;
let vec: Vec<_> = [a, b, c, d].iter().cloned().map(i64_into_bytes).collect();
assert!(is_sorted(&vec), "{:?}", vec);
}
}