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
pub use env_loader_convert::convert;
mod error;
mod inner_value;
#[cfg(test)]
mod tests;
mod validators;
use error::{ConfigLoaderError, ConstraintValidationError};
use inner_value::{InnerValue, Value};
use std::collections::HashMap;
use std::env;
use validators::Constraint;

pub struct ConfigLoader(HashMap<String, InnerValue>);

fn parse_constraint(constraint_mask: &str) -> Option<Vec<Constraint>> {
    if constraint_mask == ",,," || constraint_mask.is_empty() {
        return None;
    }
    let splitted: Vec<&str> = constraint_mask.split(',').collect();
    let mut res = vec![];
    for (index, value) in splitted.iter().enumerate() {
        if splitted[index].is_empty() {
            continue;
        }
        match index {
            0 => res.push(Constraint::Max(value.parse().unwrap())),
            1 => res.push(Constraint::Min(value.parse().unwrap())),
            2 => res.push(Constraint::Optional),
            3 => res.push(Constraint::NotEmpty),
            _ => continue,
        }
    }
    Some(res)
}

fn check(
    val: &inner_value::InnerValue,
    constraints: &Option<Vec<Constraint>>,
) -> Result<bool, ConstraintValidationError> {
  
    match constraints {
        Some(constraints) => match val {
            InnerValue::Int(val) => validators::check_num(*val as i64, constraints),
            InnerValue::Long(val) => validators::check_num(*val, constraints),
            InnerValue::Str(val) => validators::check_str(val, constraints),
            _ => Ok(true),
        },
        None => Ok(true),
    }
}
impl ConfigLoader {
    pub fn new<T>(names: T) -> Result<Self, ConfigLoaderError>
    where
        T: IntoIterator<Item = (&'static str, Value, String)>,
    {
        let dotenv_reading_result = dotenv::dotenv();
        if dotenv_reading_result.is_err() {
            return Err(ConfigLoaderError::NoEnvFile);
        }

        let mut store = HashMap::new();
        for (name, typing, constraints) in names {
            let value = env::var(name);
            let constraints = parse_constraint(&constraints);
            let is_optional_value = constraints
                .as_ref()
                .is_some_and(|constraints| constraints.contains(&Constraint::Optional));
            if value.is_err() && !is_optional_value {
                return Err(ConfigLoaderError::ValueNotInEnv(format!(
                    "{} not in env file. Add it to file or mark as optional",
                    name
                )));
            }
            match value {
                Ok(value) => {
                    let res: InnerValue = (value, &typing).into();

                    match res {
                        InnerValue::None => return Err(ConfigLoaderError::WrongConvertion),
                        val => match check(&val, &constraints) {
                            Err(e) => {
                                return Err(ConfigLoaderError::ValueValidationFail(e));
                            }

                            Ok(_) => store.insert(String::from(name), val),
                        },
                    };
                }
                Err(_) => {
                    if !is_optional_value {
                        return Err(ConfigLoaderError::ValueNotInEnv(format!(
                            "{} not in env file. Add it to file or mark as optional",
                            name
                        )));
                    }
                }
            }
        }
        Ok(Self(store))
    }

    pub fn get<T>(&self, name: &'static str) -> Result<T, ConfigLoaderError>
    where
        Option<T>: From<InnerValue>,
    {
        let val = self.0.get(name);
        if val.is_none() {
            return Err(ConfigLoaderError::IsNotPartOfRuntime(name));
        }

        match (*val.unwrap()).clone().into() {
            Some(inner) => Ok(inner),
            None => Err(ConfigLoaderError::WrongTypeTryingToGet(name)),
        }
    }
}