use crate::artifact::{ArtifactError, StageArtifact};
pub trait PersistedStack: Sized {
const STAGE_TAGS: &'static [u16];
fn encode_stages(
&self,
input_schema: [u8; 32],
transformed_schema: [u8; 32],
) -> Result<Vec<Vec<u8>>, ArtifactError>;
fn decode_stages(
components: &[&[u8]],
input_schema: [u8; 32],
transformed_schema: [u8; 32],
) -> Result<Self, ArtifactError>;
}
macro_rules! impl_persisted_stack {
($($stage:ident $index:tt),+) => {
impl<$($stage),+> PersistedStack for ($($stage,)+)
where
$($stage: StageArtifact,)+
{
const STAGE_TAGS: &'static [u16] = &[$($stage::STAGE_TAG),+];
fn encode_stages(
&self,
input_schema: [u8; 32],
transformed_schema: [u8; 32],
) -> Result<Vec<Vec<u8>>, ArtifactError> {
Ok(vec![
$(self.$index.to_artifact(input_schema, transformed_schema)?,)+
])
}
fn decode_stages(
components: &[&[u8]],
input_schema: [u8; 32],
transformed_schema: [u8; 32],
) -> Result<Self, ArtifactError> {
if components.len() != Self::STAGE_TAGS.len() {
return Err(ArtifactError::InvalidPayload);
}
let mut remaining = components.iter();
Ok((
$(
$stage::from_artifact(
remaining.next().ok_or(ArtifactError::InvalidPayload)?,
input_schema,
transformed_schema,
)?,
)+
))
}
}
};
}
impl_persisted_stack!(A 0);
impl_persisted_stack!(A 0, B 1);
impl_persisted_stack!(A 0, B 1, C 2);
impl_persisted_stack!(A 0, B 1, C 2, D 3);
impl_persisted_stack!(A 0, B 1, C 2, D 3, E 4);
impl_persisted_stack!(A 0, B 1, C 2, D 3, E 4, F 5);
impl_persisted_stack!(A 0, B 1, C 2, D 3, E 4, F 5, G 6);
impl_persisted_stack!(A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7);
impl_persisted_stack!(A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8);
impl_persisted_stack!(A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9);
impl_persisted_stack!(A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9, K 10);
impl_persisted_stack!(A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9, K 10, L 11);