gix_config/file/init/
mod.rs1use 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;
14pub mod from_env;
16pub mod from_paths;
18
19impl File {
20 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 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 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 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}