organic/src/context/file_access_interface.rs

29 lines
884 B
Rust
Raw Normal View History

use std::fmt::Debug;
use std::path::PathBuf;
#[cfg(any(feature = "compare", feature = "foreign_document_test"))]
2023-10-14 17:48:38 -04:00
pub trait FileAccessInterface: Sync + Debug {
2023-09-04 16:53:02 -04:00
fn read_file(&self, path: &str) -> Result<String, std::io::Error>;
}
#[cfg(not(any(feature = "compare", feature = "foreign_document_test")))]
pub trait FileAccessInterface: Debug {
fn read_file(&self, path: &str) -> Result<String, std::io::Error>;
}
#[derive(Debug, Clone)]
pub struct LocalFileAccessInterface {
pub working_directory: Option<PathBuf>,
}
impl FileAccessInterface for LocalFileAccessInterface {
2023-09-04 16:53:02 -04:00
fn read_file(&self, path: &str) -> Result<String, std::io::Error> {
let final_path = self
.working_directory
2023-10-16 18:29:21 -04:00
.as_deref()
.map(|pb| pb.join(path))
.unwrap_or_else(|| PathBuf::from(path));
2023-10-16 18:54:41 -04:00
std::fs::read_to_string(final_path)
}
}