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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
use libc;
use std::ffi::CString;
use std::marker;
use std::path::{Path, PathBuf};
use std::ptr;
use std::str;

use crate::util::{self, Binding};
use crate::{raw, Buf, ConfigLevel, Error, IntoCString};

/// A structure representing a git configuration key/value store
pub struct Config {
    raw: *mut raw::git_config,
}

/// A struct representing a certain entry owned by a `Config` instance.
///
/// An entry has a name, a value, and a level it applies to.
pub struct ConfigEntry<'cfg> {
    raw: *mut raw::git_config_entry,
    _marker: marker::PhantomData<&'cfg Config>,
    owned: bool,
}

/// An iterator over the `ConfigEntry` values of a `Config` structure.
///
/// Due to lifetime restrictions, `ConfigEntries` does not implement the
/// standard [`Iterator`] trait. It provides a [`next`] function which only
/// allows access to one entry at a time. [`for_each`] is available as a
/// convenience function.
///
/// [`next`]: ConfigEntries::next
/// [`for_each`]: ConfigEntries::for_each
///
/// # Example
///
/// ```
/// // Example of how to collect all entries.
/// use git2::Config;
///
/// let config = Config::new()?;
/// let iter = config.entries(None)?;
/// let mut entries = Vec::new();
/// iter
///     .for_each(|entry| {
///         let name = entry.name().unwrap().to_string();
///         let value = entry.value().unwrap_or("").to_string();
///         entries.push((name, value))
///     })?;
/// for entry in &entries {
///     println!("{} = {}", entry.0, entry.1);
/// }
/// # Ok::<(), git2::Error>(())
///
/// ```
pub struct ConfigEntries<'cfg> {
    raw: *mut raw::git_config_iterator,
    current: Option<ConfigEntry<'cfg>>,
    _marker: marker::PhantomData<&'cfg Config>,
}

impl Config {
    /// Allocate a new configuration object
    ///
    /// This object is empty, so you have to add a file to it before you can do
    /// anything with it.
    pub fn new() -> Result<Config, Error> {
        crate::init();
        let mut raw = ptr::null_mut();
        unsafe {
            try_call!(raw::git_config_new(&mut raw));
            Ok(Binding::from_raw(raw))
        }
    }

    /// Create a new config instance containing a single on-disk file
    pub fn open(path: &Path) -> Result<Config, Error> {
        crate::init();
        let mut raw = ptr::null_mut();
        // Normal file path OK (does not need Windows conversion).
        let path = path.into_c_string()?;
        unsafe {
            try_call!(raw::git_config_open_ondisk(&mut raw, path));
            Ok(Binding::from_raw(raw))
        }
    }

    /// Open the global, XDG and system configuration files
    ///
    /// Utility wrapper that finds the global, XDG and system configuration
    /// files and opens them into a single prioritized config object that can
    /// be used when accessing default config data outside a repository.
    pub fn open_default() -> Result<Config, Error> {
        crate::init();
        let mut raw = ptr::null_mut();
        unsafe {
            try_call!(raw::git_config_open_default(&mut raw));
            Ok(Binding::from_raw(raw))
        }
    }

    /// Locate the path to the global configuration file
    ///
    /// The user or global configuration file is usually located in
    /// `$HOME/.gitconfig`.
    ///
    /// This method will try to guess the full path to that file, if the file
    /// exists. The returned path may be used on any method call to load
    /// the global configuration file.
    ///
    /// This method will not guess the path to the XDG compatible config file
    /// (`.config/git/config`).
    pub fn find_global() -> Result<PathBuf, Error> {
        crate::init();
        let buf = Buf::new();
        unsafe {
            try_call!(raw::git_config_find_global(buf.raw()));
        }
        Ok(util::bytes2path(&buf).to_path_buf())
    }

