use super::PhysicalPlan;
#[derive(Debug, thiserror::Error)]
pub enum WireError {
#[error("{0}")]
InvalidPlan(&'static str),
#[error("plan codec: {0}")]
Codec(String),
}
pub fn encode(plan: &PhysicalPlan) -> Result<Vec<u8>, WireError> {
if matches!(plan, PhysicalPlan::ClusterArray(_)) {
return Err(WireError::InvalidPlan(
"ClusterArray plans must not be sent over the wire",
));
}
zerompk::to_msgpack_vec(plan).map_err(|e| WireError::Codec(format!("encode: {e}")))
}
pub fn decode(bytes: &[u8]) -> Result<PhysicalPlan, WireError> {
zerompk::from_msgpack(bytes).map_err(|e| WireError::Codec(format!("decode: {e}")))
}
pub fn encode_batch(plans: &Vec<PhysicalPlan>) -> Result<Vec<u8>, WireError> {
for plan in plans {
if matches!(plan, PhysicalPlan::ClusterArray(_)) {
return Err(WireError::InvalidPlan(
"ClusterArray plans must not be shipped via the sequencer",
));
}
}
zerompk::to_msgpack_vec(plans).map_err(|e| WireError::Codec(format!("batch encode: {e}")))
}
pub fn decode_batch(bytes: &[u8]) -> Result<Vec<PhysicalPlan>, WireError> {
zerompk::from_msgpack(bytes).map_err(|e| WireError::Codec(format!("batch decode: {e}")))
}