source_text/
process.rs

1use crate::{LoadSource, Source};
2
3/// Process any [Source] with a callback, annotating the error with the source's origin `name`.
4pub fn process_source<S, F, R>(source: S, f: F) -> anyhow::Result<R>
5where
6    S: LoadSource,
7    F: FnOnce(&Source) -> anyhow::Result<R>,
8{
9    use anyhow::Context;
10
11    let srcval = source.load()?;
12    let name = srcval.name();
13    f(&srcval).with_context(|| format!("Error in {}:", name))
14}
15
16/// Process any text `&str` with a callback, annotating the error with the source's origin `name`.
17pub fn process_text<S, F, R>(source: S, f: F) -> anyhow::Result<R>
18where
19    S: LoadSource,
20    F: FnOnce(&str) -> anyhow::Result<R>,
21{
22    process_source(source, |s: &Source| f(s.text()))
23}