Merge the Float and Integer enum variant into the Number one

This commit is contained in:
Kerollmops 2021-04-07 11:13:47 +02:00
parent f5ec14c54c
commit efbfa81fa7
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4
3 changed files with 16 additions and 24 deletions

6
Cargo.lock generated
View File

@ -1522,8 +1522,7 @@ checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
[[package]] [[package]]
name = "pest" name = "pest"
version = "2.1.3" version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://github.com/pest-parser/pest.git?rev=51fd1d49f1041f7839975664ef71fe15c7dcaf67#51fd1d49f1041f7839975664ef71fe15c7dcaf67"
checksum = "10f4872ae94d7b90ae48754df22fd42ad52ce740b8f370b03da4835417403e53"
dependencies = [ dependencies = [
"ucd-trie", "ucd-trie",
] ]
@ -1531,7 +1530,8 @@ dependencies = [
[[package]] [[package]]
name = "pest" name = "pest"
version = "2.1.3" 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 = [ dependencies = [
"ucd-trie", "ucd-trie",
] ]

View File

@ -8,16 +8,14 @@ use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub enum FacetType { pub enum FacetType {
String, String,
Float, Number,
Integer,
} }
impl fmt::Display for FacetType { impl fmt::Display for FacetType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
FacetType::String => f.write_str("string"), FacetType::String => f.write_str("string"),
FacetType::Float => f.write_str("float"), FacetType::Number => f.write_str("number"),
FacetType::Integer => f.write_str("integer"),
} }
} }
} }
@ -26,12 +24,10 @@ impl FromStr for FacetType {
type Err = InvalidFacetType; type Err = InvalidFacetType;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.eq_ignore_ascii_case("string") { if s.trim().eq_ignore_ascii_case("string") {
Ok(FacetType::String) Ok(FacetType::String)
} else if s.eq_ignore_ascii_case("float") { } else if s.trim().eq_ignore_ascii_case("number") {
Ok(FacetType::Float) Ok(FacetType::Number)
} else if s.eq_ignore_ascii_case("integer") {
Ok(FacetType::Integer)
} else { } else {
Err(InvalidFacetType) Err(InvalidFacetType)
} }
@ -43,7 +39,7 @@ pub struct InvalidFacetType;
impl fmt::Display for InvalidFacetType { impl fmt::Display for InvalidFacetType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 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""#)
} }
} }

View File

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