photonic_interface_grpc_client/
values.rs1use std::fmt;
2use std::str::FromStr;
3
4use palette::Srgb;
5
6use photonic_interface_grpc_proto::input_value::{ColorRange, DecimalRange, IntegerRange, Rgb};
7
8#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
9pub enum ValueType {
10 Trigger,
11 Bool,
12 Integer,
13 Decimal,
14 Color,
15 IntegerRange,
16 DecimalRange,
17 ColorRange,
18}
19
20impl fmt::Display for ValueType {
21 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22 return f.write_str(match self {
23 Self::Trigger => "trigger",
24 Self::Bool => "bool",
25 Self::Integer => "integer",
26 Self::Decimal => "decimal",
27 Self::Color => "color",
28 Self::IntegerRange => "range<integer>",
29 Self::DecimalRange => "range<decimal>",
30 Self::ColorRange => "range<color>",
31 });
32 }
33}
34
35#[derive(Copy, Clone)]
36pub struct ColorValue {
37 pub r: f32,
38 pub g: f32,
39 pub b: f32,
40}
41
42impl FromStr for ColorValue {
43 type Err = anyhow::Error;
44
45 fn from_str(s: &str) -> Result<Self, Self::Err> {
46 let rgb = s.parse::<Srgb<u8>>()?;
47 let rgb = rgb.into_format::<f32>();
48
49 return Ok(Self {
50 r: rgb.red,
51 g: rgb.green,
52 b: rgb.blue,
53 });
54 }
55}
56
57impl From<ColorValue> for Rgb {
58 fn from(val: ColorValue) -> Self {
59 return Rgb {
60 r: val.r,
61 g: val.g,
62 b: val.b,
63 };
64 }
65}
66
67pub struct RangeValue<V> {
68 pub a: V,
69 pub b: V,
70}
71
72impl<V> FromStr for RangeValue<V>
73where
74 V: FromStr + Copy,
75 <V as FromStr>::Err: Into<anyhow::Error>,
76{
77 type Err = anyhow::Error;
78
79 fn from_str(s: &str) -> Result<Self, Self::Err> {
80 return Ok(if let Some((a, b)) = s.split_once("..") {
81 Self {
82 a: a.parse().map_err(Into::into)?,
83 b: b.parse().map_err(Into::into)?,
84 }
85 } else {
86 let s = s.parse().map_err(Into::into)?;
87 Self {
88 a: s,
89 b: s,
90 }
91 });
92 }
93}
94
95impl From<RangeValue<i64>> for IntegerRange {
96 fn from(val: RangeValue<i64>) -> Self {
97 return IntegerRange {
98 a: val.a,
99 b: val.b,
100 };
101 }
102}
103
104impl From<RangeValue<f32>> for DecimalRange {
105 fn from(val: RangeValue<f32>) -> Self {
106 return DecimalRange {
107 a: val.a,
108 b: val.b,
109 };
110 }
111}
112
113impl From<RangeValue<ColorValue>> for ColorRange {
114 fn from(val: RangeValue<ColorValue>) -> Self {
115 return ColorRange {
116 a: Some(val.a.into()),
117 b: Some(val.b.into()),
118 };
119 }
120}