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
#![allow(unused)]
#![allow(clippy::result_unit_err)]
use std::{
borrow::Cow,
convert::TryFrom,
path::{Path, PathBuf},
};
use crate::file::{GitConfig, GitConfigError};
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum ConfigSource {
System,
Global,
User,
Repository,
Env,
Cli,
}
#[derive(Debug, PartialEq, Clone, Eq, Hash, Default)]
pub struct ConfigBuilder {
no_system: bool,
load_env_conf: bool,
override_system_config: Option<PathBuf>,
override_global_config: Option<PathBuf>,
override_repo_config: Option<PathBuf>,
}
impl ConfigBuilder {
#[inline]
#[must_use]
pub fn new() -> Self {
Self {
load_env_conf: true,
..Self::default()
}
}
#[must_use]
pub fn no_system(&mut self, no_system: bool) -> &mut Self {
self.no_system = no_system;
self
}
#[must_use]
pub fn load_environment_entries(&mut self, load_conf: bool) -> &mut Self {
self.load_env_conf = load_conf;
self
}
#[must_use]
pub fn system_config_path(&mut self, path: Option<PathBuf>) -> &mut Self {
self.override_system_config = path;
self
}
#[must_use]
pub fn global_config_path(&mut self, path: Option<PathBuf>) -> &mut Self {
self.override_global_config = path;
self
}
#[must_use]
pub fn repository_config_path(&mut self, path: Option<PathBuf>) -> &mut Self {
self.override_repo_config = path;
self
}
#[must_use]
pub fn build(&self) -> Config {
let system_conf = if self.no_system { None } else { todo!() };
let global_conf = {
let path = self
.override_global_config
.as_ref()
.map_or_else(|| Path::new(".git/config"), PathBuf::as_path);
GitConfig::open(path).ok()
};
let env_conf = if self.load_env_conf {
GitConfig::from_env().ok().flatten()
} else {
None
};
Config {
system_conf,
global_conf,
user_conf: todo!(),
repository_conf: todo!(),
worktree_conf: todo!(),
env_conf,
cli_conf: todo!(),
}
}
pub fn try_build(&self) -> Result<Config, ()> {
todo!()
}
}
pub struct Config<'config> {
system_conf: Option<GitConfig<'config>>,
global_conf: Option<GitConfig<'config>>,
user_conf: Option<GitConfig<'config>>,
repository_conf: Option<GitConfig<'config>>,
worktree_conf: Option<GitConfig<'config>>,
env_conf: Option<GitConfig<'config>>,
cli_conf: Option<GitConfig<'config>>,
}
impl<'config> Config<'config> {
#[inline]
#[must_use]
pub fn value<T: TryFrom<Cow<'config, [u8]>>>(
&'config self,
section_name: &str,
subsection_name: Option<&str>,
key: &str,
) -> Option<T> {
self.value_with_source(section_name, subsection_name, key)
.map(|(value, _)| value)
}
fn value_with_source<T: TryFrom<Cow<'config, [u8]>>>(
&'config self,
section_name: &str,
subsection_name: Option<&str>,
key: &str,
) -> Option<(T, ConfigSource)> {
let mapping = self.mapping();
for (conf, source) in mapping {
if let Some(conf) = conf {
if let Ok(v) = conf.value(section_name, subsection_name, key) {
return Some((v, source));
}
}
}
None
}
#[inline]
pub fn try_value<'lookup, T: TryFrom<Cow<'config, [u8]>>>(
&'config self,
section_name: &'lookup str,
subsection_name: Option<&'lookup str>,
key: &'lookup str,
) -> Result<Option<T>, GitConfigError<'lookup>> {
self.try_value_with_source(section_name, subsection_name, key)
.map(|res| res.map(|(value, _)| value))
}
pub fn try_value_with_source<'lookup, T: TryFrom<Cow<'config, [u8]>>>(
&'config self,
section_name: &'lookup str,
subsection_name: Option<&'lookup str>,
key: &'lookup str,
) -> Result<Option<(T, ConfigSource)>, GitConfigError<'lookup>> {
let mapping = self.mapping();
for (conf, source) in mapping {
if let Some(conf) = conf {
return Ok(Some((conf.value(section_name, subsection_name, key)?, source)));
}
}
Ok(None)
}
const fn mapping(&self) -> [(&Option<GitConfig>, ConfigSource); 6] {
[
(&self.cli_conf, ConfigSource::Cli),
(&self.env_conf, ConfigSource::Env),
(&self.repository_conf, ConfigSource::Repository),
(&self.user_conf, ConfigSource::User),
(&self.global_conf, ConfigSource::Global),
(&self.system_conf, ConfigSource::System),
]
}
}
impl<'config> Config<'config> {
#[must_use]
pub fn get_config(&self, source: ConfigSource) -> Option<&GitConfig<'config>> {
match source {
ConfigSource::System => self.system_conf.as_ref(),
ConfigSource::Global => self.global_conf.as_ref(),
ConfigSource::User => self.user_conf.as_ref(),
ConfigSource::Repository => self.repository_conf.as_ref(),
ConfigSource::Env => self.env_conf.as_ref(),
ConfigSource::Cli => self.cli_conf.as_ref(),
}
}
#[must_use]
pub fn get_config_mut(&mut self, source: ConfigSource) -> Option<&mut GitConfig<'config>> {
match source {
ConfigSource::System => self.system_conf.as_mut(),
ConfigSource::Global => self.global_conf.as_mut(),
ConfigSource::User => self.user_conf.as_mut(),
ConfigSource::Repository => self.repository_conf.as_mut(),
ConfigSource::Env => self.env_conf.as_mut(),
ConfigSource::Cli => self.cli_conf.as_mut(),
}
}
}