sui_spec/loader.rs
1//! Generic spec loader — thin wrapper around `tatara_lisp::compile_typed`.
2//!
3//! Parses a Lisp source string into a typed [`TataraDomain`] value,
4//! returning either a single value (`load_one`) or all values in
5//! document order (`load_all`). Exists mostly so that call sites
6//! don't have to import `tatara_lisp` directly.
7
8use tatara_lisp::TataraDomain;
9
10use crate::SpecError;
11
12/// Compile the entire `src` and return every top-level form of type `T`.
13///
14/// # Errors
15///
16/// Returns an error if the source fails to read, macroexpand, or
17/// compile under `T`'s schema.
18pub fn load_all<T: TataraDomain>(src: &str) -> Result<Vec<T>, SpecError> {
19 Ok(tatara_lisp::compile_typed::<T>(src)?)
20}
21
22/// Convenience: compile `src` and return the single top-level form
23/// of type `T`, erroring if there are zero forms. Extra forms are
24/// discarded (callers who care should use [`load_all`]).
25///
26/// # Errors
27///
28/// Returns an error if compilation fails or produces no forms.
29pub fn load_one<T: TataraDomain>(src: &str) -> Result<T, SpecError> {
30 let mut forms = load_all::<T>(src)?;
31 forms.pop().ok_or_else(|| SpecError::Load(
32 format!("no `({} ...)` form found in source", T::KEYWORD),
33 ))
34}