plausible_rs/api/event/
prop_value.rs

1use serde::{Deserialize, Serialize};
2
3/// Custom properties only accepts scalar values such as strings, numbers and booleans.
4/// Data structures such as objects, arrays etc. aren't accepted.
5// Implementation on how to constrain types easily from: https://stackoverflow.com/a/52582432/11767294
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub enum PropValue {
8    // string
9    String(String),
10
11    // bool
12    Bool(bool),
13
14    // numbers
15    U8(u8),
16    U16(u16),
17    U32(u32),
18    U64(u64),
19    U128(u128),
20    Usize(usize),
21
22    I8(i8),
23    I16(i16),
24    I32(i32),
25    I64(i64),
26    I128(i128),
27    Isize(isize),
28
29    F32(f32),
30    F64(f64),
31}
32
33impl From<String> for PropValue {
34    fn from(s: String) -> Self {
35        Self::String(s)
36    }
37}
38
39impl From<bool> for PropValue {
40    fn from(b: bool) -> Self {
41        Self::Bool(b)
42    }
43}
44
45impl From<u8> for PropValue {
46    fn from(u: u8) -> Self {
47        Self::U8(u)
48    }
49}
50
51impl From<u16> for PropValue {
52    fn from(u: u16) -> Self {
53        Self::U16(u)
54    }
55}
56
57impl From<u32> for PropValue {
58    fn from(u: u32) -> Self {
59        Self::U32(u)
60    }
61}
62
63impl From<u64> for PropValue {
64    fn from(u: u64) -> Self {
65        Self::U64(u)
66    }
67}
68
69impl From<u128> for PropValue {
70    fn from(u: u128) -> Self {
71        Self::U128(u)
72    }
73}
74
75impl From<usize> for PropValue {
76    fn from(u: usize) -> Self {
77        Self::Usize(u)
78    }
79}
80
81impl From<i8> for PropValue {
82    fn from(i: i8) -> Self {
83        Self::I8(i)
84    }
85}
86
87impl From<i16> for PropValue {
88    fn from(i: i16) -> Self {
89        Self::I16(i)
90    }
91}
92
93impl From<i32> for PropValue {
94    fn from(i: i32) -> Self {
95        Self::I32(i)
96    }
97}
98
99impl From<i64> for PropValue {
100    fn from(i: i64) -> Self {
101        Self::I64(i)
102    }
103}
104
105impl From<i128> for PropValue {
106    fn from(i: i128) -> Self {
107        Self::I128(i)
108    }
109}
110
111impl From<isize> for PropValue {
112    fn from(i: isize) -> Self {
113        Self::Isize(i)
114    }
115}
116
117impl From<f32> for PropValue {
118    fn from(f: f32) -> Self {
119        Self::F32(f)
120    }
121}
122
123impl From<f64> for PropValue {
124    fn from(f: f64) -> Self {
125        Self::F64(f)
126    }
127}