exemplify_lib/layers/implementations/
file_reader_factory.rs

1use std::fs::File;
2
3use std::path::Path;
4
5
6
7
8
9
10use crate::layers::domain::reader_factory::{ReaderFactory, ReaderContext};
11
12pub struct FileReaderFactory {}
13
14impl ReaderFactory<File> for FileReaderFactory {
15    fn make_reader(&self, name: String) -> Result<ReaderContext<File>, String> {
16        let file_path = Path::new(&name);
17
18        if !file_path.is_file() {
19            return Err(format!("{} is not a file", name).into());
20        }
21
22        Ok(ReaderContext {
23            source_name: name.clone(),
24            reader: std::fs::File::open(file_path).map_err(|e| e.to_string())?
25        })
26    }
27}