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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#![allow(clippy::result_large_err)]
use std::{
borrow::Cow,
error::Error,
fmt::{Debug, Formatter},
};
use crate::{
bstr::BStr,
config,
config::tree::{Key, Link, Note, Section, SubSectionRequirement},
};
pub struct Any<T: Validate = validate::All> {
pub name: &'static str,
pub section: &'static dyn Section,
pub subsection_requirement: Option<SubSectionRequirement>,
pub link: Option<Link>,
pub note: Option<Note>,
validate: T,
}
impl Any<validate::All> {
pub const fn new(name: &'static str, section: &'static dyn Section) -> Self {
Any::new_with_validate(name, section, validate::All)
}
}
impl<T: Validate> Any<T> {
pub const fn new_with_validate(name: &'static str, section: &'static dyn Section, validate: T) -> Self {
Any {
name,
section,
subsection_requirement: Some(SubSectionRequirement::Never),
link: None,
note: None,
validate,
}
}
}
impl<T: Validate> Any<T> {
pub const fn with_subsection_requirement(mut self, requirement: Option<SubSectionRequirement>) -> Self {
self.subsection_requirement = requirement;
self
}
pub const fn with_environment_override(mut self, var: &'static str) -> Self {
self.link = Some(Link::EnvironmentOverride(var));
self
}
pub const fn with_fallback(mut self, key: &'static dyn Key) -> Self {
self.link = Some(Link::FallbackKey(key));
self
}
pub const fn with_note(mut self, message: &'static str) -> Self {
self.note = Some(Note::Informative(message));
self
}
pub const fn with_deviation(mut self, message: &'static str) -> Self {
self.note = Some(Note::Deviation(message));
self
}
}
impl<T: Validate> Any<T> {
pub fn try_into_refspec(
&'static self,
value: std::borrow::Cow<'_, BStr>,
op: git_refspec::parse::Operation,
) -> Result<git_refspec::RefSpec, config::refspec::Error> {
git_refspec::parse(value.as_ref(), op)
.map(|spec| spec.to_owned())
.map_err(|err| config::refspec::Error::from_value(self, value.into_owned()).with_source(err))
}
pub fn try_into_string(&'static self, value: Cow<'_, BStr>) -> Result<std::string::String, config::string::Error> {
use crate::bstr::ByteVec;
Vec::from(value.into_owned()).into_string().map_err(|err| {
let utf8_err = err.utf8_error().clone();
config::string::Error::from_value(self, err.into_vec().into()).with_source(utf8_err)
})
}
}
impl<T: Validate> Debug for Any<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.logical_name().fmt(f)
}
}
impl<T: Validate> std::fmt::Display for Any<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.logical_name())
}
}
impl<T: Validate> Key for Any<T> {
fn name(&self) -> &str {
self.name
}
fn validate(&self, value: &BStr) -> Result<(), config::tree::key::validate::Error> {
Ok(self.validate.validate(value)?)
}
fn section(&self) -> &dyn Section {
self.section
}
fn subsection_requirement(&self) -> Option<&SubSectionRequirement> {
self.subsection_requirement.as_ref()
}
fn link(&self) -> Option<&Link> {
self.link.as_ref()
}
fn note(&self) -> Option<&Note> {
self.note.as_ref()
}
}
pub type Time = Any<validate::Time>;
pub type LockTimeout = Any<validate::LockTimeout>;
pub type DurationInMilliseconds = Any<validate::DurationInMilliseconds>;
pub type UnsignedInteger = Any<validate::UnsignedInteger>;
pub type RemoteName = Any<validate::RemoteName>;
pub type Boolean = Any<validate::Boolean>;
pub type Program = Any<validate::Program>;
pub type Executable = Any<validate::Executable>;
pub type Path = Any<validate::Path>;
pub type Url = Any<validate::Url>;
pub type String = Any<validate::String>;
pub type PushRefSpec = Any<validate::PushRefSpec>;
pub type FetchRefSpec = Any<validate::FetchRefSpec>;
mod duration {
use std::time::Duration;
use crate::{
config,
config::tree::{keys::DurationInMilliseconds, Section},
};
impl DurationInMilliseconds {
pub const fn new_duration(name: &'static str, section: &'static dyn Section) -> Self {
Self::new_with_validate(name, section, super::validate::DurationInMilliseconds)
}
pub fn try_into_duration(
&'static self,
value: Result<i64, git_config::value::Error>,
) -> Result<std::time::Duration, config::duration::Error> {
let value = value.map_err(|err| config::duration::Error::from(self).with_source(err))?;
Ok(match value {
val if val < 0 => Duration::from_secs(u64::MAX),
val => Duration::from_millis(val.try_into().expect("i64 to u64 always works if positive")),
})
}
}
}
mod lock_timeout {
use std::time::Duration;
use git_lock::acquire::Fail;
use crate::{
config,
config::tree::{keys::LockTimeout, Section},
};
impl LockTimeout {
pub const fn new_lock_timeout(name: &'static str, section: &'static dyn Section) -> Self {
Self::new_with_validate(name, section, super::validate::LockTimeout)
}
pub fn try_into_lock_timeout(
&'static self,
value: Result<i64, git_config::value::Error>,
) -> Result<git_lock::acquire::Fail, config::lock_timeout::Error> {
let value = value.map_err(|err| config::lock_timeout::Error::from(self).with_source(err))?;
Ok(match value {
val if val < 0 => Fail::AfterDurationWithBackoff(Duration::from_secs(u64::MAX)),
val if val == 0 => Fail::Immediately,
val => Fail::AfterDurationWithBackoff(Duration::from_millis(
val.try_into().expect("i64 to u64 always works if positive"),
)),
})
}
}
}
mod refspecs {
use crate::config::tree::{
keys::{validate, FetchRefSpec, PushRefSpec},
Section,
};
impl PushRefSpec {
pub const fn new_push_refspec(name: &'static str, section: &'static dyn Section) -> Self {
Self::new_with_validate(name, section, validate::PushRefSpec)
}
}
impl FetchRefSpec {
pub const fn new_fetch_refspec(name: &'static str, section: &'static dyn Section) -> Self {
Self::new_with_validate(name, section, validate::FetchRefSpec)
}
}
}
mod url {
use std::borrow::Cow;
use crate::{
bstr::BStr,
config,
config::tree::{
keys::{validate, Url},
Section,
},
};
impl Url {
pub const fn new_url(name: &'static str, section: &'static dyn Section) -> Self {
Self::new_with_validate(name, section, validate::Url)
}
pub fn try_into_url(&'static self, value: Cow<'_, BStr>) -> Result<git_url::Url, config::url::Error> {
git_url::parse(value.as_ref())
.map_err(|err| config::url::Error::from_value(self, value.into_owned()).with_source(err))
}
}
}
impl String {
pub const fn new_string(name: &'static str, section: &'static dyn Section) -> Self {
Self::new_with_validate(name, section, validate::String)
}
}
impl Program {
pub const fn new_program(name: &'static str, section: &'static dyn Section) -> Self {
Self::new_with_validate(name, section, validate::Program)
}
}
impl Executable {
pub const fn new_executable(name: &'static str, section: &'static dyn Section) -> Self {
Self::new_with_validate(name, section, validate::Executable)
}
}
impl Path {
pub const fn new_path(name: &'static str, section: &'static dyn Section) -> Self {
Self::new_with_validate(name, section, validate::Path)
}
}
mod workers {
use crate::config::tree::{keys::UnsignedInteger, Section};
impl UnsignedInteger {
pub const fn new_unsigned_integer(name: &'static str, section: &'static dyn Section) -> Self {
Self::new_with_validate(name, section, super::validate::UnsignedInteger)
}
pub fn try_into_usize(
&'static self,
value: Result<i64, git_config::value::Error>,
) -> Result<usize, crate::config::unsigned_integer::Error> {
value
.map_err(|err| crate::config::unsigned_integer::Error::from(self).with_source(err))
.and_then(|value| {
value
.try_into()
.map_err(|_| crate::config::unsigned_integer::Error::from(self))
})
}
pub fn try_into_u64(
&'static self,
value: Result<i64, git_config::value::Error>,
) -> Result<u64, crate::config::unsigned_integer::Error> {
value
.map_err(|err| crate::config::unsigned_integer::Error::from(self).with_source(err))
.and_then(|value| {
value
.try_into()
.map_err(|_| crate::config::unsigned_integer::Error::from(self))
})
}
pub fn try_into_u32(
&'static self,
value: Result<i64, git_config::value::Error>,
) -> Result<u32, crate::config::unsigned_integer::Error> {
value
.map_err(|err| crate::config::unsigned_integer::Error::from(self).with_source(err))
.and_then(|value| {
value
.try_into()
.map_err(|_| crate::config::unsigned_integer::Error::from(self))
})
}
}
}
mod time {
use std::borrow::Cow;
use crate::{
bstr::{BStr, ByteSlice},
config::tree::{
keys::{validate, Time},
Section,
},
};
impl Time {
pub const fn new_time(name: &'static str, section: &'static dyn Section) -> Self {
Self::new_with_validate(name, section, validate::Time)
}
pub fn try_into_time(
&self,
value: Cow<'_, BStr>,
now: Option<std::time::SystemTime>,
) -> Result<git_date::Time, git_date::parse::Error> {
git_date::parse(
value
.as_ref()
.to_str()
.map_err(|_| git_date::parse::Error::InvalidDateString {
input: value.to_string(),
})?,
now,
)
}
}
}
mod boolean {
use crate::{
config,
config::tree::{
keys::{validate, Boolean},
Section,
},
};
impl Boolean {
pub const fn new_boolean(name: &'static str, section: &'static dyn Section) -> Self {
Self::new_with_validate(name, section, validate::Boolean)
}
pub fn enrich_error(
&'static self,
value: Result<bool, git_config::value::Error>,
) -> Result<bool, config::boolean::Error> {
value.map_err(|err| config::boolean::Error::from(self).with_source(err))
}
}
}
mod remote_name {
use std::borrow::Cow;
use crate::{
bstr::{BStr, BString},
config,
config::tree::{keys::RemoteName, Section},
};
impl RemoteName {
pub const fn new_remote_name(name: &'static str, section: &'static dyn Section) -> Self {
Self::new_with_validate(name, section, super::validate::RemoteName)
}
#[allow(clippy::result_large_err)]
pub fn try_into_symbolic_name(
&'static self,
name: Cow<'_, BStr>,
) -> Result<BString, config::remote::symbolic_name::Error> {
crate::remote::name::validated(name.into_owned())
.map_err(|err| config::remote::symbolic_name::Error::from(self).with_source(err))
}
}
}
pub trait Validate {
fn validate(&self, value: &BStr) -> Result<(), Box<dyn Error + Send + Sync + 'static>>;
}
pub mod validate {
use std::{borrow::Cow, error::Error};
use crate::{
bstr::{BStr, ByteSlice},
config::tree::keys::Validate,
remote,
};
#[derive(Default)]
pub struct All;
impl Validate for All {
fn validate(&self, _value: &BStr) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
Ok(())
}
}
#[derive(Default)]
pub struct Time;
impl Validate for Time {
fn validate(&self, value: &BStr) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
git_date::parse(value.to_str()?, std::time::SystemTime::now().into())?;
Ok(())
}
}
#[derive(Default)]
pub struct UnsignedInteger;
impl Validate for UnsignedInteger {
fn validate(&self, value: &BStr) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
usize::try_from(
git_config::Integer::try_from(value)?
.to_decimal()
.ok_or_else(|| format!("integer {value} cannot be represented as `usize`"))?,
)?;
Ok(())
}
}
#[derive(Default)]
pub struct Boolean;
impl Validate for Boolean {
fn validate(&self, value: &BStr) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
git_config::Boolean::try_from(value)?;
Ok(())
}
}
#[derive(Default)]
pub struct RemoteName;
impl Validate for RemoteName {
fn validate(&self, value: &BStr) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
remote::Name::try_from(Cow::Borrowed(value))
.map_err(|_| format!("Illformed UTF-8 in remote name: \"{}\"", value.to_str_lossy()))?;
Ok(())
}
}
#[derive(Default)]
pub struct Program;
impl Validate for Program {
fn validate(&self, _value: &BStr) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
Ok(())
}
}
#[derive(Default)]
pub struct Executable;
impl Validate for Executable {
fn validate(&self, _value: &BStr) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
Ok(())
}
}
#[derive(Default)]
pub struct Url;
impl Validate for Url {
fn validate(&self, value: &BStr) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
git_url::parse(value)?;
Ok(())
}
}
#[derive(Default)]
pub struct PushRefSpec;
impl Validate for PushRefSpec {
fn validate(&self, value: &BStr) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
git_refspec::parse(value, git_refspec::parse::Operation::Push)?;
Ok(())
}
}
#[derive(Default)]
pub struct FetchRefSpec;
impl Validate for FetchRefSpec {
fn validate(&self, value: &BStr) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
git_refspec::parse(value, git_refspec::parse::Operation::Fetch)?;
Ok(())
}
}
pub struct LockTimeout;
impl Validate for LockTimeout {
fn validate(&self, value: &BStr) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
let value = git_config::Integer::try_from(value)?
.to_decimal()
.ok_or_else(|| format!("integer {value} cannot be represented as integer"));
super::super::Core::FILES_REF_LOCK_TIMEOUT.try_into_lock_timeout(Ok(value?))?;
Ok(())
}
}
pub struct DurationInMilliseconds;
impl Validate for DurationInMilliseconds {
fn validate(&self, value: &BStr) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
let value = git_config::Integer::try_from(value)?
.to_decimal()
.ok_or_else(|| format!("integer {value} cannot be represented as integer"));
super::super::gitoxide::Http::CONNECT_TIMEOUT.try_into_duration(Ok(value?))?;
Ok(())
}
}
pub struct String;
impl Validate for String {
fn validate(&self, value: &BStr) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
value.to_str()?;
Ok(())
}
}
pub struct Path;
impl Validate for Path {
fn validate(&self, _value: &BStr) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
Ok(())
}
}
}