use toasty_core::{Error, stmt};
pub(crate) enum LazySlot<T> {
Unloaded,
Loaded(T),
}
pub(crate) fn loaded_expr(value: stmt::Expr) -> stmt::Expr {
stmt::Expr::record([value])
}
pub(crate) fn decode<T>(
value: stmt::Value,
label: &'static str,
load: impl FnOnce(stmt::Value) -> crate::Result<T>,
) -> crate::Result<LazySlot<T>> {
match value {
stmt::Value::Null => Ok(LazySlot::Unloaded),
stmt::Value::Record(record) if record.fields.len() == 1 => {
let mut iter = record.fields.into_iter();
Ok(LazySlot::Loaded(load(iter.next().unwrap())?))
}
value => Err(Error::from_args(format_args!(
"{label} decoder expected Null or single-field Record, got {value:?}"
))),
}
}