use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum PropValue {
String(String),
Number(f64),
Bool(bool),
Nil,
Color { r: f64, g: f64, b: f64, a: f64 },
ActionRef {
#[serde(rename = "__action")]
action: String,
#[serde(rename = "__args", skip_serializing_if = "Option::is_none")]
args: Option<Vec<PropValue>>,
},
Lambda {
#[serde(rename = "__lambda")]
lambda_id: u32,
},
List(Vec<PropValue>),
Record(BTreeMap<String, PropValue>),
}
impl PropValue {
pub fn action(name: impl Into<String>) -> Self {
PropValue::ActionRef {
action: name.into(),
args: None,
}
}
pub fn action_with_args(name: impl Into<String>, args: Vec<PropValue>) -> Self {
PropValue::ActionRef {
action: name.into(),
args: Some(args),
}
}
pub fn lambda(id: u32) -> Self {
PropValue::Lambda { lambda_id: id }
}
pub fn color(r: f64, g: f64, b: f64, a: f64) -> Self {
PropValue::Color { r, g, b, a }
}
pub fn type_name(&self) -> &'static str {
match self {
PropValue::String(_) => "string",
PropValue::Number(_) => "number",
PropValue::Bool(_) => "bool",
PropValue::Nil => "nil",
PropValue::Color { .. } => "color",
PropValue::ActionRef { .. } => "action",
PropValue::Lambda { .. } => "lambda",
PropValue::List(_) => "list",
PropValue::Record(_) => "record",
}
}
}
impl From<&str> for PropValue {
fn from(s: &str) -> Self {
PropValue::String(s.to_string())
}
}
impl From<String> for PropValue {
fn from(s: String) -> Self {
PropValue::String(s)
}
}
impl From<f64> for PropValue {
fn from(n: f64) -> Self {
PropValue::Number(n)
}
}
impl From<i64> for PropValue {
fn from(n: i64) -> Self {
PropValue::Number(n as f64)
}
}
impl From<bool> for PropValue {
fn from(b: bool) -> Self {
PropValue::Bool(b)
}
}