    /// Locate the path to the system configuration file
    ///
    /// If /etc/gitconfig doesn't exist, it will look for `%PROGRAMFILES%`
    pub fn find_system() -> Result<PathBuf, Error> {
        crate::init();
        let buf = Buf::new();
        unsafe {
            try_call!(raw::git_config_find_system(buf.raw()));
        }
        Ok(util::bytes2path(&buf).to_path_buf())
    }

    /// Locate the path to the global XDG compatible configuration file
    ///
    /// The XDG compatible configuration file is usually located in
    /// `$HOME/.config/git/config`.
    pub fn find_xdg() -> Result<PathBuf, Error> {
        crate::init();
        let buf = Buf::new();
        unsafe {
            try_call!(raw::git_config_find_xdg(buf.raw()));
        }
        Ok(util::bytes2path(&buf).to_path_buf())
    }

    /// Add an on-disk config file instance to an existing config
    ///
    /// The on-disk file pointed at by path will be opened and parsed; it's
    /// expected to be a native Git config file following the default Git config
    /// syntax (see man git-config).
    ///
    /// Further queries on this config object will access each of the config
    /// file instances in order (instances with a higher priority level will be
    /// accessed first).
    pub fn add_file(&mut self, path: &Path, level: ConfigLevel, force: bool) -> Result<(), Error> {
        // Normal file path OK (does not need Windows conversion).
        let path = path.into_c_string()?;
        unsafe {
            try_call!(raw::git_config_add_file_ondisk(
                self.raw,
                path,
                level,
                ptr::null(),
                force
            ));
            Ok(())
        }
    }

    /// Delete a config variable from the config file with the highest level
    /// (usually the local one).
    pub fn remove(&mut self, name: &str) -> Result<(), Error> {
        let name = CString::new(name)?;
        unsafe {
            try_call!(raw::git_config_delete_entry(self.raw, name));
            Ok(())
        }
    }

    /// Remove multivar config variables in the config file with the highest level (usually the
    /// local one).
    ///
    /// The regular expression is applied case-sensitively on the value.
    pub fn remove_multivar(&mut self, name: &str, regexp: &str) -> Result<(), Error> {
        let name = CString::new(name)?;
        let regexp = CString::new(regexp)?;
        unsafe {
            try_call!(raw::git_config_delete_multivar(self.raw, name, regexp));
        }
        Ok(())
    }

    /// Get the value of a boolean config variable.
    ///
    /// All config files will be looked into, in the order of their defined
    /// level. A higher level means a higher priority. The first occurrence of
    /// the variable will be returned here.
    pub fn get_bool(&self, name: &str) -> Result<bool, Error> {
        let mut out = 0 as libc::c_int;
        let name = CString::new(name)?;
        unsafe {
            try_call!(raw::git_config_get_bool(&mut out, &*self.raw, name));
        }
        Ok(out != 0)
    }

    /// Get the value of an integer config variable.
    ///
    /// All config files will be looked into, in the order of their defined
    /// level. A higher level means a higher priority. The first occurrence of
    /// the variable will be returned here.
    pub fn get_i32(&self, name: &str) -> Result<i32, Error> {
        let mut out = 0i32;
        let name = CString::new(name)?;
        unsafe {
            try_call!(raw::git_config_get_int32(&mut out, &*self.raw, name));
        }
        Ok(out)
    }

    /// Get the value of an integer config variable.
    ///
    /// All config files will be looked into, in the order of their defined
    /// level. A higher level means a higher priority. The first occurrence of
    /// the variable will be returned here.
    pub fn get_i64(&self, name: &str) -> Result<i64, Error> {
        let mut out = 0i64;
        let name = CString::new(name)?;
        unsafe {
            try_call!(raw::git_config_get_int64(&mut out, &*self.raw, name));
        }
        Ok(out)
    }

    /// Get the value of a string config variable.
    ///
    /// This is the same as `get_bytes` except that it may return `Err` if
    /// the bytes are not valid utf-8.
    ///
    /// This method will return an error if this `Config` is not a snapshot.
    pub fn get_str(&self, name: &str) -> Result<&str, Error> {
        str::from_utf8(self.get_bytes(name)?)
            .map_err(|_| Error::from_str("configuration value is not valid utf8"))
    }

