use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Value {
Float(f64),
Int(i64),
Categorical(String),
}
impl Value {
pub fn as_float(&self) -> Option<f64> {
match self {
Value::Float(x) => Some(*x),
_ => None,
}
}
pub fn as_int(&self) -> Option<i64> {
match self {
Value::Int(x) => Some(*x),
_ => None,
}
}
pub fn as_categorical(&self) -> Option<&str> {
match self {
Value::Categorical(s) => Some(s.as_str()),
_ => None,
}
}
pub fn to_f64(&self) -> Option<f64> {
match self {
Value::Float(x) => Some(*x),
Value::Int(x) => Some(*x as f64),
Value::Categorical(_) => None,
}
}
}
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Value::Float(x) => write!(f, "{x}"),
Value::Int(x) => write!(f, "{x}"),
Value::Categorical(s) => write!(f, "{s}"),
}
}
}