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
use std::{borrow::Cow, path::PathBuf};
use smallvec::SmallVec;
use crate::{EnvPath, parser::parse};
#[derive(Debug, Clone, Hash, Eq, PartialEq, PartialOrd, Ord)]
pub enum EnvPathRaw<'r> {
Ref(SmallVec<&'r str, 1>),
Cow(SmallVec<Cow<'r, str>, 1>),
Owned(SmallVec<String, 1>),
}
impl<'r> EnvPathRaw<'r> {
pub fn is_empty(&self) -> bool {
use EnvPathRaw::*;
match self {
Ref(x) => x.is_empty(),
Cow(x) => x.is_empty(),
Owned(x) => x.is_empty(),
}
}
pub fn parse(&self) -> Option<PathBuf> {
use EnvPathRaw::*;
match self {
Ref(x) => parse(x),
Cow(x) => parse(x),
Owned(x) => parse(x),
}
}
}
impl<'r> Default for EnvPathRaw<'r> {
fn default() -> Self {
EnvPathRaw::Ref(SmallVec::new())
}
}
impl<'r> EnvPath<'r> {
/// Get a reference to the raw sequence of strings.
///
/// # Examples
///
/// ```
/// use envpath::EnvPath;
///
/// let path = EnvPath::from(["$env: home ?? userprofile", "3D Print"]);
/// dbg!(path.get_raw());
/// ```
// pub fn get_raw(&self) -> &[&str] {
// self.raw.as_ref()
// }
pub fn get_raw(&self) -> &EnvPathRaw {
&self.raw
}
/// `get_raw_mut` is a public method of the `EnvPath` struct that returns a
/// mutable reference to the raw sequence of strings.
///
/// This method can be used to modify the raw sequence and update the
/// `EnvPath` object accordingly. It takes no arguments and returns a mutable
/// reference to an `EnvPathRaw` object.
pub fn get_raw_mut(&mut self) -> &mut EnvPathRaw<'r> {
&mut self.raw
}
/// Set the raw sequence of strings.
///
/// # Examples
///
/// ```
/// use envpath::EnvPath;
///
/// let mut path = EnvPath::from(["$dir: cfg", "config.ron"]);
/// dbg!(path.get_raw());
///
/// path.set_raw(vec!["$project( com. x. y ): cfg", "config.toml"]);
/// dbg!(path.get_raw());
///
/// path.set_raw([" $dir: bin ?? first-path "]);
/// dbg!(path.de().display());
/// ```
pub fn set_raw<V: IntoIterator<Item = &'r str>>(&mut self, raw: V) {
self.raw = Self::create_ref_raw(raw);
}
/// Clear the raw sequence of strings.
///
/// # Examples
///
/// ```
/// use envpath::EnvPath;
///
/// let mut path =
/// EnvPath::from(["$env: xdg_data_home", "$const: pkg", "files"]);
///
/// path.clear_raw();
///
/// assert!(path.get_raw().is_empty());
/// ```
pub fn clear_raw(&mut self) {
self.raw = EnvPathRaw::Ref(SmallVec::new());
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn clear_raw_doc() {
let mut path = EnvPath::new(vec!["$env: xdg_data_home", "$const: pkg", "files"]);
path.clear_raw();
assert!(!path.exists());
}
}