feat: Introduce the Number type

This commit is contained in:
Clément Renault 2019-03-29 17:38:29 +01:00
parent 95dfbd1fe0
commit e41c551757
No known key found for this signature in database
GPG Key ID: 0151CDAB43460DAE
4 changed files with 62 additions and 4 deletions

View File

@ -8,6 +8,7 @@ edition = "2018"
bincode = "1.1.2" bincode = "1.1.2"
linked-hash-map = { version = "0.5.2", features = ["serde_impl"] } linked-hash-map = { version = "0.5.2", features = ["serde_impl"] }
meilidb-core = { path = "../meilidb-core", version = "0.1.0" } 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 = { version = "1.0.88", features = ["derive"] }
serde_json = { version = "1.0.39", features = ["preserve_order"] } serde_json = { version = "1.0.39", features = ["preserve_order"] }
sled = "0.20.0" sled = "0.20.0"

View File

@ -1,5 +1,9 @@
mod database; mod database;
mod schema; pub mod schema;
mod ranked_map;
mod number;
pub use self::database::{Database, Index}; 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;

View File

@ -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<f64>),
}
impl FromStr for Number {
type Err = ParseNumberError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
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)
}
}
}

View File

@ -8,8 +8,6 @@ use std::sync::Arc;
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use linked_hash_map::LinkedHashMap; use linked_hash_map::LinkedHashMap;
use meilidb_core::DocumentId;
pub const STORED: SchemaProps = SchemaProps { stored: true, indexed: false, ranked: false }; pub const STORED: SchemaProps = SchemaProps { stored: true, indexed: false, ranked: false };
pub const INDEXED: SchemaProps = SchemaProps { stored: false, indexed: true, ranked: false }; pub const INDEXED: SchemaProps = SchemaProps { stored: false, indexed: true, ranked: false };
pub const RANKED: SchemaProps = SchemaProps { stored: false, indexed: false, ranked: true }; pub const RANKED: SchemaProps = SchemaProps { stored: false, indexed: false, ranked: true };