    /// Get the value of a string config variable as a byte slice.
    ///
    /// This method will return an error if this `Config` is not a snapshot.
    pub fn get_bytes(&self, name: &str) -> Result<&[u8], Error> {
        let mut ret = ptr::null();
        let name = CString::new(name)?;
        unsafe {
            try_call!(raw::git_config_get_string(&mut ret, &*self.raw, name));
            Ok(crate::opt_bytes(self, ret).unwrap())
        }
    }

    /// Get the value of a string config variable as an owned string.
    ///
    /// All config files will be looked into, in the order of their
    /// defined level. A higher level means a higher priority. The
    /// first occurrence of the variable will be returned here.
    ///
    /// An error will be returned if the config value is not valid utf-8.
    pub fn get_string(&self, name: &str) -> Result<String, Error> {
        let ret = Buf::new();
        let name = CString::new(name)?;
        unsafe {
            try_call!(raw::git_config_get_string_buf(ret.raw(), self.raw, name));
        }
        str::from_utf8(&ret)
            .map(|s| s.to_string())
            .map_err(|_| Error::from_str("configuration value is not valid utf8"))
    }

    /// Get the value of a path config variable as an owned `PathBuf`.
    ///
    /// A leading '~' will be expanded to the global search path (which
    /// defaults to the user's home directory but can be overridden via
    /// [`raw::git_libgit2_opts`].
    ///
    /// All config files will be looked into, in the order of their
    /// defined level. A higher level means a higher priority. The
    /// first occurrence of the variable will be returned here.
    pub fn get_path(&self, name: &str) -> Result<PathBuf, Error> {
        let ret = Buf::new();
        let name = CString::new(name)?;
        unsafe {
            try_call!(raw::git_config_get_path(ret.raw(), self.raw, name));
        }
        Ok(crate::util::bytes2path(&ret).to_path_buf())
    }

