From 6fa869d894a29824425f5efd7234b0533a9becf2 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Mon, 31 Oct 2016 23:50:49 +0300 Subject: [PATCH] block hash builder --- test-data/src/block.rs | 43 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) 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,