plottery_project/project_params/
project_param_value.rs

1use anyhow::Result;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
5pub enum ProjectParamValue {
6    Float(f32),
7    FloatRanged { val: f32, min: f32, max: f32 },
8    Int(i32),
9    IntRanged { val: i32, min: i32, max: i32 },
10    Bool(bool),
11}
12
13impl ProjectParamValue {
14    pub fn get_f32(&self) -> Result<f32> {
15        match self {
16            ProjectParamValue::Float(val) => Ok(*val),
17            ProjectParamValue::FloatRanged {
18                val,
19                min: _,
20                max: _,
21            } => Ok(*val),
22            _ => Err(anyhow::anyhow!(
23                "Failed to get_f32 - Expected value to be of type 'float' but found {}",
24                self.type_name()
25            )),
26        }
27    }
28    pub fn set_f32(&mut self, new_val: f32) {
29        match self {
30            ProjectParamValue::Float(val) => *val = new_val,
31            ProjectParamValue::FloatRanged { val, min, max } => *val = new_val.clamp(*min, *max),
32            _ => panic!("Failed to set_f32 - Type is '{}'.", self.type_name()),
33        }
34    }
35
36    pub fn get_i32(&self) -> Result<i32> {
37        match self {
38            ProjectParamValue::Int(val) => Ok(*val),
39            ProjectParamValue::IntRanged {
40                val,
41                min: _,
42                max: _,
43            } => Ok(*val),
44            _ => Err(anyhow::anyhow!(
45                "Failed to get_i32 - Expected value to be of type 'int' but found {}",
46                self.type_name()
47            )),
48        }
49    }
50    pub fn set_i32(&mut self, new_val: i32) {
51        match self {
52            ProjectParamValue::Int(val) => *val = new_val,
53            ProjectParamValue::IntRanged { val, min, max } => *val = new_val.clamp(*min, *max),
54            _ => panic!("Failed to set_i32 - Type is '{}'.", self.type_name()),
55        }
56    }
57
58    pub fn get_bool(&self) -> Result<bool> {
59        match self {
60            ProjectParamValue::Bool(val) => Ok(*val),
61            _ => Err(anyhow::anyhow!(
62                "Failed to get_bool - Expected value to be of type 'bool' but found {}",
63                self.type_name()
64            )),
65        }
66    }
67    pub fn set_bool(&mut self, new_val: bool) {
68        match self {
69            ProjectParamValue::Bool(val) => *val = new_val,
70            _ => panic!("Failed to set_bool - Type is '{}'.", self.type_name()),
71        }
72    }
73
74    pub fn type_name(&self) -> String {
75        match self {
76            ProjectParamValue::Float(_) => "f32".to_string(),
77            ProjectParamValue::FloatRanged { .. } => "f32 (ranged)".to_string(),
78            ProjectParamValue::Int(_) => "i32".to_string(),
79            ProjectParamValue::IntRanged { .. } => "i32 (ranged)".to_string(),
80            ProjectParamValue::Bool(_) => "bool".to_string(),
81        }
82    }
83
84    pub fn value_as_string(&self) -> String {
85        match self {
86            ProjectParamValue::Float(val) => val.to_string(),
87            ProjectParamValue::FloatRanged { val, .. } => val.to_string(),
88            ProjectParamValue::Int(val) => val.to_string(),
89            ProjectParamValue::IntRanged { val, .. } => val.to_string(),
90            ProjectParamValue::Bool(val) => val.to_string(),
91        }
92    }
93}