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
use crate::common::types::DatabaseType;
use std::env::var;

#[derive(Clone, Debug)]
pub struct Dotenv {
    pub db_type: Option<DatabaseType>,
    pub db_user: Option<String>,
    pub db_host: Option<String>,
    pub db_port: Option<u16>,
    pub db_pass: Option<String>,
    pub db_name: Option<String>,
}

impl Dotenv {
    pub fn new() -> Dotenv {
        Dotenv {
            db_type: match var("DB_TYPE").ok() {
                None => None,
                Some(val) => {
                    if val == "mysql" {
                        Some(DatabaseType::Mysql)
                    } else {
                        Some(DatabaseType::Postgres)
                    }
                }
            },
            db_user: var("DB_USER").ok(),
            db_host: var("DB_HOST").ok(),
            db_port: var("DB_PORT")
                .ok()
                .map(|val| val.parse::<u16>().expect("DB_PORT is not a valid integer")),
            db_pass: var("DB_PASS").ok(),
            db_name: var("DB_NAME").ok(),
        }
    }
}