2021-01-13 18:59:16 +08:00
|
|
|
use ordered_float::OrderedFloat;
|
2021-01-26 21:14:37 +08:00
|
|
|
use serde::{Serialize, Serializer};
|
2021-01-13 18:59:16 +08:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
|
|
|
|
pub enum FacetValue {
|
|
|
|
String(String),
|
2021-04-07 17:13:47 +08:00
|
|
|
Number(OrderedFloat<f64>),
|
2021-01-13 18:59:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2021-04-07 17:13:47 +08:00
|
|
|
FacetValue::Number(OrderedFloat(float))
|
2021-01-13 18:59:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<OrderedFloat<f64>> for FacetValue {
|
|
|
|
fn from(float: OrderedFloat<f64>) -> FacetValue {
|
2021-04-07 17:13:47 +08:00
|
|
|
FacetValue::Number(float)
|
2021-01-13 18:59:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<i64> for FacetValue {
|
|
|
|
fn from(integer: i64) -> FacetValue {
|
2021-04-07 17:13:47 +08:00
|
|
|
FacetValue::Number(integer as f64)
|
2021-01-13 18:59:16 +08:00
|
|
|
}
|
|
|
|
}
|
2021-01-26 21:14:37 +08:00
|
|
|
|
2021-01-27 23:31:57 +08:00
|
|
|
/// We implement Serialize ourselves because we need to always serialize it as a string,
|
|
|
|
/// JSON object keys must be strings not numbers.
|
2021-04-07 17:13:47 +08:00
|
|
|
// TODO remove this impl and convert them into string, by hand, when required.
|
2021-01-26 21:14:37 +08:00
|
|
|
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),
|
2021-04-07 17:13:47 +08:00
|
|
|
FacetValue::Number(number) => {
|
|
|
|
let string = number.to_string();
|
2021-01-26 21:14:37 +08:00
|
|
|
serializer.serialize_str(&string)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|