@@ -7,11 +7,10 @@ use futures::stream::StreamExt;
77use  ipld_core:: cid:: Cid ; 
88use  tokio:: io:: { AsyncRead ,  AsyncSeek ,  AsyncSeekExt ,  AsyncWrite } ; 
99
10- use  super :: { DEFAULT_BLOCK_SIZE ,  DEFAULT_TREE_WIDTH } ; 
1110use  crate :: { 
1211    unixfs:: stream_balanced_tree, 
1312    v1:: { self } , 
14-     v2,  BlockMetadata ,  Error ,  Index ,  IndexEntry ,  IndexSorted ,  SingleWidthIndex , 
13+     v2,  BlockMetadata ,  Config ,   Error ,  Index ,  IndexEntry ,  IndexSorted ,  SingleWidthIndex , 
1514} ; 
1615
1716/// CAR file writer. 
@@ -20,6 +19,7 @@ pub struct Blockwriter<W> {
2019    index :  HashMap < Cid ,  BlockMetadata > , 
2120    roots :  Vec < Cid > , 
2221    started :  bool , 
22+     config :  Config , 
2323} 
2424
2525impl < W >  Blockwriter < W >  { 
@@ -30,6 +30,7 @@ impl<W> Blockwriter<W> {
3030            index :  HashMap :: new ( ) , 
3131            roots :  Vec :: with_capacity ( 1 ) , 
3232            started :  false , 
33+             config :  Default :: default ( ) , 
3334        } 
3435    } 
3536
@@ -66,6 +67,7 @@ impl Blockwriter<Cursor<Vec<u8>>> {
6667            index :  HashMap :: new ( ) , 
6768            roots :  Vec :: with_capacity ( 1 ) , 
6869            started :  false , 
70+             config :  Default :: default ( ) , 
6971        } 
7072    } 
7173} 
@@ -74,6 +76,19 @@ impl<W> Blockwriter<W>
7476where 
7577    W :  AsyncWrite  + AsyncSeek  + Unpin , 
7678{ 
79+     /// Convert `source` into a CAR file, writing it to `writer`. 
80+ /// Returns the root [`Cid`]. 
81+ pub  async  fn  import < S > ( source :  S ,  writer :  W )  -> Result < Cid ,  Error > 
82+     where 
83+         S :  AsyncRead  + Unpin , 
84+     { 
85+         let  mut  writer = Self :: new ( writer) ; 
86+         writer. write_from ( source) . await ?; 
87+         let  root = * writer. roots . first ( ) . ok_or ( Error :: EmptyRootsError ) ?; 
88+         writer. finish ( ) . await ?; 
89+         Ok ( root) 
90+     } 
91+ 
7792    /// Writes the contents from `source`, adding a new root to [`Blockwriter`]. 
7893pub  async  fn  write_from < S > ( & mut  self ,  source :  S )  -> Result < ( ) ,  Error > 
7994    where 
@@ -87,8 +102,15 @@ where
87102
88103        let  mut  current_position = self . writer . get_inner_mut ( ) . stream_position ( ) . await ?; 
89104
90-         let  chunker = crate :: chunker:: byte_stream_chunker ( source,  DEFAULT_BLOCK_SIZE ) ; 
91-         let  nodes = stream_balanced_tree ( chunker,  DEFAULT_TREE_WIDTH ) . peekable ( ) ; 
105+         let  nodes = match  self . config  { 
106+             Config :: Balanced  { 
107+                 chunk_size, 
108+                 tree_width, 
109+             }  => { 
110+                 let  chunker = crate :: chunker:: byte_stream_chunker ( source,  chunk_size) ; 
111+                 stream_balanced_tree ( chunker,  tree_width) . peekable ( ) 
112+             } 
113+         } ; 
92114        tokio:: pin!( nodes) ; 
93115
94116        let  mut  root = None ; 
0 commit comments