-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
env.rs
189 lines (159 loc) 路 5.16 KB
/
env.rs
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
//! Unix-alike process environment parser.
use std::ffi::{OsStr, OsString};
use std::os::unix::ffi::OsStringExt;
use std::slice;
use std::vec;
/// Container for environment variables.
#[derive(Debug)]
pub struct Environment(Vec<(OsString, OsString)>);
impl Environment {
/// Parse environment variables from the `bytes` blob, separated by `\0`.
///
/// Reference: https://github.com/rust-lang/rust/blob/04e7f96dd89b1f0ad615dff1c85d11d4c4c64cb4/src/libstd/sys/unix/os.rs#L494-L510
pub fn from_bytes(bytes: &[u8]) -> Self {
let capacity = memchr::memchr_iter(b'\0', bytes).count();
let mut result = Vec::with_capacity(capacity);
let mut position = 0;
while let Some(offset) = memchr::memchr(b'\0', &bytes[position..]) {
// Null byte at the start or the double null byte means that we are also done parsing
if offset == 0 {
break;
}
let kv = &bytes[position..position + offset];
// Skipping the trailing `\0` also
position += offset + 1;
match memchr::memchr(b'=', &kv[1..]) {
Some(mut delimiter) => {
// Skipping the `=` part
delimiter += 1;
let key = OsString::from_vec(kv[..delimiter].to_vec());
let value = OsString::from_vec(kv[delimiter + 1..].to_vec());
result.push((key, value));
}
None => continue,
}
}
Self(result)
}
pub fn iter(&self) -> EnvironmentIter {
let kv = self.0.iter();
EnvironmentIter(kv)
}
}
impl IntoIterator for Environment {
type Item = (OsString, OsString);
type IntoIter = IntoEnvironmentIter;
fn into_iter(self) -> Self::IntoIter {
let kv = self.0.into_iter();
IntoEnvironmentIter(kv)
}
}
impl<'e> IntoIterator for &'e Environment {
type Item = (&'e OsStr, &'e OsStr);
type IntoIter = EnvironmentIter<'e>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
#[derive(Debug)]
pub struct IntoEnvironmentIter(vec::IntoIter<(OsString, OsString)>);
impl Iterator for IntoEnvironmentIter {
type Item = (OsString, OsString);
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
#[derive(Debug)]
pub struct EnvironmentIter<'e>(slice::Iter<'e, (OsString, OsString)>);
impl<'e> Iterator for EnvironmentIter<'e> {
type Item = (&'e OsStr, &'e OsStr);
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|(k, v)| (k.as_os_str(), v.as_os_str()))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
#[cfg(test)]
mod tests {
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
use super::Environment;
static SUITE: &[u8] = b"COLORFGBG=15;0\0COLORTERM=truecolor\0DISPLAY=:0\0HOME=/home/user\0LANG=en_US.UTF-8\0MAIL=/var/spool/mail/user\0PROFILEHOME=\0PATH=/home/user/.cargo/bin:/usr/local/bin:/usr/bin\0";
#[test]
fn test_iter() {
let mut env = Environment::from_bytes(&SUITE).into_iter();
assert_eq!(
Some((
OsStr::from_bytes(b"COLORFGBG").into(),
OsStr::from_bytes(b"15;0").into()
)),
env.next(),
);
assert_eq!(
Some((
OsStr::from_bytes(b"COLORTERM").into(),
OsStr::from_bytes(b"truecolor").into()
)),
env.next(),
);
assert_eq!(
Some((
OsStr::from_bytes(b"DISPLAY").into(),
OsStr::from_bytes(b":0").into()
)),
env.next(),
);
assert_eq!(
Some((
OsStr::from_bytes(b"HOME").into(),
OsStr::from_bytes(b"/home/user").into()
)),
env.next(),
);
assert_eq!(
Some((
OsStr::from_bytes(b"LANG").into(),
OsStr::from_bytes(b"en_US.UTF-8").into()
)),
env.next(),
);
assert_eq!(
Some((
OsStr::from_bytes(b"MAIL").into(),
OsStr::from_bytes(b"/var/spool/mail/user").into()
)),
env.next(),
);
assert_eq!(
Some((
OsStr::from_bytes(b"PROFILEHOME").into(),
OsStr::from_bytes(b"").into()
)),
env.next(),
);
assert_eq!(
Some((
OsStr::from_bytes(b"PATH").into(),
OsStr::from_bytes(b"/home/user/.cargo/bin:/usr/local/bin:/usr/bin").into()
)),
env.next(),
);
assert_eq!(None, env.next());
}
#[test]
fn test_empty() {
let mut env = Environment::from_bytes(b"").into_iter();
assert_eq!(None, env.next());
}
#[test]
fn test_nulls() {
let mut env =
Environment::from_bytes(b"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")
.into_iter();
assert_eq!(None, env.next());
}
}