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
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
#[serde(untagged)]
pub enum EnvironmentVariable {
  String(String),
  Number(f64),
  Boolean(bool),
}

impl ToString for EnvironmentVariable {
  fn to_string(&self) -> String {
    match self {
      EnvironmentVariable::String(s) => s.to_string(),
      EnvironmentVariable::Number(n) => n.to_string(),
      EnvironmentVariable::Boolean(b) => b.to_string(),
    }
  }
}

impl From<String> for EnvironmentVariable {
  fn from(s: String) -> Self {
    EnvironmentVariable::String(s)
  }
}

impl From<&str> for EnvironmentVariable {
  fn from(s: &str) -> Self {
    EnvironmentVariable::String(s.to_string())
  }
}

impl From<f64> for EnvironmentVariable {
  fn from(n: f64) -> Self {
    EnvironmentVariable::Number(n)
  }
}

impl From<bool> for EnvironmentVariable {
  fn from(b: bool) -> Self {
    EnvironmentVariable::Boolean(b)
  }
}

pub type EnvironmentVariables = HashMap<String, EnvironmentVariable>;

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn test_to_string() {
    assert_eq!(
      EnvironmentVariable::String("test".to_string()).to_string(),
      "test".to_string()
    );
    assert_eq!(
      EnvironmentVariable::Number(1.0).to_string(),
      "1".to_string()
    );
    assert_eq!(
      EnvironmentVariable::Boolean(true).to_string(),
      "true".to_string()
    );
  }

  #[test]
  fn test_from() {
    assert_eq!(
      EnvironmentVariable::from("test".to_string()),
      EnvironmentVariable::String("test".to_string())
    );
    assert_eq!(
      EnvironmentVariable::from("test"),
      EnvironmentVariable::String("test".to_string())
    );
    assert_eq!(
      EnvironmentVariable::from(1.0),
      EnvironmentVariable::Number(1.0)
    );
    assert_eq!(
      EnvironmentVariable::from(true),
      EnvironmentVariable::Boolean(true)
    );
  }
}