24 lines
661 B
Rust
24 lines
661 B
Rust
use std::fmt::Debug;
|
|
use std::path::PathBuf;
|
|
|
|
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 {
|
|
fn read_file(&self, path: &str) -> Result<String, std::io::Error> {
|
|
let final_path = self
|
|
.working_directory
|
|
.as_ref()
|
|
.map(PathBuf::as_path)
|
|
.map(|pb| pb.join(path))
|
|
.unwrap_or_else(|| PathBuf::from(path));
|
|
Ok(std::fs::read_to_string(final_path)?)
|
|
}
|
|
}
|