use core::fmt;
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Value {
Number(f64),
Text(String),
Bool(bool),
}
impl Value {
#[must_use]
pub fn is_truthy(&self) -> bool {
match self {
Self::Bool(v) => *v,
Self::Number(v) => *v != 0.0,
Self::Text(v) => !v.is_empty(),
}
}
}
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Number(v) => {
if v.fract() == 0.0 && v.abs() < 1e15 {
write!(f, "{v:.0}")
} else {
write!(f, "{v}")
}
}
Self::Text(v) => f.write_str(v),
Self::Bool(v) => write!(f, "{v}"),
}
}
}
impl From<f64> for Value {
fn from(v: f64) -> Self {
Self::Number(v)
}
}
impl From<bool> for Value {
fn from(v: bool) -> Self {
Self::Bool(v)
}
}
impl From<String> for Value {
fn from(v: String) -> Self {
Self::Text(v)
}
}
impl From<&str> for Value {
fn from(v: &str) -> Self {
Self::Text(v.to_owned())
}
}
#[cfg(test)]
#[path = "types_tests.rs"]
mod tests;