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
use crate::scanner::dir::ConfigDir;
use crate::ConfigFile;
use std::path::PathBuf;
use strut_core::AppProfile;
/// Represents a filesystem entry that is relevant for config files.
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq)]
pub enum ConfigEntry {
/// A directory.
Directory(ConfigDir),
/// A config file.
File(ConfigFile),
}
impl ConfigEntry {
/// Creates a [directory](ConfigEntry::Directory)-type [`ConfigEntry`] from
/// the given path with no questions asked (no validation on the path).
///
/// This is intended for creating root entries.
pub fn dir(path: PathBuf) -> Self {
Self::Directory(ConfigDir::at(path))
}
/// Creates a [`ConfigEntry`] from the given [`PathBuf`], if the path points
/// to a workable config-related filesystem entry.
pub fn try_from(path: PathBuf) -> Option<Self> {
Self::try_from_with_profile(path, None)
}
/// Creates a [`ConfigEntry`] from the given [`PathBuf`], if the path points
/// to a workable config-related filesystem entry, optionally applying the
/// given known profile name to the config files.
pub fn try_from_with_profile(path: PathBuf, known_profile: Option<&str>) -> Option<Self> {
// A directory is easy
if path.is_dir() {
let config_dir = ConfigDir::make_with_profile(path, known_profile);
return Some(ConfigEntry::Directory(config_dir));
}
// A file is easy too
if let Some(config_file) = ConfigFile::try_make_with_profile(path, known_profile) {
return Some(ConfigEntry::File(config_file));
}
None
}
}
impl ConfigEntry {
/// Reports whether this [`ConfigEntry`] is a
/// [directory](ConfigEntry::Directory).
pub fn is_directory(&self) -> bool {
matches!(self, ConfigEntry::Directory(_))
}
/// Reports whether this [`ConfigEntry`] is a config
/// [file](ConfigEntry::File).
pub fn is_file(&self) -> bool {
matches!(self, ConfigEntry::File(_))
}
/// Returns a reference to the internally held [`PathBuf`].
pub fn path(&self) -> &PathBuf {
match *self {
ConfigEntry::Directory(ref config_dir) => config_dir.path(),
ConfigEntry::File(ref config_file) => config_file.path(),
}
}
/// Returns a reference to the internally held file/directory name.
pub fn name(&self) -> Option<&str> {
self.path().file_name().and_then(std::ffi::OsStr::to_str)
}
/// Reports whether this [`ConfigEntry`] [applies](ConfigEntry::applies_to)
/// to the [active](AppProfile::active) [`AppProfile`].
pub fn applies_to_active_profile(&self) -> bool {
self.applies_to(AppProfile::active())
}
/// Reports whether this [`ConfigEntry`] applies to the given [`AppProfile`].
///
/// Delegates to the underlying logic for both the
/// [directory](ConfigDir::applies_to) and the
/// [file](ConfigFile::applies_to) variants.
pub fn applies_to(&self, profile: impl AsRef<AppProfile>) -> bool {
match *self {
ConfigEntry::Directory(ref config_dir) => config_dir.applies_to(profile),
ConfigEntry::File(ref config_file) => config_file.applies_to(profile),
}
}
/// Consumes this [`ConfigEntry`] and yields only [`ConfigFile`]s.
pub fn to_config_file(self) -> Option<ConfigFile> {
match self {
ConfigEntry::Directory(_) => None,
ConfigEntry::File(config_file) => Some(config_file),
}
}
}
impl ConfigEntry {
/// Iterates over [`ConfigEntry`]s that are the immediate children of this
/// [`ConfigEntry`], if this entry is a [directory](ConfigEntry::Directory).
/// If this entry is a file, yields a [`Once`](std::iter::Once) iterator
/// over that file.
///
/// If this entry is a directory that is [associated](ConfigDir::Specific)
/// with a profile — the profile is carried over into all nested entries.
///
/// All failure conditions (e.g., non-existing paths, un-readable files) are
/// silently ignored.
///
/// This is intended for convenient flat-mapping to move deeper into nested
/// directories, if any.
pub fn cd(self) -> ConfigEntryIter {
match self {
ConfigEntry::Directory(ref config_dir) => {
let config_entries = config_dir.expand(config_dir.profile());
ConfigEntryIter::Directory(config_entries.into_iter())
}
ConfigEntry::File(_) => ConfigEntryIter::File(std::iter::once(self)),
}
}
/// Same as [`cd`](ConfigEntry::cd), but if this entry is a directory, then
/// instead of carrying over the profile that this directory may be
/// associated with, captures the profile from this directory’s name.
pub fn cd_capturing_profile(self) -> ConfigEntryIter {
match self {
ConfigEntry::Directory(ref config_dir) => {
let config_entries = config_dir.expand(config_dir.name());
ConfigEntryIter::Directory(config_entries.into_iter())
}
ConfigEntry::File(_) => ConfigEntryIter::File(std::iter::once(self)),
}
}
/// Same as [`cd`](ConfigEntry::cd), but if this entry is a directory, then
/// instead of carrying over the profile that this directory may be
/// associated with, explicitly “forgets” any associated profile: the
/// children entries will not be associated with any profile.
pub fn cd_forgetting_profile(self) -> ConfigEntryIter {
match self {
ConfigEntry::Directory(ref config_dir) => {
let config_entries = config_dir.expand(None);
ConfigEntryIter::Directory(config_entries.into_iter())
}
ConfigEntry::File(_) => ConfigEntryIter::File(std::iter::once(self)),
}
}
}
impl From<ConfigEntry> for PathBuf {
fn from(file: ConfigEntry) -> Self {
match file {
ConfigEntry::Directory(config_dir) => PathBuf::from(config_dir),
ConfigEntry::File(config_file) => PathBuf::from(config_file),
}
}
}
/// Represents an iterator on the expanded [`ConfigEntry`]:
pub enum ConfigEntryIter {
/// For a [directory](ConfigEntry::Directory), iterates over nested
/// [`ConfigEntry`]s.
Directory(std::vec::IntoIter<ConfigEntry>),
/// For a [file](ConfigEntry::File), iterates over that single file.
File(std::iter::Once<ConfigEntry>),
}
impl Iterator for ConfigEntryIter {
type Item = ConfigEntry;
fn next(&mut self) -> Option<Self::Item> {
match *self {
Self::Directory(ref mut iter) => iter.next(),
Self::File(ref mut iter) => iter.next(),
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
match *self {
Self::Directory(ref iter) => iter.size_hint(),
Self::File(ref iter) => iter.size_hint(),
}
}
}