diff --git a/test-data/src/block.rs b/test-data/src/block.rs index 89377770..f22a167a 100644 --- a/test-data/src/block.rs +++ b/test-data/src/block.rs @@ -1,10 +1,51 @@ //! Block builder -use chain; +use chain::{self, RepresentH256}; use primitives::hash::H256; use primitives::bytes::Bytes; use invoke::{Invoke, Identity}; +pub struct BlockHashBuilder { + callback: F, + block: Option, +} + +impl BlockHashBuilder where F: Invoke<(H256, chain::Block)> { + pub fn with_callback(callback: F) -> Self { + BlockHashBuilder { + block: None, + callback: callback, + } + } + + pub fn block(self) -> BlockBuilder { + BlockBuilder::with_callback(self) + } + + pub fn with_block(mut self, block: chain::Block) -> Self { + self.block = Some(block); + self + } + + pub fn build(self) -> F::Result { + let block = self.block.expect("Block is supposed to be build here to get hash"); + self.callback.invoke(( + block.hash(), + block + )) + } +} + +impl Invoke for BlockHashBuilder + where F: Invoke<(H256, chain::Block)> +{ + type Result = Self; + + fn invoke(self, block: chain::Block) -> Self { + self.with_block(block) + } +} + pub struct BlockBuilder { callback: F, header: Option,