use alloc::string::String;
use crate::{error::TemplateError, value::Value};
#[inline]
pub(super) fn render_value_into(val: &Value, output: &mut String) -> Result<(), TemplateError> {
match val {
Value::Str(s) => output.push_str(s),
Value::Bool(true) => output.push_str(crate::consts::LIT_TRUE),
Value::Bool(false) => output.push_str(crate::consts::LIT_FALSE),
Value::Int(i) => {
let mut buf = itoa::Buffer::new();
output.push_str(buf.format(*i));
}
Value::Float(f) => {
use core::fmt::Write;
if *f == 0.0 {
output.push('0');
} else {
write!(output, "{f}").expect("fmt::Write for String is infallible");
}
}
Value::None => { }
Value::List(_) | Value::Struct(_) | Value::Tmpl(_) => {
return Err(TemplateError::syntax(alloc::format!(
"cannot display value of type '{}'",
val.type_name()
)));
}
}
Ok(())
}