From e41c551757f67c4989652287a65b7269b776a9e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Fri, 29 Mar 2019 17:38:29 +0100 Subject: [PATCH] feat: Introduce the Number type --- meilidb-data/Cargo.toml | 1 + meilidb-data/src/lib.rs | 8 ++++-- meilidb-data/src/number.rs | 55 ++++++++++++++++++++++++++++++++++++++ meilidb-data/src/schema.rs | 2 -- 4 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 meilidb-data/src/number.rs diff --git a/meilidb-data/Cargo.toml b/meilidb-data/Cargo.toml index f0046bc1a..18881367b 100644 --- a/meilidb-data/Cargo.toml +++ b/meilidb-data/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" bincode = "1.1.2" linked-hash-map = { version = "0.5.2", features = ["serde_impl"] } meilidb-core = { path = "../meilidb-core", version = "0.1.0" } +ordered-float = { version = "1.0.2", features = ["serde"] } serde = { version = "1.0.88", features = ["derive"] } serde_json = { version = "1.0.39", features = ["preserve_order"] } sled = "0.20.0" diff --git a/meilidb-data/src/lib.rs b/meilidb-data/src/lib.rs index a2f028ecd..96d6bdf6e 100644 --- a/meilidb-data/src/lib.rs +++ b/meilidb-data/src/lib.rs @@ -1,5 +1,9 @@ mod database; -mod schema; +pub mod schema; +mod ranked_map; +mod number; pub use self::database::{Database, Index}; -pub use self::schema::{Schema, SchemaAttr, SchemaBuilder}; +pub use self::schema::{Schema, SchemaAttr}; +pub use self::ranked_map::RankedMap; +pub use self::number::Number; diff --git a/meilidb-data/src/number.rs b/meilidb-data/src/number.rs new file mode 100644 index 000000000..9a2d0ea24 --- /dev/null +++ b/meilidb-data/src/number.rs @@ -0,0 +1,55 @@ +use std::num::{ParseIntError, ParseFloatError}; +use std::str::FromStr; +use std::fmt; + +use ordered_float::OrderedFloat; +use serde::{Serialize, Deserialize}; + +#[derive(Serialize, Deserialize)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum Number { + Unsigned(u64), + Signed(i64), + Float(OrderedFloat), +} + +impl FromStr for Number { + type Err = ParseNumberError; + + fn from_str(s: &str) -> Result { + let uint_error = match u64::from_str(s) { + Ok(unsigned) => return Ok(Number::Unsigned(unsigned)), + Err(error) => error, + }; + + let int_error = match i64::from_str(s) { + Ok(signed) => return Ok(Number::Signed(signed)), + Err(error) => error, + }; + + let float_error = match f64::from_str(s) { + Ok(float) => return Ok(Number::Float(OrderedFloat(float))), + Err(error) => error, + }; + + Err(ParseNumberError { uint_error, int_error, float_error }) + } +} + +#[derive(Clone, PartialEq, Eq)] +pub struct ParseNumberError { + uint_error: ParseIntError, + int_error: ParseIntError, + float_error: ParseFloatError, +} + +impl fmt::Display for ParseNumberError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if self.uint_error == self.int_error { + write!(f, "can not parse number: {}, {}", self.uint_error, self.float_error) + } else { + write!(f, "can not parse number: {}, {}, {}", + self.uint_error, self.int_error, self.float_error) + } + } +} diff --git a/meilidb-data/src/schema.rs b/meilidb-data/src/schema.rs index c73b8b067..bff7806dc 100644 --- a/meilidb-data/src/schema.rs +++ b/meilidb-data/src/schema.rs @@ -8,8 +8,6 @@ use std::sync::Arc; use serde::{Serialize, Deserialize}; use linked_hash_map::LinkedHashMap; -use meilidb_core::DocumentId; - pub const STORED: SchemaProps = SchemaProps { stored: true, indexed: false, ranked: false }; pub const INDEXED: SchemaProps = SchemaProps { stored: false, indexed: true, ranked: false }; pub const RANKED: SchemaProps = SchemaProps { stored: false, indexed: false, ranked: true };