3636//! # }
3737//! ```
3838
39- use std:: { fmt:: Debug , future:: Future , ops:: Deref , sync:: Arc } ;
39+ use std:: { fmt:: Debug , future:: Future , ops:: Deref , path :: Path , sync:: Arc } ;
4040
4141use iroh:: {
4242 endpoint:: Connection ,
@@ -49,6 +49,7 @@ use tracing::error;
4949use crate :: {
5050 api:: Store ,
5151 provider:: { Event , EventSender } ,
52+ store:: { fs:: FsStore , mem:: MemStore } ,
5253 ticket:: BlobTicket ,
5354 HashAndFormat ,
5455} ;
@@ -66,6 +67,32 @@ pub struct BlobsProtocol {
6667 pub ( crate ) inner : Arc < BlobsInner > ,
6768}
6869
70+ /// A builder for the blobs protocol handler.
71+ pub struct BlobsProtocolBuilder {
72+ pub store : Store ,
73+ pub events : Option < mpsc:: Sender < Event > > ,
74+ }
75+
76+ impl BlobsProtocolBuilder {
77+ fn new ( store : Store ) -> Self {
78+ Self {
79+ store,
80+ events : None ,
81+ }
82+ }
83+
84+ /// Set provider events.
85+ pub fn events ( mut self , events : mpsc:: Sender < Event > ) -> Self {
86+ self . events = Some ( events) ;
87+ self
88+ }
89+
90+ /// Build the blobs protocol handler.
91+ pub fn build ( self , endpoint : & Endpoint ) -> BlobsProtocol {
92+ BlobsProtocol :: new ( & self . store , endpoint. clone ( ) , self . events )
93+ }
94+ }
95+
6996impl Deref for BlobsProtocol {
7097 type Target = Store ;
7198
@@ -85,6 +112,22 @@ impl BlobsProtocol {
85112 }
86113 }
87114
115+ /// Create a new Blobs protocol handler builder, given a store.
116+ pub fn builder ( store : & Store ) -> BlobsProtocolBuilder {
117+ BlobsProtocolBuilder :: new ( store. clone ( ) )
118+ }
119+
120+ /// Create a new memory-backed Blobs protocol handler.
121+ pub fn memory ( ) -> BlobsProtocolBuilder {
122+ Self :: builder ( & MemStore :: new ( ) )
123+ }
124+
125+ /// Load a persistent Blobs protocol handler from a path.
126+ pub async fn persistent ( path : impl AsRef < Path > ) -> anyhow:: Result < BlobsProtocolBuilder > {
127+ let store = FsStore :: load ( path) . await ?;
128+ Ok ( Self :: builder ( & store) )
129+ }
130+
88131 pub fn store ( & self ) -> & Store {
89132 & self . inner . store
90133 }
0 commit comments