1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3
4#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
5#[serde(untagged)]
6pub enum EnvironmentVariable {
7 String(String),
8 Number(f64),
9 Boolean(bool),
10}
11
12impl std::fmt::Display for EnvironmentVariable {
13 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14 match self {
15 EnvironmentVariable::String(s) => write!(f, "{}", s),
16 EnvironmentVariable::Number(n) => write!(f, "{}", n),
17 EnvironmentVariable::Boolean(b) => write!(f, "{}", b),
18 }
19 }
20}
21
22impl From<String> for EnvironmentVariable {
23 fn from(s: String) -> Self {
24 EnvironmentVariable::String(s)
25 }
26}
27
28impl From<&str> for EnvironmentVariable {
29 fn from(s: &str) -> Self {
30 EnvironmentVariable::String(s.to_string())
31 }
32}
33
34impl From<f64> for EnvironmentVariable {
35 fn from(n: f64) -> Self {
36 EnvironmentVariable::Number(n)
37 }
38}
39
40impl From<bool> for EnvironmentVariable {
41 fn from(b: bool) -> Self {
42 EnvironmentVariable::Boolean(b)
43 }
44}
45
46pub type EnvironmentVariables = HashMap<String, EnvironmentVariable>;
47
48#[cfg(test)]
49mod tests {
50 use super::*;
51
52 #[test]
53 fn test_to_string() {
54 assert_eq!(
55 EnvironmentVariable::String("test".to_string()).to_string(),
56 "test".to_string()
57 );
58 assert_eq!(
59 EnvironmentVariable::Number(1.0).to_string(),
60 "1".to_string()
61 );
62 assert_eq!(
63 EnvironmentVariable::Boolean(true).to_string(),
64 "true".to_string()
65 );
66 }
67
68 #[test]
69 fn test_from() {
70 assert_eq!(
71 EnvironmentVariable::from("test".to_string()),
72 EnvironmentVariable::String("test".to_string())
73 );
74 assert_eq!(
75 EnvironmentVariable::from("test"),
76 EnvironmentVariable::String("test".to_string())
77 );
78 assert_eq!(
79 EnvironmentVariable::from(1.0),
80 EnvironmentVariable::Number(1.0)
81 );
82 assert_eq!(
83 EnvironmentVariable::from(true),
84 EnvironmentVariable::Boolean(true)
85 );
86 }
87}