use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum StringifiedDouble {
StringValue(String),
NumberValue(f64),
}
impl std::fmt::Display for StringifiedDouble {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
StringifiedDouble::StringValue(s) => write!(f, "{}", s),
StringifiedDouble::NumberValue(n) => write!(f, "{}", n),
}
}
}
impl StringifiedDouble {
pub fn as_str(&self) -> String {
match self {
StringifiedDouble::StringValue(s) => s.clone(),
StringifiedDouble::NumberValue(n) => n.to_string(),
}
}
pub fn as_num(&self) -> Option<f64> {
match self {
StringifiedDouble::NumberValue(n) => Some(*n),
_ => None,
}
}
}
impl From<f64> for StringifiedDouble {
fn from(n: f64) -> Self {
StringifiedDouble::NumberValue(n)
}
}
impl From<&str> for StringifiedDouble {
fn from(s: &str) -> Self {
StringifiedDouble::StringValue(s.to_string())
}
}
impl From<String> for StringifiedDouble {
fn from(s: String) -> Self {
StringifiedDouble::StringValue(s)
}
}