use gix_features::threading::OwnShared; use crate::{ File, file::{Metadata, includes, section}, parse, }; mod types; pub use types::{Error, Options}; mod comfort; /// pub mod from_env; /// pub mod from_paths; impl File { /// Return an empty `File` with the given `meta`-data to be attached to all new sections. pub fn new(meta: impl Into>) -> Self { Self { backing: Default::default(), frontmatter_events: Default::default(), frontmatter_post_section: Default::default(), section_lookup_tree: Default::default(), sections: Default::default(), next_section_id: 0, section_order: Default::default(), meta: meta.into(), } } /// Instantiate a new `File` from given `input`, associating each section and their values with /// `meta`-data, while respecting `options`. pub fn from_bytes_no_includes( input: &[u8], meta: impl Into>, options: Options<'_>, ) -> Result { let meta = meta.into(); Ok(Self::from_parse_events_no_includes( parse::Events::from_bytes(input, options.to_event_filter())?, meta, )) } /// Instantiate a new `File` from given `events`, associating each section and their values with /// `meta`-data. pub fn from_parse_events_no_includes( parse::Events { backing, frontmatter, sections, }: parse::Events, meta: impl Into>, ) -> Self { let meta = meta.into(); let mut this = File::new(OwnShared::clone(&meta)); this.backing = backing; this.frontmatter_events = frontmatter; this.sections.reserve(sections.len()); this.section_order.reserve(sections.len()); for section in sections { this.push_section_internal(crate::file::SectionData { header: section.header, body: section::BodyData(section.events), meta: OwnShared::clone(&meta), id: Default::default(), }); } this } } impl File { /// Instantiate a new fully-owned `File` from given `input` (later reused as buffer when resolving includes), /// associating each section and their values with `meta`-data, while respecting `options`, and /// following includes as configured there. pub fn from_bytes_owned( input_and_buf: &mut Vec, meta: impl Into>, options: Options<'_>, ) -> Result { let mut config = Self::from_parse_events_no_includes( parse::Events::from_bytes(input_and_buf, options.to_event_filter()).map_err(Error::from)?, meta, ); includes::resolve(&mut config, input_and_buf, options).map_err(Error::from)?; Ok(config) } }