use crate::doc_index::DocIndex;
use std::collections::HashMap;
use std::sync::Arc;
use tera::{Tera, Value};
pub fn register(env: &mut Tera, index: Arc<DocIndex>) {
env.register_function(
"all",
move |args: &HashMap<String, Value>| -> tera::Result<Value> {
check_no_args(args)?;
let docs: Vec<&crate::doc::Doc> = index.docs().collect();
tera::to_value(docs).map_err(tera::Error::from)
},
);
}
fn check_no_args(args: &HashMap<String, Value>) -> tera::Result<()> {
if let Some(key) = args.keys().next() {
return Err(tera::Error::msg(format!(
"all: unexpected argument `{}` (all() takes no arguments — define a \
collection to order/limit/filter, or pipe through filters like \
omit_docs, dirtree, or slice)",
key
)));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn check_no_args_accepts_empty() {
assert!(check_no_args(&HashMap::new()).is_ok());
}
#[test]
fn check_no_args_rejects_any_kwarg() {
let mut args = HashMap::new();
args.insert("limit".to_string(), Value::from(5u64));
assert!(check_no_args(&args).is_err());
}
}