use super::{dispatch_read, enrich_read};
use crate::structure::Structure;
use molframe_core::ExecutionContext;
use molframe_core::contract::ContentDigest;
use molframe_core::diagnostic::{Diagnostic, Findings};
use molframe_core::io::{InputBuffer, InputDigest, ReadOptions};
use std::path::Path;
pub fn read_with_options_in(
path: impl AsRef<Path>,
options: &ReadOptions,
context: &ExecutionContext,
) -> Result<(Structure, Vec<Diagnostic>), Findings> {
let path = path.as_ref();
let input = InputBuffer::open(path, options.limits).map_err(Findings::from)?;
let name = path.file_name().and_then(|name| name.to_str());
read_buffer_in(&input, name, options, context)
}
pub fn read_bytes_in(
bytes: Vec<u8>,
name: Option<&str>,
options: &ReadOptions,
context: &ExecutionContext,
) -> Result<(Structure, Vec<Diagnostic>), Findings> {
read_buffer_in(&InputBuffer::from_bytes(bytes), name, options, context)
}
pub fn read_buffer_in(
input: &InputBuffer,
name: Option<&str>,
options: &ReadOptions,
context: &ExecutionContext,
) -> Result<(Structure, Vec<Diagnostic>), Findings> {
enrich_read(dispatch_read(input, name, options), options, context).map(
|(structure, diagnostics)| {
let digest = matches!(options.input_digest, InputDigest::Sha256)
.then(|| ContentDigest::of(input.as_bytes()));
let structure = structure.with_input(name.map(Into::into), digest);
(structure.into(), diagnostics)
},
)
}