eval_api.rs provides thin wrappers around Evaluator that cover the common
entry points: evaluating a string, a single file, or a batch of files. These
are the functions used by the binary, the test suite, and external callers.
Design rationale
Shared evaluator across multiple files
eval_files processes each input file through the same Evaluator instance.
This means macro definitions in file N are visible in file N+1 — the intended
behaviour when processing a collection of literate-source fragments that share
a common macro library.
eval_files_with_config creates a fresh evaluator; use it when isolation
between runs is required.
eval_string vs eval_string_with_defaults
eval_string accepts an existing evaluator and an optional real_path for
source attribution (used when the string originated from a file on disk).
eval_string_with_defaults is the simplest entry point — a fresh evaluator,
no path, % as the special char. It is used extensively in tests.
canonical() — resolving paths that do not yet exist
The output file may not exist before the first run. canonical resolves the
parent directory (which must exist) and appends the file name. This keeps the
in == out guard reliable without requiring the output file to be created first.
Input == output guard
eval_file compares canonical input and output paths before evaluating.
Without this guard, weaveback-macro src.adoc src.adoc would silently
overwrite the source with the expanded output.
File structure
// <<@file weaveback-macro/src/evaluator/eval_api.rs>>=
// <<eval api preamble>>
// <<eval string>>
// <<canonical helper>>
// <<eval file>>
// <<eval file with config>>
// <<eval files>>
// <<eval files with config>>
// <<eval string with defaults>>
// @
Preamble
// <<eval api preamble>>=
// crates/weaveback-macro/src/evaluator/eval_api.rs
use fs;
use ;
use Evaluator;
use ;
use EvalConfig;
// @
eval_string
Parses source using evaluator.parse_string and then evaluates the AST.
If real_path is supplied, the evaluator’s current_file is updated so that
%here and error messages reference the correct file path.
// <<eval string>>=
// @
canonical — resolve path through parent when file does not exist
// <<canonical helper>>=
/// Returns the canonical path of `p`, resolving through the parent directory
/// when `p` itself does not yet exist (common for output files).
// @
eval_file
// <<eval file>>=
// @
eval_file_with_config
// <<eval file with config>>=
// @
eval_files
Processes a batch of inputs through the same evaluator, writing each output
to output_dir / input_filename.
// <<eval files>>=
// @
eval_files_with_config
// <<eval files with config>>=
// @
eval_string_with_defaults
The simplest entry point: fresh evaluator, default % special char, no path.
Used extensively in unit tests.
// <<eval string with defaults>>=
// @