1use std::{
4 collections::BTreeMap,
5 io::ErrorKind,
6 path::{Path, PathBuf},
7};
8
9pub trait Source {
11 fn read(&self, path: &Path) -> Result<Option<String>, String>;
16}
17
18#[derive(Debug, Clone, Copy, Default)]
20pub struct DiskSource;
21
22impl Source for DiskSource {
23 fn read(&self, path: &Path) -> Result<Option<String>, String> {
24 match std::fs::read_to_string(path) {
25 Ok(text) => Ok(Some(text)),
26 Err(error) if error.kind() == ErrorKind::NotFound => Ok(None),
27 Err(error) => Err(format!("{}: {error}", path.display())),
28 }
29 }
30}
31
32#[derive(Debug, Clone, Default)]
35pub struct MapSource {
36 files: BTreeMap<PathBuf, Option<String>>,
37}
38
39impl MapSource {
40 #[must_use]
42 pub fn new(files: BTreeMap<PathBuf, Option<String>>) -> Self {
43 Self { files }
44 }
45}
46
47impl Source for MapSource {
48 fn read(&self, path: &Path) -> Result<Option<String>, String> {
49 self.files
50 .get(path)
51 .cloned()
52 .ok_or_else(|| format!("{}: not recorded in this plan", path.display()))
53 }
54}