From efbfa81fa7dcfee29fc261530d10f1647242a718 Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Wed, 7 Apr 2021 11:13:47 +0200 Subject: [PATCH] Merge the Float and Integer enum variant into the Number one --- Cargo.lock | 6 +++--- milli/src/facet/facet_type.rs | 16 ++++++---------- milli/src/facet/facet_value.rs | 18 +++++++----------- 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d28745d8b..7facf6e8b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1522,8 +1522,7 @@ checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" [[package]] name = "pest" version = "2.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10f4872ae94d7b90ae48754df22fd42ad52ce740b8f370b03da4835417403e53" +source = "git+https://github.com/pest-parser/pest.git?rev=51fd1d49f1041f7839975664ef71fe15c7dcaf67#51fd1d49f1041f7839975664ef71fe15c7dcaf67" dependencies = [ "ucd-trie", ] @@ -1531,7 +1530,8 @@ dependencies = [ [[package]] name = "pest" version = "2.1.3" -source = "git+https://github.com/pest-parser/pest.git?rev=51fd1d49f1041f7839975664ef71fe15c7dcaf67#51fd1d49f1041f7839975664ef71fe15c7dcaf67" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10f4872ae94d7b90ae48754df22fd42ad52ce740b8f370b03da4835417403e53" dependencies = [ "ucd-trie", ] diff --git a/milli/src/facet/facet_type.rs b/milli/src/facet/facet_type.rs index 4fdc80798..09f29bc00 100644 --- a/milli/src/facet/facet_type.rs +++ b/milli/src/facet/facet_type.rs @@ -8,16 +8,14 @@ use serde::{Serialize, Deserialize}; #[derive(Serialize, Deserialize)] pub enum FacetType { String, - Float, - Integer, + Number, } 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"), + FacetType::Number => f.write_str("number"), } } } @@ -26,12 +24,10 @@ impl FromStr for FacetType { type Err = InvalidFacetType; fn from_str(s: &str) -> Result { - if s.eq_ignore_ascii_case("string") { + if s.trim().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 if s.trim().eq_ignore_ascii_case("number") { + Ok(FacetType::Number) } else { Err(InvalidFacetType) } @@ -43,7 +39,7 @@ 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""#) + f.write_str(r#"Invalid facet type, must be "string" or "number""#) } } diff --git a/milli/src/facet/facet_value.rs b/milli/src/facet/facet_value.rs index f311ca3dd..2fd2fdf40 100644 --- a/milli/src/facet/facet_value.rs +++ b/milli/src/facet/facet_value.rs @@ -4,8 +4,7 @@ use serde::{Serialize, Serializer}; #[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)] pub enum FacetValue { String(String), - Float(OrderedFloat), - Integer(i64), + Number(OrderedFloat), } impl From for FacetValue { @@ -22,24 +21,25 @@ impl From<&str> for FacetValue { impl From for FacetValue { fn from(float: f64) -> FacetValue { - FacetValue::Float(OrderedFloat(float)) + FacetValue::Number(OrderedFloat(float)) } } impl From> for FacetValue { fn from(float: OrderedFloat) -> FacetValue { - FacetValue::Float(float) + FacetValue::Number(float) } } impl From for FacetValue { fn from(integer: i64) -> FacetValue { - FacetValue::Integer(integer) + FacetValue::Number(integer as f64) } } /// We implement Serialize ourselves because we need to always serialize it as a string, /// JSON object keys must be strings not numbers. +// TODO remove this impl and convert them into string, by hand, when required. impl Serialize for FacetValue { fn serialize(&self, serializer: S) -> Result where @@ -47,12 +47,8 @@ impl Serialize for FacetValue { { 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(); + FacetValue::Number(number) => { + let string = number.to_string(); serializer.serialize_str(&string) }, }