use std::io::{Error, ErrorKind};
#[cfg(feature = "fs")]
use serde_json::Value;
#[inline]
pub(crate) fn to_io_err(error: impl std::fmt::Display) -> Error {
Error::new(ErrorKind::InvalidData, error.to_string())
}
#[inline]
#[cfg(feature = "fs")]
pub(crate) fn render_with_field_swap<F>(
ctx: &mut handlebars::Context,
key: &str,
value: Value,
render: F,
) -> Result<String, Error>
where
F: FnOnce(&handlebars::Context) -> Result<String, Error>,
{
let obj = ctx
.data_mut()
.as_object_mut()
.expect("context should be an object");
let original = obj.insert(key.to_owned(), value);
let result = render(ctx);
let obj = ctx.data_mut().as_object_mut().unwrap();
match original {
Some(v) => {
obj.insert(key.to_owned(), v);
}
None => {
obj.remove(key);
}
}
result
}