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 File {
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            backing: Default::default(),
23            frontmatter_events: Default::default(),
24            frontmatter_post_section: Default::default(),
25            section_lookup_tree: Default::default(),
26            sections: Default::default(),
27            next_section_id: 0,
28            section_order: Default::default(),
29            meta: meta.into(),
30        }
31    }
32
33    /// Instantiate a new `File` from given `input`, associating each section and their values with
34    /// `meta`-data, while respecting `options`.
35    pub fn from_bytes_no_includes(
36        input: &[u8],
37        meta: impl Into<OwnShared<Metadata>>,
38        options: Options<'_>,
39    ) -> Result<Self, Error> {
40        let meta = meta.into();
41        Ok(Self::from_parse_events_no_includes(
42            parse::Events::from_bytes(input, options.to_event_filter())?,
43            meta,
44        ))
45    }
46
47    /// Instantiate a new `File` from given `events`, associating each section and their values with
48    /// `meta`-data.
49    pub fn from_parse_events_no_includes(
50        parse::Events {
51            backing,
52            frontmatter,
53            sections,
54        }: parse::Events,
55        meta: impl Into<OwnShared<Metadata>>,
56    ) -> Self {
57        let meta = meta.into();
58        let mut this = File::new(OwnShared::clone(&meta));
59
60        this.backing = backing;
61        this.frontmatter_events = frontmatter;
62
63        this.sections.reserve(sections.len());
64        this.section_order.reserve(sections.len());
65        for section in sections {
66            this.push_section_internal(crate::file::SectionData {
67                header: section.header,
68                body: section::BodyData(section.events),
69                meta: OwnShared::clone(&meta),
70                id: Default::default(),
71            });
72        }
73        this
74    }
75}
76
77impl File {
78    /// Instantiate a new fully-owned `File` from given `input` (later reused as buffer when resolving includes),
79    /// associating each section and their values with `meta`-data, while respecting `options`, and
80    /// following includes as configured there.
81    pub fn from_bytes_owned(
82        input_and_buf: &mut Vec<u8>,
83        meta: impl Into<OwnShared<Metadata>>,
84        options: Options<'_>,
85    ) -> Result<Self, Error> {
86        let mut config = Self::from_parse_events_no_includes(
87            parse::Events::from_bytes(input_and_buf, options.to_event_filter()).map_err(Error::from)?,
88            meta,
89        );
90
91        includes::resolve(&mut config, input_and_buf, options).map_err(Error::from)?;
92        Ok(config)
93    }
94}