1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
pub type RuleProperties = HashMap<String, RulePropertyValue>;
#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum RulePropertyValue {
Boolean(bool),
String(String),
Usize(usize),
Float(f64),
StringList(Vec<String>),
None,
}
impl From<bool> for RulePropertyValue {
fn from(value: bool) -> Self {
Self::Boolean(value)
}
}
impl From<&str> for RulePropertyValue {
fn from(value: &str) -> Self {
Self::String(value.to_owned())
}
}
impl From<String> for RulePropertyValue {
fn from(value: String) -> Self {
Self::String(value)
}
}
impl From<usize> for RulePropertyValue {
fn from(value: usize) -> Self {
Self::Usize(value)
}
}
impl From<f64> for RulePropertyValue {
fn from(value: f64) -> Self {
Self::Float(value)
}
}
impl<T: Into<RulePropertyValue>> From<Option<T>> for RulePropertyValue {
fn from(value: Option<T>) -> Self {
match value {
Some(value) => value.into(),
None => Self::None,
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn from_true() {
assert_eq!(
RulePropertyValue::from(true),
RulePropertyValue::Boolean(true)
);
}
#[test]
fn from_false() {
assert_eq!(
RulePropertyValue::from(false),
RulePropertyValue::Boolean(false)
);
}
#[test]
fn from_string() {
assert_eq!(
RulePropertyValue::from(String::from("hello")),
RulePropertyValue::String(String::from("hello"))
);
}
#[test]
fn from_str() {
assert_eq!(
RulePropertyValue::from("hello"),
RulePropertyValue::String(String::from("hello"))
);
}
#[test]
fn from_usize() {
assert_eq!(RulePropertyValue::from(6), RulePropertyValue::Usize(6));
}
#[test]
fn from_float() {
assert_eq!(RulePropertyValue::from(1.0), RulePropertyValue::Float(1.0));
}
#[test]
fn from_boolean_option_some() {
let bool = Some(true);
assert_eq!(
RulePropertyValue::from(bool),
RulePropertyValue::Boolean(true)
);
}
#[test]
fn from_boolean_option_none() {
let bool: Option<bool> = None;
assert_eq!(RulePropertyValue::from(bool), RulePropertyValue::None);
}
}