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
use config::{File, FileFormat, FileSourceFile};
use std::cmp::Ordering;
use std::path::PathBuf;
use strut_core::AppProfile;
/// Represents a single config file.
#[derive(Debug, Eq, PartialEq)]
pub enum ConfigFile {
/// A file like `app.toml`.
GenericToml(PathBuf),
/// A file like `app.yaml` / `app.yml`.
GenericYaml(PathBuf),
/// A file like `app.prod.toml`.
SpecificToml {
/// The path to the file.
path: PathBuf,
/// The associated profile name.
profile: String,
},
/// A file like `app.prod.yaml` / `app.prod.yml`.
SpecificYaml {
/// The path to the file.
path: PathBuf,
/// The associated profile name.
profile: String,
},
}
impl ConfigFile {
/// Creates a [`ConfigFile`] from the given [`PathBuf`], if the path points
/// to a workable config file.
pub fn try_at(path: PathBuf) -> Option<Self> {
Self::try_make_with_profile(path, None)
}
/// Creates a [`ConfigFile`] from the given [`PathBuf`], if the path points
/// to a workable config file, optionally applying the given known profile
/// name.
pub fn try_make_with_profile(path: PathBuf, known_profile: Option<&str>) -> Option<Self> {
// Read file name
let name = match path.file_name().and_then(std::ffi::OsStr::to_str) {
Some(name) => name,
None => return None,
};
// Split file name on `.`
let chunks = name.split('.').collect::<Vec<_>>();
// Match chunk pattern
match *chunks.as_slice() {
[_name, extension] => {
if is_toml_extension(extension) {
Self::toml_from(path, known_profile)
} else if is_yaml_extension(extension) {
Self::yaml_from(path, known_profile)
} else {
None
}
}
[_name, profile, extension] => {
// Do we know the profile already?
if let Some(known_profile) = known_profile {
// If we know the profile already, only take specific file of that profile
if !known_profile.eq_ignore_ascii_case(profile) {
return None;
}
}
// Only take supported extensions
if is_toml_extension(extension) {
let profile = profile.to_string();
Self::toml_from(path, Some(profile))
} else if is_yaml_extension(extension) {
let profile = profile.to_string();
Self::yaml_from(path, Some(profile))
} else {
None
}
}
_ => None,
}
}
fn toml_from(path: PathBuf, profile: Option<impl Into<String>>) -> Option<Self> {
match profile {
None => Some(ConfigFile::GenericToml(path)),
Some(profile) => {
let profile = profile.into();
Some(ConfigFile::SpecificToml { path, profile })
}
}
}
fn yaml_from(path: PathBuf, profile: Option<impl Into<String>>) -> Option<Self> {
match profile {
None => Some(ConfigFile::GenericYaml(path)),
Some(profile) => {
let profile = profile.into();
Some(ConfigFile::SpecificYaml { path, profile })
}
}
}
}
impl ConfigFile {
/// Reports whether this [`ConfigFile`] is applicable regardless of the
/// [active](AppProfile::active) [`AppProfile`].
pub fn is_generic(&self) -> bool {
match *self {
Self::GenericToml(_) | Self::GenericYaml(_) => true,
Self::SpecificToml { .. } | Self::SpecificYaml { .. } => false,
}
}
/// Reports whether this [`ConfigFile`] is applicable only to a particular
/// [`AppProfile`].
pub fn is_specific(&self) -> bool {
!self.is_generic()
}
/// Returns a reference to the internally held [`PathBuf`].
pub fn path(&self) -> &PathBuf {
match *self {
Self::GenericToml(ref path) => path,
Self::GenericYaml(ref path) => path,
Self::SpecificToml { ref path, .. } => path,
Self::SpecificYaml { ref path, .. } => path,
}
}
/// Returns a reference to the internally held profile name (if this
/// variant is [specific](ConfigFile::is_specific)).
pub fn profile(&self) -> Option<&str> {
match *self {
Self::GenericToml(_) => None,
Self::GenericYaml(_) => None,
Self::SpecificToml { ref profile, .. } => Some(profile),
Self::SpecificYaml { ref profile, .. } => Some(profile),
}
}
/// Reports whether this [`ConfigFile`] [applies](ConfigFile::applies_to) to
/// the [active](AppProfile::active) [`AppProfile`].
pub fn applies_to_active_profile(&self) -> bool {
self.applies_to(AppProfile::active())
}
/// Reports whether this [`ConfigFile`] applies to the given [`AppProfile`].
///
/// A generic config file (without a profile name in its file name) applies
/// to any profile by default. A specific config file (with a profile name
/// in its file name) applies to the given profile if the profile name
/// matches.
pub fn applies_to(&self, profile: impl AsRef<AppProfile>) -> bool {
let given_profile = profile.as_ref();
match *self {
Self::GenericToml(_) | Self::GenericYaml(_) => true,
Self::SpecificToml { ref profile, .. } | Self::SpecificYaml { ref profile, .. } => {
given_profile.is(profile)
}
}
}
}
impl PartialOrd for ConfigFile {
/// Delegates to the [`Ord`] implementation.
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for ConfigFile {
/// Implements the custom ordering rules for [`ConfigFile`].
///
/// First rule is that generics always come before specifics (so that
/// specifics would override generics). After that, both subgroups are
/// ordered by their file path. For specific files, we order by the profile
/// name before we order by the path.
fn cmp(&self, other: &Self) -> Ordering {
// Are the given two generic?
let self_generic = self.is_generic();
let other_generic = other.is_generic();
// If we have a generic against specific, it’s an easy job
match (self_generic, other_generic) {
(true, false) => return Ordering::Less,
(false, true) => return Ordering::Greater,
_ => { /* either both generic or both specific */ }
}
// Extract path references
let self_path = self.path();
let other_path = other.path();
// If both are generic, it’s also an easy job
if self_generic {
return self_path.cmp(other_path);
}
// Both are profile-specific: extract profile names
let self_profile = self.profile();
let other_profile = other.profile();
// Unfortunately, profiles are optional, so check them first
match (self_profile, other_profile) {
// Both have profile name
(Some(self_profile_name), Some(other_profile_name)) => {
// Compare profile names first, then paths
match self_profile_name.cmp(other_profile_name) {
Ordering::Equal => self_path.cmp(other_path),
non_eq => non_eq,
}
}
// No profile names: just compare paths
_ => self_path.cmp(other_path),
}
}
}
/// Reports whether the given string slice is a recognized YAML extension.
fn is_yaml_extension(ext: &str) -> bool {
ext.eq_ignore_ascii_case("yml") || ext.eq_ignore_ascii_case("yaml")
}
/// Reports whether the given string slice is a recognized TOML extension.
fn is_toml_extension(ext: &str) -> bool {
ext.eq_ignore_ascii_case("toml")
}
impl ConfigFile {
/// Returns the corresponding [`FileFormat`].
fn format(&self) -> FileFormat {
match *self {
Self::GenericToml(_) | Self::SpecificToml { .. } => FileFormat::Toml,
Self::GenericYaml(_) | Self::SpecificYaml { .. } => FileFormat::Yaml,
}
}
}
impl From<ConfigFile> for PathBuf {
fn from(file: ConfigFile) -> Self {
match file {
ConfigFile::GenericToml(path) => path,
ConfigFile::GenericYaml(path) => path,
ConfigFile::SpecificToml { path, .. } => path,
ConfigFile::SpecificYaml { path, .. } => path,
}
}
}
impl From<ConfigFile> for File<FileSourceFile, FileFormat> {
fn from(file: ConfigFile) -> Self {
let format = file.format();
let path = PathBuf::from(file);
File::from(path).format(format)
}
}