use super::expr::Expr;
use crate::has_opcode::HasStaticOpCode;
use crate::serialization::op_code::OpCode;
use crate::serialization::sigma_byte_reader::SigmaByteRead;
use crate::serialization::sigma_byte_writer::SigmaByteWrite;
use crate::serialization::SigmaParsingError;
use crate::serialization::SigmaSerializable;
use crate::serialization::SigmaSerializeResult;
use crate::types::stype::SType;
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct BlockValue {
pub items: Vec<Expr>,
pub result: Box<Expr>,
}
impl BlockValue {
pub fn tpe(&self) -> SType {
self.result.tpe()
}
}
impl HasStaticOpCode for BlockValue {
const OP_CODE: OpCode = OpCode::BLOCK_VALUE;
}
impl SigmaSerializable for BlockValue {
fn sigma_serialize<W: SigmaByteWrite>(&self, w: &mut W) -> SigmaSerializeResult {
self.items.sigma_serialize(w)?;
self.result.sigma_serialize(w)
}
fn sigma_parse<R: SigmaByteRead>(r: &mut R) -> Result<Self, SigmaParsingError> {
let items = Vec::<Expr>::sigma_parse(r)?;
let result = Expr::sigma_parse(r)?;
Ok(BlockValue {
items,
result: Box::new(result),
})
}
}
#[cfg(feature = "arbitrary")]
mod arbitrary {
use super::*;
use proptest::collection::vec;
use proptest::prelude::*;
impl Arbitrary for BlockValue {
type Strategy = BoxedStrategy<Self>;
type Parameters = ();
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
(any::<Expr>(), vec(any::<Expr>(), 0..10))
.prop_map(|(result, items)| Self {
items,
result: Box::new(result),
})
.boxed()
}
}
}
#[cfg(test)]
#[allow(clippy::panic)]
pub mod tests {
use crate::mir::block::BlockValue;
use crate::mir::expr::Expr;
use crate::serialization::sigma_serialize_roundtrip;
use proptest::prelude::*;
proptest! {
#[test]
fn ser_roundtrip(block in any::<BlockValue>()) {
let e = Expr::BlockValue(block);
prop_assert_eq![sigma_serialize_roundtrip(&e), e];
}
}
}