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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
use std::borrow::Cow;
use smallvec::SmallVec;
use crate::{EnvPath, Raw};
impl FromIterator<String> for EnvPath<'_> {
/// This is similar to `new()`.
/// But the difference is that `new()` automatically converts the raw to path,
/// whereas `from_iter()` needs to be done manually.
fn from_iter<I: IntoIterator<Item = String>>(iter: I) -> Self {
Self {
raw: Raw::Owned(iter.into_iter().collect()),
path: None,
}
}
}
impl<'r> FromIterator<Cow<'r, str>> for EnvPath<'r> {
fn from_iter<I: IntoIterator<Item = Cow<'r, str>>>(iter: I) -> Self {
Self {
raw: Raw::Cow(iter.into_iter().collect()),
path: None,
}
}
}
impl<'r> FromIterator<&'r str> for EnvPath<'r> {
fn from_iter<I: IntoIterator<Item = &'r str>>(iter: I) -> Self {
Self {
raw: Self::create_ref_raw(iter),
path: None,
}
}
}
impl<'r, const N: usize> From<&[&'r str; N]> for EnvPath<'r> {
/// # Examples
///
/// ```
/// use envpath::EnvPath;
///
/// const ENV_PATH_RAW: [&str; 2] = ["$env: home", ".local"];
/// let v = EnvPath::from(&ENV_PATH_RAW).de();
/// dbg!(v.get_raw());
/// dbg!(v.display());
/// ```
fn from(raw: &[&'r str; N]) -> Self {
Self {
raw: Raw::Ref(SmallVec::from_slice(raw)),
path: None,
}
}
}
impl<'r, const N: usize> From<[&'r str; N]> for EnvPath<'r> {
/// # Examples
///
/// ```
/// use envpath::EnvPath;
///
/// let arr = ["$env:home", "dev"];
/// let path: EnvPath = arr.into();
/// dbg!(path.de().display());
/// ```
fn from(raw: [&'r str; N]) -> Self {
Self {
raw: Raw::Ref(raw.to_vec().into()),
path: None,
}
}
}
impl<'r> From<Vec<&'r str>> for EnvPath<'r> {
/// This is similar to `new()` when you use `Vec<S>` (S: `Into<String>`) as an
/// argument to `from()`. But the difference is that `new()` automatically
/// converts the raw to path, whereas `from()` or `into()` needs to be done
/// manually.
///
/// # Examples
///
/// ```
/// use envpath::EnvPath;
/// let v: EnvPath = vec!["$env:home"].into();
/// dbg!(v.get_raw());
/// dbg!(v.de().display());
/// ```
fn from(raw: Vec<&'r str>) -> Self {
Self {
raw: Raw::Ref(raw.into()),
path: None,
}
}
}
impl<'r, T: AsRef<str>> From<&[T]> for EnvPath<'r> {
/// The elements of an array slice can be of a type that implements
/// `AsRef<str>`.
fn from(raw: &[T]) -> Self {
Self {
raw: Raw::Owned(
raw
.iter()
.map(|s| s.as_ref().to_owned())
.collect(),
),
path: None,
}
}
}
impl<'r> From<&Vec<&'r str>> for EnvPath<'r> {
/// # Examples
///
/// ```
/// use envpath::EnvPath;
///
/// let path = EnvPath::from(&vec!["$env:home"]);
/// dbg!(path.get_raw());
/// ```
fn from(raw: &Vec<&'r str>) -> Self {
Self {
raw: Raw::Ref(raw.to_vec().into()),
path: None,
}
}
}
impl<'r> EnvPath<'r> {
/// Create a new instance of `EnvPath` from an iterator over borrowed strings.
///
/// The function takes an iterator over borrowed strings and creates an
/// instance of `EnvPath`. The raw value is automatically converted to
/// represent the path structure.
///
/// If you only need to serialize the path to a configuration and do not
/// require deserialization support, consider using the `from()` method
/// instead of this constructor.
///
/// # Examples
///
/// ```
/// use envpath::EnvPath;
///
/// let path = EnvPath::new([
/// "$env: home",
/// ".local",
/// "share",
/// "$const: pkg",
/// "$const: ver"
/// ]);
///
/// dbg!(path.display(), path.exists());
/// ```
pub fn new<V>(iter: V) -> Self
where
V: IntoIterator<Item = &'r str>,
{
Self {
raw: Self::create_ref_raw(iter),
path: None,
}
.de()
}
/// Create a new instance of `EnvPath` from an iterator over owned strings.
///
/// Note: `new_owned()` will convert `&str` to `String`, which may result in
/// additional heap memory allocation.
///
/// To avoid additional heap memory allocation, you can use `new()` instead of
/// `new_owned()`.
///
/// The function takes an iterator over owned strings and creates an instance
/// of `EnvPath`. The raw value is automatically converted to represent the
/// path structure.
///
/// # Examples
///
/// ```
/// use envpath::EnvPath;
///
/// let arr = [
/// "$env: home",
/// ".local",
/// "share",
/// "$const: pkg",
/// "$const: ver",
/// ];
///
/// let path = EnvPath::new_owned(arr);
///
/// dbg!(path.display(), path.exists());
/// ```
pub fn new_owned<V, S>(iter: V) -> Self
where
V: IntoIterator<Item = S>,
S: Into<String>,
{
Self {
raw: Raw::Owned(
iter
.into_iter()
.map(|s| s.into())
.collect(),
),
path: None,
}
.de()
}
/// Create a new instance of `EnvPath` from an iterator over borrowed Cow
/// strings.
///
/// The function takes an iterator over borrowed Cow strings and creates an
/// instance of `EnvPath`. The raw value is automatically converted to
/// represent the path structure.
///
/// # Examples
///
/// ```
/// use std::borrow::Cow;
/// use envpath::EnvPath;
///
/// let arr = [
/// "$env: home",
/// ".local",
/// "share",
/// "$const: pkg",
/// "$const: ver",
/// ];
///
/// let path = EnvPath::new_cow(arr.map(Cow::Borrowed));
/// dbg!(path.display(), path.exists());
/// ```
pub fn new_cow<V>(iter: V) -> Self
where
V: IntoIterator<Item = Cow<'r, str>>,
{
Self {
raw: Raw::Cow(iter.into_iter().collect()),
path: None,
}
.de()
}
/// Create a new instance of `Raw` from an iterator over borrowed strings.
///
/// which is used internally by the other constructor methods to create
/// `EnvPath` instances.
pub(crate) fn create_ref_raw<V>(iter: V) -> Raw<'r>
where
V: IntoIterator<Item = &'r str>,
{
Raw::Ref(iter.into_iter().collect())
}
}
#[cfg(test)]
mod tests {
use crate::EnvPath;
#[test]
fn from_iter_ref() {
let arr = vec!["$dir: data", "test"];
let path = EnvPath::from(&arr);
dbg!(arr);
dbg!(path.de());
}
#[test]
fn new_env_path() {
let arr = ["$dir: cfg", "test2"].map(|x| x.to_string());
let path = EnvPath::new_owned(&arr);
dbg!(&path, &arr);
let arr = [
"$env: home",
".local",
"share",
"$const: pkg",
"$const: ver",
];
let path = EnvPath::new_cow(arr.map(std::borrow::Cow::Borrowed));
dbg!(path.display(), path.exists());
}
}