    /// Get the ConfigEntry for a config variable.
    pub fn get_entry(&self, name: &str) -> Result<ConfigEntry<'_>, Error> {
        let mut ret = ptr::null_mut();
        let name = CString::new(name)?;
        unsafe {
            try_call!(raw::git_config_get_entry(&mut ret, self.raw, name));
            Ok(Binding::from_raw(ret))
        }
    }

    /// Iterate over all the config variables
    ///
    /// If `glob` is `Some`, then the iterator will only iterate over all
    /// variables whose name matches the pattern.
    ///
    /// The regular expression is applied case-sensitively on the normalized form of
    /// the variable name: the section and variable parts are lower-cased. The
    /// subsection is left unchanged.
    ///
    /// Due to lifetime restrictions, the returned value does not implement
    /// the standard [`Iterator`] trait. See [`ConfigEntries`] for more.
    ///
    /// # Example
    ///
    /// ```
    /// use git2::Config;
    ///
    /// let cfg = Config::new().unwrap();
    ///
    /// let mut entries = cfg.entries(None).unwrap();
    /// while let Some(entry) = entries.next() {
    ///     let entry = entry.unwrap();
    ///     println!("{} => {}", entry.name().unwrap(), entry.value().unwrap());
    /// }
    /// ```
    pub fn entries(&self, glob: Option<&str>) -> Result<ConfigEntries<'_>, Error> {
        let mut ret = ptr::null_mut();
        unsafe {
            match glob {
                Some(s) => {
                    let s = CString::new(s)?;
                    try_call!(raw::git_config_iterator_glob_new(&mut ret, &*self.raw, s));
                }
                None => {
                    try_call!(raw::git_config_iterator_new(&mut ret, &*self.raw));
                }
            }
            Ok(Binding::from_raw(ret))
        }
    }

    /// Iterate over the values of a multivar
    ///
    /// If `regexp` is `Some`, then the iterator will only iterate over all
    /// values which match the pattern.
    ///
    /// The regular expression is applied case-sensitively on the normalized form of
    /// the variable name: the section and variable parts are lower-cased. The
    /// subsection is left unchanged.
    ///
    /// Due to lifetime restrictions, the returned value does not implement
    /// the standard [`Iterator`] trait. See [`ConfigEntries`] for more.
    pub fn multivar(&self, name: &str, regexp: Option<&str>) -> Result<ConfigEntries<'_>, Error> {
        let mut ret = ptr::null_mut();
        let name = CString::new(name)?;
        let regexp = regexp.map(CString::new).transpose()?;
        unsafe {
            try_call!(raw::git_config_multivar_iterator_new(
                &mut ret, &*self.raw, name, regexp
            ));
            Ok(Binding::from_raw(ret))
        }
    }

    /// Open the global/XDG configuration file according to git's rules
    ///
    /// Git allows you to store your global configuration at `$HOME/.config` or
    /// `$XDG_CONFIG_HOME/git/config`. For backwards compatibility, the XDG file
    /// shouldn't be used unless the use has created it explicitly. With this
    /// function you'll open the correct one to write to.
    pub fn open_global(&mut self) -> Result<Config, Error> {
        let mut raw = ptr::null_mut();
        unsafe {
            try_call!(raw::git_config_open_global(&mut raw, self.raw));
            Ok(Binding::from_raw(raw))
        }
    }

    /// Build a single-level focused config object from a multi-level one.
    ///
    /// The returned config object can be used to perform get/set/delete
    /// operations on a single specific level.
    pub fn open_level(&self, level: ConfigLevel) -> Result<Config, Error> {
        let mut raw = ptr::null_mut();
        unsafe {
            try_call!(raw::git_config_open_level(&mut raw, &*self.raw, level));
            Ok(Binding::from_raw(raw))
        }
    }

    /// Set the value of a boolean config variable in the config file with the
    /// highest level (usually the local one).
    pub fn set_bool(&mut self, name: &str, value: bool) -> Result<(), Error> {
        let name = CString::new(name)?;
        unsafe {
            try_call!(raw::git_config_set_bool(self.raw, name, value));
        }
        Ok(())
    }

    /// Set the value of an integer config variable in the config file with the
    /// highest level (usually the local one).
    pub fn set_i32(&mut self, name: &str, value: i32) -> Result<(), Error> {
        let name = CString::new(name)?;
        unsafe {
            try_call!(raw::git_config_set_int32(self.raw, name, value));
        }
        Ok(())
    }

    /// Set the value of an integer config variable in the config file with the
    /// highest level (usually the local one).
    pub fn set_i64(&mut self, name: &str, value: i64) -> Result<(), Error> {
        let name = CString::new(name)?;
        unsafe {
            try_call!(raw::git_config_set_int64(self.raw, name, value));
        }
        Ok(())
    }

    /// Set the value of an multivar config variable in the config file with the
    /// highest level (usually the local one).
    ///
    /// The regular expression is applied case-sensitively on the value.
    pub fn set_multivar(&mut self, name: &str, regexp: &str, value: &str) -> Result<(), Error> {
        let name = CString::new(name)?;
        let regexp = CString::new(regexp)?;
        let value = CString::new(value)?;
        unsafe {
            try_call!(raw::git_config_set_multivar(self.raw, name, regexp, value));
        }
        Ok(())
    }

    /// Set the value of a string config variable in the config file with the
    /// highest level (usually the local one).
    pub fn set_str(&mut self, name: &str, value: &str) -> Result<(), Error> {
        let name = CString::new(name)?;
        let value = CString::new(value)?;
        unsafe {
            try_call!(raw::git_config_set_string(self.raw, name, value));
        }
        Ok(())
    }

    /// Create a snapshot of the configuration
    ///
    /// Create a snapshot of the current state of a configuration, which allows
    /// you to look into a consistent view of the configuration for looking up
    /// complex values (e.g. a remote, submodule).
    pub fn snapshot(&mut self) -> Result<Config, Error> {
        let mut ret = ptr::null_mut();
        unsafe {
            try_call!(raw::git_config_snapshot(&mut ret, self.raw));
            Ok(Binding::from_raw(ret))
        }
    }

    /// Parse a string as a bool.
    ///
    /// Interprets "true", "yes", "on", 1, or any non-zero number as true.
    /// Interprets "false", "no", "off", 0, or an empty string as false.
    pub fn parse_bool<S: IntoCString>(s: S) -> Result<bool, Error> {
        let s = s.into_c_string()?;
        let mut out = 0;
        crate::init();
        unsafe {
            try_call!(raw::git_config_parse_bool(&mut out, s));
        }
        Ok(out != 0)
    }

    /// Parse a string as an i32; handles suffixes like k, M, or G, and
    /// multiplies by the appropriate power of 1024.
    pub fn parse_i32<S: IntoCString>(s: S) -> Result<i32, Error> {
        let s = s.into_c_string()?;
        let mut out = 0;
        crate::init();
        unsafe {
            try_call!(raw::git_config_parse_int32(&mut out, s));
        }
        Ok(out)
    }

    /// Parse a string as an i64; handles suffixes like k, M, or G, and
    /// multiplies by the appropriate power of 1024.
    pub fn parse_i64<S: IntoCString>(s: S) -> Result<i64, Error> {
        let s = s.into_c_string()?;
        let mut out = 0;
        crate::init();
        unsafe {
            try_call!(raw::git_config_parse_int64(&mut out, s));
        }
        Ok(out)
    }
}

