use ordered_float::OrderedFloat; use serde::{Serialize, Serializer}; #[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)] pub enum FacetValue { String(String), Float(OrderedFloat), Integer(i64), } impl From 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 for FacetValue { fn from(float: f64) -> FacetValue { FacetValue::Float(OrderedFloat(float)) } } impl From> for FacetValue { fn from(float: OrderedFloat) -> FacetValue { FacetValue::Float(float) } } impl From for FacetValue { fn from(integer: i64) -> FacetValue { FacetValue::Integer(integer) } } /// We implement Serialize ourselves because we need to always serialize it as a string, /// JSON object keys must be strings not numbers. impl Serialize for FacetValue { fn serialize(&self, serializer: S) -> Result where S: Serializer, { 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(); serializer.serialize_str(&string) }, } } }