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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//! Helper functions and data types
//!
//! This module provides business-logic agnostic helpers
//! which can be used throughout the cenv codebase.
//!
//!

use std::fs;
use std::io::Write;

/// Configuration of the current domain
#[derive(PartialEq, Debug)]
pub struct Config {
    pub keyword: String,
}

impl Config {
    /// Returns Result with error on keyword being an empty string
    pub fn new(keyword: &str) -> Result<Config, &'static str> {
        let keyword = match keyword {
            "" => return Err("Keyword missing"),
            word => word,
        };

        Ok(Config {
            keyword: String::from(keyword),
        })
    }
    /// Accepts a list of arguments, usually an [Args][std::env::Args] struct
    /// sourced from the [std::env::args] function.
    pub fn new_from_args<T>(mut args: T) -> Result<Config, &'static str>
    where
        T: Iterator<Item = String>,
    {
        // ignore first arg (program name)
        args.next();

        let keyword = match args.next() {
            Some(word) => word,
            None => return Err("Keyword missing"),
        };

        Ok(Config { keyword })
    }
}

/// Details around the content to be parsed
#[derive(PartialEq, Debug)]
pub struct EnvContents {
    pub contents: String,
}

impl EnvContents {
    pub fn new(contents: String) -> EnvContents {
        EnvContents { contents }
    }
}

/// Reads .env file in execution scope
pub fn read_env_file() -> Result<EnvContents, &'static str> {
    let contents = match fs::read_to_string(".env") {
        Ok(w) => w,
        Err(_) => return Err("Unable to read .env file"),
    };
    Ok(EnvContents { contents })
}

/// Writes to the .env file in execution scope
pub fn write_env_file(env: &EnvContents) -> Result<(), String> {
    let mut file = match fs::File::create(".env") {
        Ok(f) => f,
        Err(e) => return Err(format!("Unable to write .env file - {}", e)),
    };

    match file.write_all(env.contents.as_bytes()) {
        Ok(_) => Ok(()),
        Err(e) => Err(format!("Unable to write .env file - {}", e)),
    }
}

#[cfg(test)]
mod config_tests {
    use super::*;
    #[test]
    fn err_on_missing_keyword() {
        let args = vec![String::from("")];
        let result = Config::new_from_args(args.into_iter());
        assert_eq!(result, Err("Keyword missing"))
    }
    #[test]
    fn returns_populated_config() {
        let args = vec![String::from(""), String::from("testing")];
        let result = Config::new_from_args(args.into_iter()).unwrap();
        assert_eq!(
            result,
            Config {
                keyword: String::from("testing")
            }
        )
    }
}

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

    #[test]
    fn includes_correct_data() {
        let e = EnvContents::new(String::from("testing"));

        assert_eq!(
            e,
            EnvContents {
                contents: String::from("testing")
            }
        )
    }
}

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

    fn setup() {
        let contents = String::from(
            "# ++ one ++
# TEST_A=1
# TEST_B=1

# ++ two ++
# TEST_A=2
# TEST_B=2

# ++ three ++
TEST_A=3
TEST_B=3
",
        );
        write_env_file(&EnvContents::new(contents)).unwrap();
    }

    #[test]
    fn does_not_error() {
        setup();
        let result = read_env_file();
        assert_ne!(result, Err("Unable to read .env file"));
    }

    #[test]
    fn returns_expected_content() {
        setup();
        let result = read_env_file();
        let contents = String::from(
            "# ++ one ++
# TEST_A=1
# TEST_B=1

# ++ two ++
# TEST_A=2
# TEST_B=2

# ++ three ++
TEST_A=3
TEST_B=3
",
        );
        assert_eq!(result, Ok(EnvContents { contents }));
    }
}

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

    #[test]
    fn does_not_error() {
        let contents = String::from(
            "# ++ one ++
# TEST_A=1
# TEST_B=1

# ++ two ++
# TEST_A=2
# TEST_B=2

# ++ three ++
TEST_A=3
TEST_B=3
",
        );
        let result = write_env_file(&EnvContents::new(contents));
        assert_eq!(result, Ok(()));
    }
}