use serde_json::value::Value as Json;
use crate::context::Context;
use crate::error::RenderError;
use crate::helpers::HelperDef;
use crate::json::value::ScopedJson;
use crate::registry::Registry;
use crate::render::{Helper, RenderContext};
#[derive(Clone, Copy)]
pub struct ReadFileHelper;
impl HelperDef for ReadFileHelper {
fn call_inner<'reg: 'rc, 'rc>(
&self,
h: &Helper<'rc>,
_: &'reg Registry<'reg>,
_: &'rc Context,
_: &mut RenderContext<'reg, 'rc>,
) -> Result<ScopedJson<'rc>, RenderError> {
let path = h
.param(0)
.ok_or_else(|| RenderError::new("Param not found for helper \"read_file\": path"))?
.value()
.as_str()
.ok_or_else(|| {
RenderError::new("Param invalid for helper \"read_file\": path must be string")
})?;
match std::fs::read_to_string(path) {
Err(err) => Err(RenderError::new(format!(
"Failed to read file at helper \"read_file\": {}",
err
))),
Ok(content) => Ok(Json::String(content).into()),
}
}
}
pub static READ_FILE_HELPER: ReadFileHelper = ReadFileHelper;