Skip to main content

gix_config/file/init/
mod.rs

1use gix_error::ExnResult;
2use gix_features::threading::OwnShared;
3
4use crate::{
5    File,
6    file::{Metadata, includes, section},
7    parse,
8};
9
10mod types;
11pub use types::Options;
12
13mod comfort;
14///
15pub mod from_env;
16///
17pub mod from_paths;
18
19impl File {
20    /// Return an empty `File` with the given `meta`-data to be attached to all new sections.
21    pub fn new(meta: impl Into<OwnShared<Metadata>>) -> Self {
22        Self {
23            backing: Default::default(),
24            frontmatter_events: Default::default(),
25            frontmatter_post_section: Default::default(),
26            section_lookup_tree: Default::default(),
27            sections: Default::default(),
28            next_section_id: 0,
29            section_order: Default::default(),
30            meta: meta.into(),
31        }
32    }
33
34    /// Instantiate a new `File` from given `input`, associating each section and their values with
35    /// `meta`-data, while respecting `options`.
36    pub fn from_bytes_no_includes(
37        input: &[u8],
38        meta: impl Into<OwnShared<Metadata>>,
39        options: Options<'_>,
40    ) -> ExnResult<Self> {
41        use gix_error::{ResultExt, message};
42        let meta = meta.into();
43        Ok(Self::from_parse_events_no_includes(
44            parse::Events::from_bytes(input, options.to_event_filter())
45                .or_raise_erased(|| message("Could not parse configuration"))?,
46            meta,
47        ))
48    }
49
50    /// Instantiate a new `File` from given `events`, associating each section and their values with
51    /// `meta`-data.
52    pub fn from_parse_events_no_includes(
53        parse::Events {
54            backing,
55            frontmatter,
56            sections,
57        }: parse::Events,
58        meta: impl Into<OwnShared<Metadata>>,
59    ) -> Self {
60        let meta = meta.into();
61        let mut this = File::new(OwnShared::clone(&meta));
62
63        this.backing = backing;
64        this.frontmatter_events = frontmatter;
65
66        this.sections.reserve(sections.len());
67        this.section_order.reserve(sections.len());
68        for section in sections {
69            this.push_section_internal(crate::file::SectionData {
70                header: section.header,
71                body: section::BodyData(section.events),
72                meta: OwnShared::clone(&meta),
73                id: Default::default(),
74            });
75        }
76        this
77    }
78}
79
80impl File {
81    /// Instantiate a new fully-owned `File` from given `input` (later reused as buffer when resolving includes),
82    /// associating each section and their values with `meta`-data, while respecting `options`, and
83    /// following includes as configured there.
84    pub fn from_bytes_owned(
85        input_and_buf: &mut Vec<u8>,
86        meta: impl Into<OwnShared<Metadata>>,
87        options: Options<'_>,
88    ) -> ExnResult<Self> {
89        use gix_error::{ResultExt, message};
90        let mut config = Self::from_parse_events_no_includes(
91            parse::Events::from_bytes(input_and_buf, options.to_event_filter())
92                .or_raise_erased(|| message("Could not parse configuration"))?,
93            meta,
94        );
95
96        includes::resolve(&mut config, input_and_buf, options)
97            .or_raise_erased(|| message("Could not resolve configuration includes"))?;
98        Ok(config)
99    }
100}