use crate::ir::{BufferDecl, Expr, Node, Program};
pub mod decode;
pub mod encode;
pub mod framing;
pub mod tags;
pub const MAX_BUFFERS: usize = 16_384;
pub const MAX_NODES: usize = 1_000_000;
pub const MAX_ARGS: usize = 4_096;
pub const MAX_STRING_LEN: usize = 1 << 20;
pub const MAX_DECODE_DEPTH: u32 = 256;
pub(crate) struct Reader<'a> {
pub bytes: &'a [u8],
pub pos: usize,
pub depth: u32,
}
impl Program {
#[inline]
pub fn to_wire(&self) -> Result<Vec<u8>, crate::error::Error> {
encode::to_wire(self).map_err(wire_err)
}
#[must_use]
#[inline]
pub fn to_bytes(&self) -> Vec<u8> {
match self.to_wire() {
Ok(bytes) => bytes,
Err(e) => {
tracing::error!(
error = %e,
"Program::to_bytes: wire encoding failed; returning empty bytes. \
This indicates a malformed Program; callers requiring strict \
encoding must use Program::to_wire directly."
);
Vec::new()
}
}
}
#[inline]
pub fn from_wire(bytes: &[u8]) -> Result<Self, crate::error::Error> {
decode::from_wire(bytes).map_err(wire_err)
}
#[inline]
pub fn from_bytes(bytes: &[u8]) -> Result<Self, crate::error::Error> {
Self::from_wire(bytes)
}
}
fn wire_err(message: String) -> crate::error::Error {
crate::error::Error::WireFormatValidation { message }
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ir::{BufferAccess, BufferDecl, DataType, Program};
#[test]
#[inline]
pub(crate) fn to_bytes_returns_empty_on_wire_error() {
let long_name = "x".repeat(MAX_STRING_LEN + 1);
let program = Program::new(
vec![BufferDecl::storage(
&long_name,
0,
BufferAccess::ReadOnly,
DataType::U32,
)],
[1, 1, 1],
vec![],
);
assert!(program.to_wire().is_err());
assert!(program.to_bytes().is_empty());
}
}