From 432e949e39b0ed7e9727d5547a1f6e1ca84295fa Mon Sep 17 00:00:00 2001 From: zhouhansen Date: Wed, 31 Jul 2019 17:28:16 +0800 Subject: [PATCH] remove builder pattern --- src/feed.rs | 42 ++++++++++++++++++------------ src/feed_builder.rs | 62 --------------------------------------------- src/lib.rs | 2 -- tests/compat.rs | 5 +--- tests/feed.rs | 15 +++-------- 5 files changed, 30 insertions(+), 96 deletions(-) delete mode 100644 src/feed_builder.rs diff --git a/src/feed.rs b/src/feed.rs index ecc2cf9..0f5683a 100644 --- a/src/feed.rs +++ b/src/feed.rs @@ -1,6 +1,5 @@ //! Hypercore's main abstraction. Exposes an append-only, secure log structure. -use crate::feed_builder::FeedBuilder; use crate::replicate::{Message, Peer}; pub use crate::storage::{Node, NodeTrait, Storage, Store}; @@ -50,20 +49,38 @@ impl Feed where T: RandomAccess + Debug, { + /// create a new feed + pub fn new(public_key: PublicKey, storage: Storage) -> Self { + Feed { + merkle: Merkle::new(), + byte_length: 0, + length: 0, + bitfield: Bitfield::default(), + tree: TreeIndex::default(), + public_key, + secret_key: None, + storage, + peers: vec![], + } + } + + /// Set the secret key. + pub fn set_secret_key(mut self, secret_key: SecretKey) -> Self { + self.secret_key = Some(secret_key); + self + } + /// Create a new instance with a custom storage backend. pub fn with_storage(mut storage: crate::storage::Storage) -> Result { match storage.read_partial_keypair() { Some(partial_keypair) => { - let builder = FeedBuilder::new(partial_keypair.public, storage); + let mut feed = Feed::new(partial_keypair.public, storage); - // return early without secret key - if partial_keypair.secret.is_none() { - return Ok(builder.build()?); + if partial_keypair.secret.is_some() { + feed = feed.set_secret_key(partial_keypair.secret.unwrap()); } - Ok(builder - .secret_key(partial_keypair.secret.unwrap()) - .build()?) + Ok(feed) } None => { // we have no keys, generate a pair and save them to the storage @@ -71,18 +88,11 @@ where storage.write_public_key(&keypair.public)?; storage.write_secret_key(&keypair.secret)?; - Ok(FeedBuilder::new(keypair.public, storage) - .secret_key(keypair.secret) - .build()?) + Ok(Feed::new(keypair.public, storage).set_secret_key(keypair.secret)) } } } - /// Starts a `FeedBuilder` with the provided `Keypair` and `Storage`. - pub fn builder(public_key: PublicKey, storage: Storage) -> FeedBuilder { - FeedBuilder::new(public_key, storage) - } - /// Get the amount of entries in the feed. #[inline] pub fn len(&self) -> usize { diff --git a/src/feed_builder.rs b/src/feed_builder.rs deleted file mode 100644 index e0a30b5..0000000 --- a/src/feed_builder.rs +++ /dev/null @@ -1,62 +0,0 @@ -use ed25519_dalek::{PublicKey, SecretKey}; - -use crate::bitfield::Bitfield; -use crate::crypto::Merkle; -use crate::storage::Storage; -use failure::Error; -use random_access_storage::RandomAccess; -use std::fmt::Debug; -use tree_index::TreeIndex; - -use crate::Feed; -use crate::Result; - -/// Construct a new `Feed` instance. -// TODO: make this an actual builder pattern. -// https://deterministic.space/elegant-apis-in-rust.html#builder-pattern -#[derive(Debug)] -pub struct FeedBuilder -where - T: RandomAccess + Debug, -{ - storage: Storage, - public_key: PublicKey, - secret_key: Option, -} - -impl FeedBuilder -where - T: RandomAccess + Debug, -{ - /// Create a new instance. - #[inline] - pub fn new(public_key: PublicKey, storage: Storage) -> Self { - Self { - storage, - public_key, - secret_key: None, - } - } - - /// Set the secret key. - pub fn secret_key(mut self, secret_key: SecretKey) -> Self { - self.secret_key = Some(secret_key); - self - } - - /// Finalize the builder. - #[inline] - pub fn build(self) -> Result> { - Ok(Feed { - merkle: Merkle::new(), - byte_length: 0, - length: 0, - bitfield: Bitfield::default(), - tree: TreeIndex::default(), - public_key: self.public_key, - secret_key: self.secret_key, - storage: self.storage, - peers: vec![], - }) - } -} diff --git a/src/lib.rs b/src/lib.rs index 4a9d7de..9099397 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,7 +37,6 @@ mod audit; mod crypto; mod event; mod feed; -mod feed_builder; mod proof; mod replicate; mod storage; @@ -45,7 +44,6 @@ mod storage; pub use crate::crypto::{generate_keypair, sign, verify, Signature}; pub use crate::event::Event; pub use crate::feed::Feed; -pub use crate::feed_builder::FeedBuilder; pub use crate::proof::Proof; pub use crate::replicate::Peer; pub use crate::storage::{Node, NodeTrait, Storage, Store}; diff --git a/tests/compat.rs b/tests/compat.rs index 8bc9102..5ac1396 100644 --- a/tests/compat.rs +++ b/tests/compat.rs @@ -79,10 +79,7 @@ fn deterministic_signatures() { for _ in 0..5 { let (dir, storage) = mk_storage(); let keypair = mk_keypair(&keypair_bytes, &key); - let mut feed = Feed::builder(keypair.public, storage) - .secret_key(keypair.secret) - .build() - .unwrap(); + let mut feed = Feed::new(keypair.public, storage).set_secret_key(keypair.secret); let data = b"abc"; for &b in data { diff --git a/tests/feed.rs b/tests/feed.rs index 6c37b30..864edc3 100644 --- a/tests/feed.rs +++ b/tests/feed.rs @@ -18,10 +18,7 @@ use std::io::Write; fn create_with_key() { let keypair = generate_keypair(); let storage = Storage::new_memory().unwrap(); - let _feed = Feed::builder(keypair.public, storage) - .secret_key(keypair.secret) - .build() - .unwrap(); + let _feed = Feed::new(keypair.public, storage); } #[test] @@ -90,10 +87,7 @@ fn verify() { let (public, secret) = copy_keys(&feed); let feed_bytes = secret.to_bytes().to_vec(); let storage = Storage::new(|_| Ok(ram::RandomAccessMemory::new(50))).unwrap(); - let mut evil_feed = Feed::builder(public, storage) - .secret_key(secret) - .build() - .unwrap(); + let mut evil_feed = Feed::new(public, storage).set_secret_key(secret); let evil_bytes = match &feed.secret_key() { Some(key) => key.to_bytes(), @@ -119,10 +113,7 @@ fn put() { let mut a = create_feed(50).unwrap(); let (public, secret) = copy_keys(&a); let storage = Storage::new(|_| Ok(ram::RandomAccessMemory::new(50))).unwrap(); - let mut b = Feed::builder(public, storage) - .secret_key(secret) - .build() - .unwrap(); + let mut b = Feed::new(public, storage).set_secret_key(secret); for _ in 0..10 { a.append(b"foo").unwrap();