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
#[cfg(test)]
#[path = "./expansion_test.rs"]
mod expansion_test;
use crate::types::ExpansionType;
use std::env;
pub(crate) fn get_os_expansion_type() -> ExpansionType {
if cfg!(windows) {
ExpansionType::Windows
} else {
ExpansionType::Unix
}
}
fn should_break_key(value: char, reading_key: bool, prefix_only: bool) -> bool {
if (reading_key && value == ' ')
|| value == '\n'
|| value == '\t'
|| value == '\r'
|| (reading_key && value == '=')
{
true
} else if prefix_only {
value == '/' || value == '\\' || value == ':'
} else {
false
}
}
pub(crate) fn expand_by_prefix(value: &str, prefix: char, default_to_empty: bool) -> String {
let mut value_string = String::new();
let mut found_prefix = false;
let mut env_key = String::new();
let mut counter = 0;
for next_char in value.chars() {
counter = counter + 1;
let last_char = counter == value.len();
if !found_prefix {
if next_char == prefix {
found_prefix = true;
env_key.clear();
} else {
value_string.push(next_char);
}
} else if last_char || should_break_key(next_char, true, true) {
if last_char {
env_key.push(next_char);
}
match env::var(&env_key) {
Ok(env_value) => value_string.push_str(&env_value),
_ => {
if !default_to_empty {
value_string.push(prefix);
value_string.push_str(&env_key);
}
}
};
if !last_char {
value_string.push(next_char);
}
env_key.clear();
found_prefix = false;
} else {
env_key.push(next_char);
}
}
value_string
}
pub(crate) fn expand_by_wrapper(
value: &str,
prefix: &str,
suffix: char,
default_to_empty: bool,
search_default: bool,
) -> String {
let mut value_string = String::new();
let prefix_length = prefix.len();
let mut prefix_index = 0;
let prefix_chars: Vec<char> = prefix.chars().collect();
let mut found_prefix = false;
let mut env_key = String::new();
let mut reading_default = false;
let mut default_value = String::new();
for next_char in value.chars() {
if !found_prefix {
if next_char == prefix_chars[prefix_index] {
prefix_index = prefix_index + 1;
if prefix_index == prefix_length {
found_prefix = true;
prefix_index = 0;
env_key.clear();
}
} else {
if prefix_index > 0 {
value_string.push_str(&prefix[..prefix_index]);
prefix_index = 0;
}
value_string.push(next_char);
}
} else if search_default && !reading_default && next_char == ':' {
reading_default = true;
} else if next_char == suffix {
match env::var(&env_key) {
Ok(env_value) => value_string.push_str(&env_value),
_ => {
if reading_default {
value_string.push_str(&default_value);
} else if !default_to_empty {
value_string.push_str(prefix);
value_string.push_str(&env_key);
value_string.push(suffix);
}
}
};
env_key.clear();
found_prefix = false;
if search_default {
reading_default = false;
default_value.clear();
}
} else if should_break_key(next_char, !reading_default, false) {
value_string.push_str(&prefix);
value_string.push_str(&env_key);
if reading_default {
value_string.push(':');
value_string.push_str(&default_value);
}
value_string.push(next_char);
env_key.clear();
found_prefix = false;
if search_default {
reading_default = false;
default_value.clear();
}
} else if reading_default {
default_value.push(next_char);
} else {
env_key.push(next_char);
}
}
if env_key.len() > 0 {
value_string.push_str(&prefix);
value_string.push_str(&env_key);
if reading_default {
value_string.push(':');
value_string.push_str(&default_value);
}
}
value_string
}