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