Skip to main content

gix_config/file/init/
mod.rs

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