source_text/
load.rs

1use crate::Source;
2use std::path::Path;
3
4/// Any type that can load a [Source] synchronously.
5pub trait LoadSource {
6    /// Load this value synchronously into a [Source] value.
7    fn load(&self) -> anyhow::Result<Source>;
8}
9
10impl<'a> LoadSource for &'a str {
11    fn load(&self) -> anyhow::Result<Source> {
12        Ok(Source::new_unnamed(*self))
13    }
14}
15
16impl<'a> LoadSource for &'a Path {
17    fn load(&self) -> anyhow::Result<Source> {
18        use anyhow::Context;
19
20        let text = std::fs::read_to_string(self)
21            .with_context(|| format!("Error in {:?}:", self.display()))?;
22        Ok(Source::new_named(self.display().to_string(), text))
23    }
24}