Enable API's that consume &str
to retrieve the text string from any LoadSource
source while tracking the origin.
Terminology
This crate uses some terminology to help in disambiguation:
- "text" - an
&str
to be "processed" by application code; in contrast to&str
used for other purposes such as naming the origin of a "text" or used in error messages. - "source" - a [Source] value which tracks both a
text
and thename
of its origin.
Example: [Parsable]
Suppose we have a config file parser which parses a configuration into a Config
struct. We
want to be able to either parse strings in memory or load and parse config files from disk,
with error messages indicating the source:
use indoc; // For cleanly formatting test assertions.
use Parsable;
let err1 = parse_source.err.unwrap;
assert_eq!;
let configpath = new;
let err2 = parse_source.err.unwrap;
assert_eq!;
Example: process_text
If [Parsable] does not fit your usage, you can wrap any fn(&str) -> anyhow::Result<T>
with
[process_text]:
use indoc; // For cleanly formatting test assertions.