impl Binding for Config {
    type Raw = *mut raw::git_config;
    unsafe fn from_raw(raw: *mut raw::git_config) -> Config {
        Config { raw }
    }
    fn raw(&self) -> *mut raw::git_config {
        self.raw
    }
}

impl Drop for Config {
    fn drop(&mut self) {
        unsafe { raw::git_config_free(self.raw) }
    }
}

impl<'cfg> ConfigEntry<'cfg> {
    /// Gets the name of this entry.
    ///
    /// May return `None` if the name is not valid utf-8
    pub fn name(&self) -> Option<&str> {
        str::from_utf8(self.name_bytes()).ok()
    }

    /// Gets the name of this entry as a byte slice.
    pub fn name_bytes(&self) -> &[u8] {
        unsafe { crate::opt_bytes(self, (*self.raw).name).unwrap() }
    }

    /// Gets the value of this entry.
    ///
    /// May return `None` if the value is not valid utf-8
    ///
    /// # Panics
    ///
    /// Panics when no value is defined.
    pub fn value(&self) -> Option<&str> {
        str::from_utf8(self.value_bytes()).ok()
    }

    /// Gets the value of this entry as a byte slice.
    ///
    /// # Panics
    ///
    /// Panics when no value is defined.
    pub fn value_bytes(&self) -> &[u8] {
        unsafe { crate::opt_bytes(self, (*self.raw).value).unwrap() }
    }

    /// Returns `true` when a value is defined otherwise `false`.
    ///
    /// No value defined is a short-hand to represent a Boolean `true`.
    pub fn has_value(&self) -> bool {
        unsafe { !(*self.raw).value.is_null() }
    }

    /// Gets the configuration level of this entry.
    pub fn level(&self) -> ConfigLevel {
        unsafe { ConfigLevel::from_raw((*self.raw).level) }
    }

    /// Depth of includes where this variable was found
    pub fn include_depth(&self) -> u32 {
        unsafe { (*self.raw).include_depth as u32 }
    }
}

impl<'cfg> Binding for ConfigEntry<'cfg> {
    type Raw = *mut raw::git_config_entry;

