Read the setup file into memory.

This commit is contained in:
Tom Alexander 2023-09-04 16:53:02 -04:00
parent a7330e38e4
commit ee02e07717
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
4 changed files with 19 additions and 5 deletions

View File

@ -1,15 +1,14 @@
use std::fmt::Debug;
use std::path::Path;
pub trait FileAccessInterface: Debug {
fn read_file(&self, path: &dyn AsRef<Path>) -> Result<String, Box<dyn std::error::Error>>;
fn read_file(&self, path: &str) -> Result<String, std::io::Error>;
}
#[derive(Debug, Clone)]
pub struct LocalFileAccessInterface;
impl FileAccessInterface for LocalFileAccessInterface {
fn read_file(&self, path: &dyn AsRef<Path>) -> Result<String, Box<dyn std::error::Error>> {
fn read_file(&self, path: &str) -> Result<String, std::io::Error> {
Ok(std::fs::read_to_string(path)?)
}
}

View File

@ -5,13 +5,14 @@ use nom::IResult;
pub type Res<T, U> = IResult<T, U, CustomError<T>>;
// TODO: MyError probably shouldn't be based on the same type as the input type since it's used exclusively with static strings right now.
#[derive(Debug, PartialEq)]
#[derive(Debug)]
pub enum CustomError<I> {
MyError(MyError<I>),
Nom(I, ErrorKind),
IO(std::io::Error),
}
#[derive(Debug, PartialEq)]
#[derive(Debug)]
pub struct MyError<I>(pub I);
impl<I> ParseError<I> for CustomError<I> {
@ -24,3 +25,9 @@ impl<I> ParseError<I> for CustomError<I> {
other
}
}
impl<I> From<std::io::Error> for CustomError<I> {
fn from(value: std::io::Error) -> Self {
CustomError::IO(value)
}
}

View File

@ -33,6 +33,7 @@ use crate::context::ExitMatcherNode;
use crate::context::GlobalSettings;
use crate::context::List;
use crate::context::RefContext;
use crate::error::CustomError;
use crate::error::Res;
use crate::parser::comment::comment;
use crate::parser::element_parser::element;
@ -82,6 +83,12 @@ fn document_org_source<'b, 'g, 'r, 's>(
let setup_file = scan_for_setup_file(input);
if setup_file.is_ok() {
let (_remaining, setup_file) = setup_file.expect("If-statement proves this is okay.");
let setup_file_contents = context
.get_global_settings()
.file_access
.read_file(Into::<&str>::into(setup_file))
.map_err(|err| nom::Err::<CustomError<OrgSource<'_>>>::Failure(err.into()))?;
println!("TODO: Process setup_file: {}", setup_file);
}
let (remaining, document) =

View File

@ -318,6 +318,7 @@ impl<'s> From<CustomError<OrgSource<'s>>> for CustomError<&'s str> {
match value {
CustomError::MyError(err) => CustomError::MyError(err.into()),
CustomError::Nom(input, error_kind) => CustomError::Nom(input.into(), error_kind),
CustomError::IO(err) => CustomError::IO(err),
}
}
}