use crate::sketchbook::data_structs::_layout_node_data::LayoutNodeDataPrototype;
use crate::sketchbook::ids::VarId;
use crate::sketchbook::model::{UpdateFn, Variable};
use crate::sketchbook::JsonSerde;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct VariableData {
    pub id: String,
    pub name: String,
    pub annotation: String,
    pub update_fn: String,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct VariableWithLayoutData {
    pub variable: VariableData,
    pub layouts: Vec<LayoutNodeDataPrototype>,
}
impl<'de> JsonSerde<'de> for VariableData {}
impl<'de> JsonSerde<'de> for VariableWithLayoutData {}
impl VariableData {
    pub fn new(id: &str, name: &str, annotation: &str, update_fn: &str) -> VariableData {
        VariableData {
            id: id.to_string(),
            name: name.to_string(),
            annotation: annotation.to_string(),
            update_fn: update_fn.to_string(),
        }
    }
    pub fn from_var(var_id: &VarId, variable: &Variable, update_fn: &UpdateFn) -> VariableData {
        VariableData::new(
            var_id.as_str(),
            variable.get_name(),
            variable.get_annotation(),
            update_fn.get_fn_expression(),
        )
    }
    pub fn to_var(&self) -> Result<Variable, String> {
        Variable::new_annotated(&self.name, &self.annotation)
    }
}