support_kit/
environment.rs1use clap::ValueEnum;
2use serde::{Deserialize, Serialize};
3use strum::{AsRefStr, Display, EnumIs, VariantArray};
4
5#[derive(
6 Clone,
7 Copy,
8 Debug,
9 Default,
10 Deserialize,
11 Display,
12 EnumIs,
13 VariantArray,
14 AsRefStr,
15 ValueEnum,
16 Serialize,
17 PartialEq,
18)]
19#[serde(rename_all = "kebab-case")]
20#[strum(serialize_all = "kebab-case")]
21pub enum Environment {
22 Test,
23 #[default]
24 Development,
25 Production,
26}
27
28impl Environment {
29 pub fn all() -> Vec<Environment> {
30 Environment::VARIANTS.to_vec()
31 }
32}
33
34impl TryFrom<String> for Environment {
35 type Error = String;
36
37 fn try_from(value: String) -> Result<Self, Self::Error> {
38 match value.as_str() {
39 "test" => Ok(Environment::Test),
40 "development" => Ok(Environment::Development),
41 "production" => Ok(Environment::Production),
42 _ => Err(value),
43 }
44 }
45}
46
47#[test]
48fn all() {
49 assert_eq!(
50 Environment::all(),
51 vec![
52 Environment::Test,
53 Environment::Development,
54 Environment::Production
55 ]
56 );
57}
58
59#[test]
60fn from_string() {
61 assert_eq!(
62 Environment::try_from("test".to_owned()),
63 Ok(Environment::Test)
64 );
65 assert_eq!(
66 Environment::try_from("development".to_owned()),
67 Ok(Environment::Development)
68 );
69
70 assert_eq!(
71 Environment::try_from("production".to_owned()),
72 Ok(Environment::Production)
73 );
74}