use std::io::BufReader;
use std::{fs::File, io::Read, path::Path};
pub trait ResourceReader {
type Resource: Read;
type Error: std::error::Error + Send + Sync + 'static;
fn read_from(&mut self, path: &Path) -> std::result::Result<Self::Resource, Self::Error>;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FilesystemResourceReader;
impl FilesystemResourceReader {
pub fn new() -> Self {
Self
}
}
impl ResourceReader for FilesystemResourceReader {
type Resource = BufReader<File>;
type Error = std::io::Error;
fn read_from(&mut self, path: &Path) -> std::result::Result<Self::Resource, Self::Error> {
let file = File::open(path)?;
Ok(BufReader::new(file))
}
}
impl<T, R, E> ResourceReader for T
where
T: for<'a> Fn(&'a Path) -> Result<R, E>,
R: Read,
E: std::error::Error + Send + Sync + 'static,
{
type Resource = R;
type Error = E;
fn read_from(&mut self, path: &Path) -> std::result::Result<Self::Resource, Self::Error> {
self(path)
}
}