gix_config/file/init/
types.rs

1use crate::{file::init, parse, parse::Event, path::interpolate};
2
3/// The error returned by [`File::from_bytes_no_includes()`][crate::File::from_bytes_no_includes()].
4#[derive(Debug, thiserror::Error)]
5#[allow(missing_docs)]
6pub enum Error {
7    #[error(transparent)]
8    Parse(#[from] parse::Error),
9    #[error(transparent)]
10    Interpolate(#[from] interpolate::Error),
11    #[error(transparent)]
12    Includes(#[from] init::includes::Error),
13}
14
15/// Options when loading git config using [`File::from_paths_metadata()`][crate::File::from_paths_metadata()].
16#[derive(Clone, Copy, Default)]
17pub struct Options<'a> {
18    /// Configure how to follow includes while handling paths.
19    pub includes: init::includes::Options<'a>,
20    /// If true, only value-bearing parse events will be kept to reduce memory usage and increase performance.
21    ///
22    /// Note that doing so will degenerate [`write_to()`][crate::File::write_to()] and strip it off its comments
23    /// and additional whitespace entirely, but will otherwise be a valid configuration file.
24    pub lossy: bool,
25    /// If true, any IO error happening when reading a configuration file will be ignored.
26    ///
27    /// That way it's possible to pass multiple files and read as many as possible, to have 'something' instead of nothing.
28    pub ignore_io_errors: bool,
29}
30
31impl Options<'_> {
32    pub(crate) fn to_event_filter(self) -> Option<fn(&Event<'_>) -> bool> {
33        if self.lossy {
34            Some(discard_nonessential_events)
35        } else {
36            None
37        }
38    }
39}
40
41fn discard_nonessential_events(e: &Event<'_>) -> bool {
42    match e {
43        Event::Whitespace(_) | Event::Comment(_) | Event::Newline(_) => false,
44        Event::SectionHeader(_)
45        | Event::SectionValueName(_)
46        | Event::KeyValueSeparator
47        | Event::Value(_)
48        | Event::ValueNotDone(_)
49        | Event::ValueDone(_) => true,
50    }
51}