@@ -215,55 +215,49 @@ impl BunLockfile {
215
215
Some ( ( key, entry) )
216
216
}
217
217
218
- pub fn lockfile ( & self ) -> Result < BunLockfileData , Error > {
219
- Ok ( BunLockfileData {
220
- lockfile_version : self . data . lockfile_version ,
221
- workspaces : self . data . workspaces . clone ( ) ,
222
- packages : self . data . packages . clone ( ) ,
223
- patched_dependencies : self . data . patched_dependencies . clone ( ) ,
224
- } )
218
+ pub fn lockfile ( self ) -> Result < BunLockfileData , Error > {
219
+ Ok ( self . data )
225
220
}
226
221
227
222
fn subgraph (
228
223
& self ,
229
224
workspace_packages : & [ String ] ,
230
225
packages : & [ String ] ,
231
226
) -> Result < BunLockfile , Error > {
232
- let mut new_packages = Map :: new ( ) ;
233
- let mut new_workspaces = Map :: new ( ) ;
234
- let mut new_patched_dependencies = Map :: new ( ) ;
235
-
236
- for package in packages {
237
- let package_key = self
238
- . key_to_entry
239
- . get ( package)
240
- . expect ( "package key should exist" ) ;
241
- if let Some ( ( key, entry) ) = self . package_entry ( & package_key) {
242
- new_packages. insert ( key. to_string ( ) , entry. clone ( ) ) ;
243
- }
244
- }
245
-
246
- // The root workspace is "" in the lockfile
247
- let root_workspace = self
248
- . data
249
- . workspaces
250
- . get ( "" )
251
- . expect ( "root workspace should exist" ) ;
252
- new_workspaces. insert ( "" . to_string ( ) , root_workspace. clone ( ) ) ;
253
-
254
- for workspace in workspace_packages {
255
- let workspace_entry = self . data . workspaces . get ( workspace) ;
256
-
257
- if let Some ( workspace_entry) = workspace_entry {
258
- new_workspaces. insert ( workspace. to_string ( ) , workspace_entry. clone ( ) ) ;
259
- }
260
- }
261
-
262
- for ( key, patch) in self . data . patched_dependencies . iter ( ) {
263
- if packages. contains ( & key) {
264
- new_patched_dependencies. insert ( key. to_string ( ) , patch. to_string ( ) ) ;
265
- }
266
- }
227
+ let new_workspaces = workspace_packages
228
+ . iter ( )
229
+ // Add the root workspace, which is always indexed by ""
230
+ . chain ( std:: iter:: once ( & "" . to_string ( ) ) )
231
+ . map ( |ws| {
232
+ (
233
+ ws. to_string ( ) ,
234
+ self . data
235
+ . workspaces
236
+ . get ( ws)
237
+ . expect ( "workspace should exist" )
238
+ . clone ( ) ,
239
+ )
240
+ } )
241
+ . collect ( ) ;
242
+
243
+ let new_packages: Map < _ , _ > = packages
244
+ . iter ( )
245
+ . filter_map ( |package| {
246
+ let key = self . key_to_entry . get ( package) ?;
247
+ self . package_entry ( key)
248
+ . map ( |( k, v) | ( k. to_string ( ) , v. clone ( ) ) )
249
+ } )
250
+ . collect ( ) ;
251
+
252
+ let new_patched_dependencies = new_packages
253
+ . values ( )
254
+ . filter_map ( |entry| {
255
+ self . data
256
+ . patched_dependencies
257
+ . get ( & entry. ident )
258
+ . map ( |patch| ( entry. ident . clone ( ) , patch. clone ( ) ) )
259
+ } )
260
+ . collect ( ) ;
267
261
268
262
Ok ( Self {
269
263
data : BunLockfileData {
@@ -378,10 +372,14 @@ mod test {
378
372
. unwrap ( ) ;
379
373
let subgraph_data = subgraph. lockfile ( ) . unwrap ( ) ;
380
374
381
- assert ! ( subgraph_data
382
- . packages
383
- . get( "is-odd" )
384
- . is_some_and
( |pkg| pkg
. ident ==
"[email protected] " ) ) ;
375
+ assert_eq ! (
376
+ subgraph_data
377
+ . packages
378
+ . iter( )
379
+ . map( |( key, pkg) | ( key. as_str( ) , pkg. ident. as_str( ) ) )
380
+ . collect:: <Vec <_>>( ) ,
381
+ vec!
[ ( "is-odd" , "[email protected] " ) ]
382
+ ) ;
385
383
assert_eq ! (
386
384
subgraph_data. workspaces. keys( ) . collect:: <Vec <_>>( ) ,
387
385
vec![ "" , "apps/docs" ]
@@ -396,10 +394,14 @@ mod test {
396
394
. unwrap ( ) ;
397
395
let subgraph_a_data = subgraph_a. lockfile ( ) . unwrap ( ) ;
398
396
399
- assert ! ( subgraph_a_data
400
- . packages
401
- . get( "is-odd" )
402
- . is_some_and
( |pkg| pkg
. ident ==
"[email protected] " ) ) ;
397
+ assert_eq ! (
398
+ subgraph_a_data
399
+ . packages
400
+ . iter( )
401
+ . map( |( key, pkg) | ( key. as_str( ) , pkg. ident. as_str( ) ) )
402
+ . collect:: <Vec <_>>( ) ,
403
+ vec!
[ ( "is-odd" , "[email protected] " ) ]
404
+ ) ;
403
405
assert_eq ! (
404
406
subgraph_a_data. workspaces. keys( ) . collect:: <Vec <_>>( ) ,
405
407
vec![ "" , "apps/a" ]
@@ -410,10 +412,14 @@ mod test {
410
412
. unwrap ( ) ;
411
413
let subgraph_b_data = subgraph_b. lockfile ( ) . unwrap ( ) ;
412
414
413
- assert ! ( subgraph_b_data
414
- . packages
415
- . get( "b/is-odd" )
416
- . is_some_and
( |pkg| pkg
. ident ==
"[email protected] " ) ) ;
415
+ assert_eq ! (
416
+ subgraph_b_data
417
+ . packages
418
+ . iter( )
419
+ . map( |( key, pkg) | ( key. as_str( ) , pkg. ident. as_str( ) ) )
420
+ . collect:: <Vec <_>>( ) ,
421
+ vec!
[ ( "b/is-odd" , "[email protected] " ) ]
422
+ ) ;
417
423
assert_eq ! (
418
424
subgraph_b_data. workspaces. keys( ) . collect:: <Vec <_>>( ) ,
419
425
vec![ "" , "apps/b" ]
0 commit comments