use super::ast::{Module, Node};
use crate::common::error::LoadError;
use crate::common::limits::{MAX_BLOB_BYTES, MAX_TREE_DEPTH};
const SIGNATURE: [u8; 4] = *b"TMPL";
pub const BLOB_VERSION: u32 = 1;
pub(crate) fn encode(module: &Module) -> Vec<u8> {
let mut out = Vec::new();
out.extend_from_slice(&SIGNATURE);
out.extend_from_slice(&BLOB_VERSION.to_le_bytes());
ciborium::ser::into_writer(module, &mut out)
.expect("encoding a compiled module to CBOR cannot fail");
out
}
pub(crate) fn decode(bytes: &[u8]) -> Result<Module, LoadError> {
if bytes.len() > MAX_BLOB_BYTES {
return Err(LoadError::Corrupt("blob exceeds maximum size".into()));
}
let header = bytes
.get(..8)
.ok_or_else(|| LoadError::Corrupt("blob too short".into()))?;
if header[..4] != SIGNATURE {
return Err(LoadError::Corrupt("not a Temple blob".into()));
}
let version = u32::from_le_bytes([header[4], header[5], header[6], header[7]]);
if version != BLOB_VERSION {
return Err(LoadError::IncompatibleVersion {
found: version,
expected: BLOB_VERSION,
});
}
let module: Module = ciborium::de::from_reader(&bytes[8..])
.map_err(|e| LoadError::Corrupt(format!("malformed blob: {e}")))?;
if too_deep(&module) {
return Err(LoadError::Corrupt("blob nests too deeply".into()));
}
Ok(module)
}
fn too_deep(module: &Module) -> bool {
let lets = module.lets.iter().map(|binding| Node::Expr(&binding.expr));
let mut stack: Vec<(Node, usize)> = lets
.chain([Node::Out(&module.output)])
.map(|node| (node, 1))
.collect();
while let Some((node, depth)) = stack.pop() {
if depth > MAX_TREE_DEPTH {
return true;
}
node.for_each_child(&mut |child| stack.push((child, depth + 1)));
}
false
}