    unsafe fn from_raw(raw: *mut raw::git_config_entry) -> ConfigEntry<'cfg> {
        ConfigEntry {
            raw,
            _marker: marker::PhantomData,
            owned: true,
        }
    }
    fn raw(&self) -> *mut raw::git_config_entry {
        self.raw
    }
}

impl<'cfg> Binding for ConfigEntries<'cfg> {
    type Raw = *mut raw::git_config_iterator;

    unsafe fn from_raw(raw: *mut raw::git_config_iterator) -> ConfigEntries<'cfg> {
        ConfigEntries {
            raw,
            current: None,
            _marker: marker::PhantomData,
        }
    }
    fn raw(&self) -> *mut raw::git_config_iterator {
        self.raw
    }
}

impl<'cfg> ConfigEntries<'cfg> {
    /// Advances the iterator and returns the next value.
    ///
    /// Returns `None` when iteration is finished.
    pub fn next(&mut self) -> Option<Result<&ConfigEntry<'cfg>, Error>> {
        let mut raw = ptr::null_mut();
        drop(self.current.take());
        unsafe {
            try_call_iter!(raw::git_config_next(&mut raw, self.raw));
            let entry = ConfigEntry {
                owned: false,
                raw,
                _marker: marker::PhantomData,
            };
            self.current = Some(entry);
            Some(Ok(self.current.as_ref().unwrap()))
        }
    }

    /// Calls the given closure for each remaining entry in the iterator.
    pub fn for_each<F: FnMut(&ConfigEntry<'cfg>)>(mut self, mut f: F) -> Result<(), Error> {
        while let Some(entry) = self.next() {
            let entry = entry?;
            f(entry);
        }
        Ok(())
    }
}

impl<'cfg> Drop for ConfigEntries<'cfg> {
    fn drop(&mut self) {
        unsafe { raw::git_config_iterator_free(self.raw) }
    }
}

impl<'cfg> Drop for ConfigEntry<'cfg> {
    fn drop(&mut self) {
        if self.owned {
            unsafe { raw::git_config_entry_free(self.raw) }
        }
    }
}

#[cfg(test)]
mod tests {
    use std::fs::File;
    use tempfile::TempDir;

    use crate::Config;

    #[test]
    fn smoke() {
        let _cfg = Config::new().unwrap();
        let _ = Config::find_global();
        let _ = Config::find_system();
        let _ = Config::find_xdg();
    }

    #[test]
    fn persisted() {
        let td = TempDir::new().unwrap();
        let path = td.path().join("foo");
        File::create(&path).unwrap();

        let mut cfg = Config::open(&path).unwrap();
        assert!(cfg.get_bool("foo.bar").is_err());
        cfg.set_bool("foo.k1", true).unwrap();
        cfg.set_i32("foo.k2", 1).unwrap();
        cfg.set_i64("foo.k3", 2).unwrap();
        cfg.set_str("foo.k4", "bar").unwrap();
        cfg.snapshot().unwrap();
        drop(cfg);

        let cfg = Config::open(&path).unwrap().snapshot().unwrap();
        assert_eq!(cfg.get_bool("foo.k1").unwrap(), true);
        assert_eq!(cfg.get_i32("foo.k2").unwrap(), 1);
        assert_eq!(cfg.get_i64("foo.k3").unwrap(), 2);
        assert_eq!(cfg.get_str("foo.k4").unwrap(), "bar");

        let mut entries = cfg.entries(None).unwrap();
        while let Some(entry) = entries.next() {
            let entry = entry.unwrap();
            entry.name();
            entry.value();
            entry.level();
        }
    }

