vespertide_core/schema/
str_or_bool.rs1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
5#[serde(rename_all = "snake_case", untagged)]
6pub enum StrOrBoolOrArray {
7 Str(String),
8 Array(Vec<String>),
9 Bool(bool),
10}
11
12#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
15#[serde(untagged)]
16pub enum StringOrBool {
17 Bool(bool),
18 String(String),
19}
20
21impl StringOrBool {
22 pub fn to_sql(&self) -> String {
24 match self {
25 StringOrBool::Bool(b) => b.to_string(),
26 StringOrBool::String(s) => s.clone(),
27 }
28 }
29}
30
31impl From<bool> for StringOrBool {
32 fn from(b: bool) -> Self {
33 StringOrBool::Bool(b)
34 }
35}
36
37impl From<String> for StringOrBool {
38 fn from(s: String) -> Self {
39 StringOrBool::String(s)
40 }
41}
42
43impl From<&str> for StringOrBool {
44 fn from(s: &str) -> Self {
45 StringOrBool::String(s.to_string())
46 }
47}
48
49#[cfg(test)]
50mod tests {
51 use super::*;
52
53 #[test]
54 fn test_string_or_bool_to_sql_bool() {
55 let val = StringOrBool::Bool(true);
56 assert_eq!(val.to_sql(), "true");
57
58 let val = StringOrBool::Bool(false);
59 assert_eq!(val.to_sql(), "false");
60 }
61
62 #[test]
63 fn test_string_or_bool_to_sql_string() {
64 let val = StringOrBool::String("hello".into());
65 assert_eq!(val.to_sql(), "hello");
66 }
67
68 #[test]
69 fn test_string_or_bool_from_bool() {
70 let val: StringOrBool = true.into();
71 assert_eq!(val, StringOrBool::Bool(true));
72
73 let val: StringOrBool = false.into();
74 assert_eq!(val, StringOrBool::Bool(false));
75 }
76
77 #[test]
78 fn test_string_or_bool_from_string() {
79 let val: StringOrBool = String::from("test").into();
80 assert_eq!(val, StringOrBool::String("test".into()));
81 }
82
83 #[test]
84 fn test_string_or_bool_from_str() {
85 let val: StringOrBool = "test".into();
86 assert_eq!(val, StringOrBool::String("test".into()));
87 }
88}