Introduce a file access interface for reading additional files.

This commit is contained in:
Tom Alexander 2023-09-04 13:00:41 -04:00
parent a8f277efe5
commit da1ce2717d
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 21 additions and 1 deletions

View File

@ -0,0 +1,13 @@
use std::path::Path;
pub trait FileAccessInterface {
fn read_file<P: AsRef<Path>>(&self, path: P) -> Result<String, Box<dyn std::error::Error>>;
}
pub struct LocalFileAccessInterface;
impl FileAccessInterface for LocalFileAccessInterface {
fn read_file<P: AsRef<Path>>(&self, path: P) -> Result<String, Box<dyn std::error::Error>> {
Ok(std::fs::read_to_string(path)?)
}
}

View File

@ -1,16 +1,20 @@
use super::FileAccessInterface;
use super::LocalFileAccessInterface;
use crate::types::Object;
// TODO: Ultimately, I think we'll need most of this: https://orgmode.org/manual/In_002dbuffer-Settings.html
#[derive(Debug, Clone)]
pub struct GlobalSettings<'g, 's> {
pub struct GlobalSettings<'g, 's, F: FileAccessInterface> {
pub radio_targets: Vec<&'g Vec<Object<'s>>>,
pub file_access: F,
}
impl<'g, 's> GlobalSettings<'g, 's> {
pub fn new() -> Self {
GlobalSettings {
radio_targets: Vec::new(),
file_access: LocalFileAccessInterface,
}
}
}

View File

@ -2,6 +2,7 @@ use crate::error::Res;
use crate::parser::OrgSource;
mod exiting;
mod file_access_interface;
mod global_settings;
mod list;
mod parser_context;
@ -18,6 +19,8 @@ pub trait Matcher = for<'s> Fn(OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s
pub type DynMatcher<'c> = dyn Matcher + 'c;
pub use exiting::ExitClass;
pub use file_access_interface::FileAccessInterface;
pub use file_access_interface::LocalFileAccessInterface;
pub use global_settings::GlobalSettings;
pub use list::List;
pub use parser_context::Context;