diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2019-06-16 14:24:13 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2020-03-25 10:50:14 +0100 |
commit | fc3243918fee9450f6be8e9a436c277718bb5cb8 (patch) | |
tree | 912dc9bd5e2bdaec95008924419932732baf51f6 | |
parent | c1a7b40f9610c552d6f6d0d51503d3011fd85841 (diff) |
Add extension type for Block
-rw-r--r-- | src/main.rs | 1 | ||||
-rw-r--r-- | src/typeext/block.rs | 40 | ||||
-rw-r--r-- | src/typeext/mod.rs | 1 |
3 files changed, 42 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs index a9b8f8c..faf5d6d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,6 +23,7 @@ mod cli_ui; mod configuration; mod repository; mod state; +mod typeext; mod types; mod version; diff --git a/src/typeext/block.rs b/src/typeext/block.rs new file mode 100644 index 0000000..e899082 --- /dev/null +++ b/src/typeext/block.rs @@ -0,0 +1,40 @@ +use failure::Error; +use futures::Future; +use futures::stream; +use futures::stream::Stream; + +use crate::types::block::Block; +use crate::repository::Repository; + +/// Wrapper for Block type which holds a reference to the repository and is thus able to provide +/// convenient functionality out of the box +pub struct BlockExt { + block: Block, + repo: Repository, +} + +impl Into<Block> for BlockExt { + fn into(self) -> Block { + self.block + } +} + +impl BlockExt { + pub fn from_block(block: Block, repo: Repository) -> Self { + BlockExt { block, repo } + } + + pub fn parents(&self) -> impl Stream<Item = BlockExt, Error = Error> { + let parents = self.block.parents().clone(); + let repo = self.repo.clone(); + + stream::unfold(parents, move |mut state| { + let repo = repo.clone(); // dont understand why this is necessary + state.pop().map(move |hash| { + repo.get_block(hash).map(move |block| { + (BlockExt::from_block(block, repo.clone()), state) + }) + }) + }) + } +} diff --git a/src/typeext/mod.rs b/src/typeext/mod.rs new file mode 100644 index 0000000..a863eaa --- /dev/null +++ b/src/typeext/mod.rs @@ -0,0 +1 @@ +pub mod block; |