use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", content = "value", rename_all = "snake_case")]
pub enum Value {
Bool(bool),
Int(i64),
Float(f64),
Ratio(f64),
Text(String),
Point {
x: f64,
y: f64,
},
Rect {
x: f64,
y: f64,
w: f64,
h: f64,
},
Enum(String),
}
impl Value {
pub fn type_name(&self) -> &'static str {
match self {
Value::Bool(_) => "bool",
Value::Int(_) => "int",
Value::Float(_) => "float",
Value::Ratio(_) => "ratio",
Value::Text(_) => "text",
Value::Point { .. } => "point",
Value::Rect { .. } => "rect",
Value::Enum(_) => "enum",
}
}
pub fn is_well_formed(&self) -> bool {
match self {
Value::Ratio(r) => (0.0..=1.0).contains(r),
Value::Float(f) => f.is_finite(),
Value::Point { x, y } => x.is_finite() && y.is_finite(),
Value::Rect { x, y, w, h } => {
x.is_finite() && y.is_finite() && w.is_finite() && h.is_finite()
}
_ => true,
}
}
}