Skip to main content

gix_config/file/init/
types.rs

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