    #[test]
    fn multivar() {
        let td = TempDir::new().unwrap();
        let path = td.path().join("foo");
        File::create(&path).unwrap();

        let mut cfg = Config::open(&path).unwrap();
        cfg.set_multivar("foo.bar", "^$", "baz").unwrap();
        cfg.set_multivar("foo.bar", "^$", "qux").unwrap();
        cfg.set_multivar("foo.bar", "^$", "quux").unwrap();
        cfg.set_multivar("foo.baz", "^$", "oki").unwrap();

        // `entries` filters by name
        let mut entries: Vec<String> = Vec::new();
        cfg.entries(Some("foo.bar"))
            .unwrap()
            .for_each(|entry| entries.push(entry.value().unwrap().to_string()))
            .unwrap();
        entries.sort();
        assert_eq!(entries, ["baz", "quux", "qux"]);

        // which is the same as `multivar` without a regex
        let mut multivals = Vec::new();
        cfg.multivar("foo.bar", None)
            .unwrap()
            .for_each(|entry| multivals.push(entry.value().unwrap().to_string()))
            .unwrap();
        multivals.sort();
        assert_eq!(multivals, entries);

        // yet _with_ a regex, `multivar` filters by value
        let mut quxish = Vec::new();
        cfg.multivar("foo.bar", Some("qu.*x"))
            .unwrap()
            .for_each(|entry| quxish.push(entry.value().unwrap().to_string()))
            .unwrap();
        quxish.sort();
        assert_eq!(quxish, ["quux", "qux"]);

        cfg.remove_multivar("foo.bar", ".*").unwrap();

        let count = |entries: super::ConfigEntries<'_>| -> usize {
            let mut c = 0;
            entries.for_each(|_| c += 1).unwrap();
            c
        };

        assert_eq!(count(cfg.entries(Some("foo.bar")).unwrap()), 0);
        assert_eq!(count(cfg.multivar("foo.bar", None).unwrap()), 0);
    }

    #[test]
    fn parse() {
        assert_eq!(Config::parse_bool("").unwrap(), false);
        assert_eq!(Config::parse_bool("false").unwrap(), false);
        assert_eq!(Config::parse_bool("no").unwrap(), false);
        assert_eq!(Config::parse_bool("off").unwrap(), false);
        assert_eq!(Config::parse_bool("0").unwrap(), false);

        assert_eq!(Config::parse_bool("true").unwrap(), true);
        assert_eq!(Config::parse_bool("yes").unwrap(), true);
        assert_eq!(Config::parse_bool("on").unwrap(), true);
        assert_eq!(Config::parse_bool("1").unwrap(), true);
        assert_eq!(Config::parse_bool("42").unwrap(), true);

        assert!(Config::parse_bool(" ").is_err());
        assert!(Config::parse_bool("some-string").is_err());
        assert!(Config::parse_bool("-").is_err());

        assert_eq!(Config::parse_i32("0").unwrap(), 0);
        assert_eq!(Config::parse_i32("1").unwrap(), 1);
        assert_eq!(Config::parse_i32("100").unwrap(), 100);
        assert_eq!(Config::parse_i32("-1").unwrap(), -1);
        assert_eq!(Config::parse_i32("-100").unwrap(), -100);
        assert_eq!(Config::parse_i32("1k").unwrap(), 1024);
        assert_eq!(Config::parse_i32("4k").unwrap(), 4096);
        assert_eq!(Config::parse_i32("1M").unwrap(), 1048576);
        assert_eq!(Config::parse_i32("1G").unwrap(), 1024 * 1024 * 1024);

        assert_eq!(Config::parse_i64("0").unwrap(), 0);
        assert_eq!(Config::parse_i64("1").unwrap(), 1);
        assert_eq!(Config::parse_i64("100").unwrap(), 100);
        assert_eq!(Config::parse_i64("-1").unwrap(), -1);
        assert_eq!(Config::parse_i64("-100").unwrap(), -100);
        assert_eq!(Config::parse_i64("1k").unwrap(), 1024);
        assert_eq!(Config::parse_i64("4k").unwrap(), 4096);
        assert_eq!(Config::parse_i64("1M").unwrap(), 1048576);
        assert_eq!(Config::parse_i64("1G").unwrap(), 1024 * 1024 * 1024);
        assert_eq!(Config::parse_i64("100G").unwrap(), 100 * 1024 * 1024 * 1024);
    }
}