use std::fs::File; use std::io::{BufReader, Error as IOError, ErrorKind, Read}; use std::path::PathBuf; pub(crate) trait Source { type Item; type Error; fn load(&self) -> Result; } pub(crate) struct FileSource { path: PathBuf, } impl FileSource { pub(crate) fn new(path: PathBuf) -> FileSource { FileSource { path } } } impl Source for FileSource { type Item = String; type Error = IOError; fn load(&self) -> Result { let mut reader = BufReader::new(File::open(&self.path)?); let mut buf = String::new(); reader.read_to_string(&mut buf)?; Ok(buf) } } pub(crate) struct LazySource Option> { loader: F, } impl Option> LazySource { pub(crate) fn new(loader: F) -> LazySource { LazySource { loader } } } impl Option> Source for LazySource { type Item = String; type Error = IOError; fn load(&self) -> Result { (self.loader)().ok_or(IOError::new(ErrorKind::Other, "Source load error")) } }