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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
extern crate regex;
use std::fs::File;
use std::io::{BufReader, BufRead};
use std::env;
use std::result::Result;
use std::path::Path;
use regex::{Captures, Regex, Error};
#[derive(Debug, Clone)]
pub enum DotenvError {
Parsing {
line: String,
},
ParseFormatter,
Io,
ExecutableNotFound,
}
impl From<regex::Error> for DotenvError {
fn from(_: regex::Error) -> DotenvError {
DotenvError::ParseFormatter
}
}
impl From<std::io::Error> for DotenvError {
fn from(_: std::io::Error) -> DotenvError {
DotenvError::Io
}
}
type ParsedLine = Result<Option<(String, String)>, DotenvError>;
type ParsedLines = Result<Vec<(String, String)>, DotenvError>;
fn named_string(captures: &Captures, name: &str) -> Option<String> {
captures.name(name).and_then(|v| Some(v.to_string()))
}
fn parse_line(line: String) -> ParsedLine {
let line_regex = try!(Regex::new(concat!(r"^(\s*(",
r"#.*|",
r"\s*|",
r"(export\s+)?",
r"(?P<key>[A-Za-z_][A-Za-z0-9_]*)",
r"\s*=\s*",
r"(?P<value>.+?)",
r")\s*)[\r\n]*$")));
line_regex.captures(&line)
.map_or(Err(DotenvError::Parsing { line: line.clone() }),
|captures| {
let key = named_string(&captures, "key");
let value = named_string(&captures, "value");
if key.is_some() && value.is_some() {
Ok(Some((key.unwrap(), value.unwrap())))
} else {
Ok(None)
}
})
}
fn from_file(file: File) -> Result<(), DotenvError> {
let reader = BufReader::new(file);
for line in reader.lines() {
let line = try!(line);
let parsed = try!(parse_line(line));
match parsed {
Some((key, value)) => {
if env::var(&key).is_err() {
env::set_var(&key, value);
}
()
}
None => (),
}
}
Ok(())
}
fn try_parent(path: &Path, filename: &str) -> Result<(), DotenvError> {
match path.parent() {
Some(parent) => {
match from_path(&parent.join(filename)) {
Ok(file) => Ok(file),
Err(DotenvError::Io) => try_parent(parent, filename),
err => err
}
},
None => Err(DotenvError::Io)
}
}
pub fn from_path(path: &Path) -> Result<(), DotenvError> {
match File::open(path) {
Ok(file) => from_file(file),
Err(_) => Err(DotenvError::Io),
}
}
pub fn from_filename(filename: &str) -> Result<(), DotenvError> {
let path = match env::current_dir() {
Ok(path) => path,
Err(_) => return Err(DotenvError::Io)
};
match from_path(&path.join(filename)) {
Ok(file) => Ok(file),
Err(DotenvError::Io) => try_parent(&path, filename),
err => err
}
}
pub fn dotenv() -> Result<(), DotenvError> {
from_filename(&".env")
}
#[test]
fn test_parse_line_env() {
let input_iter = vec!["THIS_IS_KEY=hi this is value",
" many_spaces = wow a maze ",
"export SHELL_LOVER=1"]
.into_iter()
.map(|input| input.to_string());
let actual_iter = input_iter.map(|input| parse_line(input));
let expected_iter = vec![("THIS_IS_KEY", "hi this is value"),
("many_spaces", "wow a maze"),
("SHELL_LOVER", "1")]
.into_iter()
.map(|(key, value)| (key.to_string(), value.to_string()));
for (expected, actual) in expected_iter.zip(actual_iter) {
assert!(actual.is_ok());
assert!(actual.clone().ok().unwrap().is_some());
assert_eq!(expected, actual.ok().unwrap().unwrap());
}
}
#[test]
fn test_parse_line_comment() {
let input_iter = vec!["# foo=bar", " # "]
.into_iter()
.map(|input| input.to_string());
let actual_iter = input_iter.map(|input| parse_line(input));
for actual in actual_iter {
assert!(actual.is_ok());
assert!(actual.ok().unwrap().is_none());
}
}
#[test]
fn test_parse_line_invalid() {
let input_iter = vec![" invalid ", "very bacon = yes indeed", "key=", "=value"]
.into_iter()
.map(|input| input.to_string());
let actual_iter = input_iter.map(|input| parse_line(input));
for actual in actual_iter {
assert!(actual.is_err());
}
}