gix_config/file/init/
mod.rs

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