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