willbe 0.34.0

Utility to publish multi-crate and multi-workspace environments and maintain their consistency.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
/// Define a private namespace for all its items.
#[ allow( clippy ::std_instead_of_alloc, clippy ::std_instead_of_core ) ]
mod private
{

  use crate :: *;
  use collection_tools :: collection;

  use std :: { fmt, str };
  use petgraph ::
  {
  prelude :: { Dfs, EdgeRef },
  algo ::toposort,
  visit ::Topo,
  Graph,
 };
  use error ::
  {
  ErrWith,
  untyped :: { Context, format_err },
 };
  use tool :: { TreePrinter, ListNodeReport };

  /// Args for `list` action.
  #[ derive( Debug, Default, Copy, Clone ) ]
  pub enum ListFormat
  {
  /// Tree like format.
  #[ default ]
  Tree,
  /// Topologically sorted list.
  Topological,
 }

  impl str ::FromStr for ListFormat
  {
  type Err = error ::untyped ::Error;

  fn from_str( s: &str ) -> Result< Self, Self ::Err >
  {
   let value = match s
   {
  "tree" => ListFormat ::Tree,
  "toposort" => ListFormat ::Topological,
  e => return Err( error ::untyped ::format_err!( "Unknown format '{e}'. Available values: [tree, toposort]" ) )
 };

   Ok( value )
 }
 }

  /// Enum representing the different dependency categories.
  ///
  /// These categories include :
  /// - `Primary` : This category represents primary dependencies.
  /// - `Dev` : This category represents development dependencies.
  /// - `Build` : This category represents build-time dependencies.
  #[ derive( Debug, Copy, Clone, Hash, Eq, PartialEq ) ]
  pub enum DependencyCategory
  {
  /// Represents the primary dependencies, i.e., libraries or packages that
  /// are required for your code to run. These are typically listed in your
  /// `Cargo.toml`'s `[dependencies]` section.
  Primary,
  /// Represents the development dependencies. These are used for compiling
  /// tests, examples, or benchmarking code. They are not used when compiling
  /// the normal application or library. These are typically listed in your
  /// `Cargo.toml`'s `[dev-dependencies]` section.
  Dev,
  /// Represents build-time dependencies. These are used only to compile
  /// build scripts (`build.rs`) but not for the package code itself. These
  /// are typically listed in your `Cargo.toml`'s `[build-dependencies]` section.
  Build,
 }

  /// Enum representing the source of a dependency.
  ///
  /// This enum has the following values :
  /// * `Local` - Represents a dependency located locally.
  /// * `Remote` - Represents a dependency fetched from a remote source.
  #[ derive( Debug, Copy, Clone, Hash, Eq, PartialEq ) ]
  pub enum DependencySource
  {
  /// Represents a dependency that is located on the local file system.
  Local,
  /// Represents a dependency that is to be fetched from a remote source.
  Remote,
 }

  /// Args for `list` action.
  #[ derive( Debug, Default, Copy, Clone ) ]
  pub enum ListFilter
  {
  /// With all packages.
  #[ default ]
  Nothing,
  /// With local only packages.
  Local,
 }

  impl str ::FromStr for ListFilter
  {
  type Err = error ::untyped ::Error;

  fn from_str( s: &str ) -> Result< Self, Self ::Err >
  {
   let value = match s
   {
  "nothing" => ListFilter ::Nothing,
  "local" => ListFilter ::Local,
  e => return Err( error ::untyped ::format_err!( "Unknown filter '{e}'. Available values: [nothing, local]" ) )
 };

   Ok( value )
 }
 }

  /// Additional information to include in a package report.
  #[ derive( Debug, Copy, Clone, Hash, Eq, PartialEq ) ]
  pub enum PackageAdditionalInfo
  {
  /// Include the version of the package, if possible.
  Version,
  /// Include the path to the package, if it exists.
  Path,
 }

  /// A struct representing the arguments for listing crates.
  ///
  /// This struct is used to pass the necessary arguments for listing crates. It includes the
  /// following fields :
  ///
  /// - `path_to_manifest` : A `CrateDir` representing the path to the manifest of the crates.
  /// - `format` : A `ListFormat` enum representing the desired format of the output.
  /// - `dependency_sources` : A `HashSet` of `DependencySource` representing the sources of the dependencies.
  #[ derive( Debug, former ::Former ) ]
  pub struct ListOptions
  {
  path_to_manifest: CrateDir,
  format: ListFormat,
  info: collection ::HashSet< PackageAdditionalInfo >,
  dependency_sources: collection ::HashSet< DependencySource >,
  dependency_categories: collection ::HashSet< DependencyCategory >,
 }

  // struct Symbols
  // {
  //   down: &'static str,
  //   tee: &'static str,
  //   ell: &'static str,
  //   right: &'static str,
  // }

  // // qqq: for Mykyta: make facade, abstract and move out tree printing. or reuse ready solution for tree printing
  // // stick to single responsibility
  // const UTF8_SYMBOLS: Symbols = Symbols
  // {
  //   down: "│",
  //   tee: "├",
  //   ell: "└",
  //   right: "─",
  // };

  // /// Represents a node in a dependency graph.
  // /// It holds essential information about the project dependencies. It is also capable
  // /// of holding any nested dependencies in a recursive manner, allowing the modeling
  // /// of complex dependency structures.
  // #[ derive( Debug, Clone, Eq, PartialEq ) ]
  // pub struct ListNodeReport
  // {
  //   /// This could be the name of the library or crate.
  //   pub name: String,
  //   /// Ihe version of the crate.
  //   pub version: Option< String >,
  //   /// The path to the node's source files in the local filesystem. This is
  //   /// optional as not all nodes may have a local presence (e.g., nodes representing remote crates).
  //   pub crate_dir: Option< CrateDir >,
  //   /// This field is a flag indicating whether the Node is a duplicate or not.
  //   pub duplicate: bool,
  //   /// A list that stores normal dependencies.
  //   /// Each element in the list is also of the same 'ListNodeReport' type to allow
  //   /// storage of nested dependencies.
  //   pub normal_dependencies: Vec< ListNodeReport >,
  //   /// A list that stores dev dependencies(dependencies required for tests or examples).
  //   /// Each element in the list is also of the same 'ListNodeReport' type to allow
  //   /// storage of nested dependencies.
  //   pub dev_dependencies: Vec< ListNodeReport >,
  //   /// A list that stores build dependencies.
  //   /// Each element in the list is also of the same 'ListNodeReport' type to allow
  //   /// storage of nested dependencies.
  //   pub build_dependencies: Vec< ListNodeReport >,
  // }

  // impl ListNodeReport
  // {
  //   /// Displays the name, version, path, and dependencies of a package with appropriate indentation and spacing.
  //   ///
  //   /// # Arguments
  //   ///
  //   /// * `spacer` - A string used for indentation.
  //   ///
  //   /// # Returns
  //   ///
  //   /// * A `Result` containing the formatted string or a `std ::fmt ::Error` if formatting fails.
  //   pub fn display_with_spacer( &self, spacer: &str ) -> Result< String, std ::fmt ::Error >
  //   {
  //     let mut f = String ::new();

  //     write!( f, "{}", self.name )?;
  //     if let Some( version ) = &self.version { write!( f, " {version}" )? }
  //     if let Some( crate_dir ) = &self.crate_dir { write!( f, " {}", crate_dir )? }
  //     if self.duplicate { write!( f, "(*)" )? }
  //     write!( f, "\n" )?;

  //     let mut new_spacer = format!( "{spacer}{}  ", if self.normal_dependencies.len() < 2 { " " } else { UTF8_SYMBOLS.down } );
  //     let mut normal_dependencies_iter = self.normal_dependencies.iter();
  //     let last = normal_dependencies_iter.next_back();

  //     for dep in normal_dependencies_iter
  //     {
  //       write!( f, "{spacer}{}{} {}", UTF8_SYMBOLS.tee, UTF8_SYMBOLS.right, dep.display_with_spacer( &new_spacer )? )?;
  // }
  //     if let Some( last ) = last
  //     {
  //       new_spacer = format!( "{spacer}   " );
  //       write!( f, "{spacer}{}{} {}", UTF8_SYMBOLS.ell, UTF8_SYMBOLS.right, last.display_with_spacer( &new_spacer )? )?;
  // }
  //     if !self.dev_dependencies.is_empty()
  //     {
  //       let mut dev_dependencies_iter = self.dev_dependencies.iter();
  //       let last = dev_dependencies_iter.next_back();
  //       write!( f, "{spacer}[dev-dependencies]\n" )?;
  //       for dep in dev_dependencies_iter
  //       {
  //         write!( f, "{spacer}{}{} {}", UTF8_SYMBOLS.tee, UTF8_SYMBOLS.right, dep.display_with_spacer( &new_spacer )? )?;
  // }
  //       // unwrap - safe because `is_empty` check
  //       write!( f, "{spacer}{}{} {}", UTF8_SYMBOLS.ell, UTF8_SYMBOLS.right, last.unwrap().display_with_spacer( &new_spacer )? )?;
  // }
  //     if !self.build_dependencies.is_empty()
  //     {
  //       let mut build_dependencies_iter = self.build_dependencies.iter();
  //       let last = build_dependencies_iter.next_back();
  //       write!( f, "{spacer}[build-dependencies]\n" )?;
  //       for dep in build_dependencies_iter
  //       {
  //         write!( f, "{spacer}{}{} {}", UTF8_SYMBOLS.tee, UTF8_SYMBOLS.right, dep.display_with_spacer( &new_spacer )? )?;
  // }
  //       // unwrap - safe because `is_empty` check
  //       write!( f, "{spacer}{}{} {}", UTF8_SYMBOLS.ell, UTF8_SYMBOLS.right, last.unwrap().display_with_spacer( &new_spacer )? )?;
  // }

  //     Ok( f )
  // }
  // }

  // impl std ::fmt ::Display for ListNodeReport
  // {
  //   fn fmt( &self, f: &mut Formatter< '_ > ) -> std ::fmt ::Result
  //   {
  //     write!( f, "{}", self.display_with_spacer( "" )? )?;

  //     Ok( () )
  // }
  // }

  /// Represents the different report formats for the `list` action.
  #[ derive( Debug, Default, Clone ) ]
  pub enum ListReport
  {
  /// Represents a tree-like report format.
  Tree( Vec< tool ::TreePrinter > ),
  /// Represents a standard list report format in topological order.
  List( Vec< String > ),
  /// Represents an empty report format.
  #[ default ]
  Empty,
 }

  impl fmt ::Display for ListReport
  {
  fn fmt( &self, f: &mut fmt ::Formatter< '_ > ) -> fmt ::Result
  {
   match self
   {
  Self ::Tree( v ) =>
  write!
  (
   f,
   "{}",
   v.iter().map( std ::string ::ToString ::to_string ).collect :: < Vec< _ > >().join( "\n" )
 ),

  Self ::List( v ) =>
  write!
  (
   f,
   "{}",
   v.iter().enumerate().map( | ( i, v ) | format!( "[{i}] {v}" ) ).collect :: < Vec< _ > >().join( "\n" )
 ),

  Self ::Empty => write!( f, "Nothing" ),
 }
 }
 }

  // aaa: for Bohdan: descirption // aaa: done
  /// The `DependencyId` struct encapsulates the essential attributes of a dependency,
  #[ derive( Debug, Clone, PartialEq, Eq, Hash ) ]
  pub struct DependencyId
  {
  /// The name of the dependency.
  ///
  /// This is typically the name of the library or package that the package relies on.
  pub name: String,
  /// The version requirements for the dependency.
  ///
  /// Note: This will be compared to other dependencies and packages to build the tree
  pub version: semver ::VersionReq,
  /// An optional path to the manifest file of the dependency.
  ///
  /// This field may contain a path to the manifest file when the dependency is a local package
  /// or when specific path information is needed to locate the dependency's manifest.
  pub path: Option< ManifestFile >,
 }

  #[ allow( clippy ::trivially_copy_pass_by_ref, clippy ::needless_lifetimes ) ]
  fn process_package_dependency
  (
  workspace: &Workspace,
  package: &WorkspacePackageRef< '_ >,
  args: &ListOptions,
  dep_rep: &mut tool ::ListNodeReport,
  visited: &mut collection ::HashSet< DependencyId >
 )
  {
  for dependency in package.dependencies()
  {

   // aaa: for Bohdan: bad: suboptimal
   // aaa: Is that what you had in mind?
   let dep_crate_dir = dependency.crate_dir();
   if dep_crate_dir.is_some() && !args.dependency_sources.contains( &DependencySource ::Local ) { continue; }
   if dep_crate_dir.is_none() && !args.dependency_sources.contains( &DependencySource ::Remote ) { continue; }

   // aaa: extend test coverage. NewType. Description
   // aaa: NewType ✅ Description ✅ test coverage ❌ how to test structure without logic?
   // qqq: extend test coverage. NewType. Description
   let dep_id = DependencyId
   {
  name: dependency.name(),
  // unwrap should be safe because of `semver ::VersionReq`
  version: dependency.req(),
  path: dependency.crate_dir().map( CrateDir ::manifest_file ),
 };
   // format!( "{}+{}+{}", dependency.name(), dependency.req(), dependency.crate_dir().unwrap().manifest_file() );
   // let dep_id = format!( "{}+{}+{}", dependency.name(), dependency.req(), dependency.path().as_ref().map( | p | p.join( "Cargo.toml" ) ).unwrap_or_default() );

   let mut temp_vis = visited.clone();
   let dependency_rep = process_dependency
   (
  workspace,
  dependency,
  args,
  &mut temp_vis
 );
   match dependency.kind()
   {
  DependencyKind ::Normal if args.dependency_categories.contains( &DependencyCategory ::Primary ) =>
  dep_rep.normal_dependencies.push( dependency_rep ),
  DependencyKind ::Development if args.dependency_categories.contains( &DependencyCategory ::Dev ) =>
  dep_rep.dev_dependencies.push( dependency_rep ),
  DependencyKind ::Build if args.dependency_categories.contains( &DependencyCategory ::Build ) =>
  dep_rep.build_dependencies.push( dependency_rep ),
  _ => { visited.remove( &dep_id ); std ::mem ::swap( &mut temp_vis, visited ); }
 }

   *visited = std ::mem ::take( &mut temp_vis );
 }
 }

  fn process_dependency
  (
  workspace: &Workspace,
  dep: DependencyRef< '_ >,
  args: &ListOptions,
  visited: &mut collection ::HashSet< DependencyId >
 )
  -> tool ::ListNodeReport
  {
  let mut dep_rep = tool ::ListNodeReport
  {
   name: dep.name().clone(),
   version: if args.info.contains( &PackageAdditionalInfo ::Version ) { Some( dep.req().to_string() ) } else { None },
   // manifest_file: if args.info.contains( &PackageAdditionalInfo ::Path ) { dep.manifest_file().as_ref().map( | p | p.clone().into_std_path_buf() ) } else { None },
   crate_dir: if args.info.contains( &PackageAdditionalInfo ::Path ) { dep.crate_dir() } else { None },
   duplicate: false,
   normal_dependencies: vec![],
   dev_dependencies: vec![],
   build_dependencies: vec![],
 };

  // let dep_id = format!( "{}+{}+{}", dep.name(), dep.req(), dep.crate_dir().as_ref().map( | p | p.join( "Cargo.toml" ) ).unwrap_or_default() );
  let dep_id = DependencyId
  {
   name: dep.name(),
   // unwrap should be safe because of `semver ::VersionReq`
   version: dep.req(),
   path: dep.crate_dir().map( CrateDir ::manifest_file ),
 };
  // if this is a cycle (we have visited this node before)
  if visited.contains( &dep_id )
  {
   dep_rep.duplicate = true;

   return dep_rep;
 }

  // if we have not visited this node before, mark it as visited
  visited.insert( dep_id );
  if let Some( crate_dir ) = &dep.crate_dir()
  {
   if let Some( package ) = workspace.package_find_by_manifest( crate_dir.clone().manifest_file() )
   {
  process_package_dependency( workspace, &package, args, &mut dep_rep, visited );
 }
 }

  dep_rep
 }

  /// Retrieve a list of packages based on the given arguments.
  ///
  /// # Arguments
  ///
  /// - `args` : `ListOptions` - The arguments for listing packages.
  ///
  /// # Returns
  ///
  /// - `Result< ListReport, (ListReport, Error) >` - A result containing the list report if successful,
  ///   or a tuple containing the list report and error if not successful.
  /// # Errors
  ///
  /// Returns an error if it fails to read the workspace manifest, parse dependencies,
  /// or if a dependency cycle is detected in topological sort mode.
  ///
  /// # Panics
  ///
  /// The function may panic if it encounters a package version that cannot be parsed
  /// into a valid `semver ::VersionReq`. This can happen with malformed `Cargo.toml` files.
  ///
  /// # Errors
  ///
  /// Returns an error if it fails to read the workspace manifest, parse dependencies,
  /// or if a dependency cycle is detected in topological sort mode.
  ///
  /// # Panics
  ///
  /// The function may panic if it encounters a package version that cannot be parsed
  /// into a valid `semver ::VersionReq`. This can happen with malformed `Cargo.toml` files.
  ///
  #[ allow( clippy ::too_many_lines ) ]
  #[ cfg_attr( feature = "tracing", tracing ::instrument ) ]
  pub fn list_all( args: ListOptions )
  -> ResultWithReport< ListReport, error ::untyped ::Error > // qqq: should be specific error
  // qqq: use typed error
  {
  let mut report = ListReport ::default();

  let manifest = Manifest ::try_from( args.path_to_manifest.clone() )
  .context( "List of packages by specified manifest path" )
  .err_with_report( &report )?;

  let workspace = Workspace ::try_from( manifest.crate_dir() )
  .context( "Reading workspace" )
  .err_with_report( &report )?;

  let is_package = manifest.package_is();
  // let is_package = manifest.package_is().context( "try to identify manifest type" ).err_with( report.clone() )?;

  let tree_package_report =
  | manifest_file: ManifestFile, report: &mut ListReport, visited: &mut collection ::HashSet< DependencyId > |
  {

   let package = workspace
   .package_find_by_manifest( manifest_file )
   .ok_or_else( || format_err!( "Package not found in the workspace" ) )?;
   let version = if args.info.contains( &PackageAdditionalInfo ::Version )
   {
  Some( package.version().to_string() )
 }
   else
   {
  None
 };
   let crate_dir = if args.info.contains( &PackageAdditionalInfo ::Path )
   {
  Some( package.crate_dir() ).transpose()
 }
   else
   {
  Result ::Ok( None )
 }?;
   let mut package_report = tool ::ListNodeReport
   {
  name: package.name().to_string(),
  // aaa: for Bohdan: too long lines
  // aaa: moved out
  version,
  // aaa: for Bohdan: don't put multiline if into struct constructor
  // aaa: moved out
  crate_dir,
  duplicate: false,
  normal_dependencies: vec![],
  dev_dependencies: vec![],
  build_dependencies: vec![],
 };

   process_package_dependency( &workspace, &package, &args, &mut package_report, visited );

   let printer = TreePrinter ::new( &package_report );
   *report = match report
   {
  ListReport ::Tree( ref mut v ) => ListReport ::Tree
  ( { v.extend( [ printer ] ); v.clone() } ),
  ListReport ::Empty => ListReport ::Tree( vec![ printer ] ),
  ListReport ::List( _ ) => unreachable!(),
 };
   Ok :: < (), error ::untyped ::Error >( () )
 };

  match args.format
  {
   ListFormat ::Tree if is_package =>
   {
  let mut visited = collection ::HashSet ::new();
  tree_package_report( manifest.manifest_file, &mut report, &mut visited ).err_with_report( &report )?;
  let ListReport ::Tree( tree ) = report else { unreachable!() };
  let printer = merge_build_dependencies( tree );
  let rep: Vec< ListNodeReport > = printer
  .iter()
  .map( | printer | printer.info.clone() )
  .collect();
  let tree = rearrange_duplicates( rep );
  report = ListReport ::Tree( tree );
 }
   ListFormat ::Tree =>
   {
  let packages = workspace.packages();
  let mut visited = packages
  .clone()
  .map
  (
   // aaa: is it safe to use unwrap here
   // unwrap is safe because Version has less information than VersionReq
   | p |
   DependencyId
   {
  name: p.name().into(),
  version: semver ::VersionReq ::parse( &p.version().to_string() ).unwrap(),
  path: p.manifest_file().ok()
 }
 )
  .collect();
  for package in packages
  {
   tree_package_report( package.manifest_file().unwrap(), &mut report, &mut visited ).err_with_report( &report )?;
 }
  let ListReport ::Tree( tree ) = report else { unreachable!() };
  let printer = merge_build_dependencies( tree );
  let rep: Vec< ListNodeReport > = printer
  .iter()
  .map( | printer | printer.info.clone() )
  .collect();
  let tree = merge_dev_dependencies( rep );
  report = ListReport ::Tree( tree );
 }
   ListFormat ::Topological =>
   {

  let root_crate = manifest.data.get( "package" )
  .map( | m | m[ "name" ].to_string().trim().replace( '\"', "" ) )
  .unwrap_or_default();

  // let root_crate = manifest
  // .data
  // // .as_ref()
  // .and_then( | m | m.get( "package" ) )
  // .map( | m | m[ "name" ].to_string().trim().replace( '\"', "" ) )
  // .unwrap_or_default();

  let dep_filter = move | _p: WorkspacePackageRef< '_ >, d: DependencyRef< '_ > |
  {
   (
  args.dependency_categories.contains( &DependencyCategory ::Primary ) && d.kind() == DependencyKind ::Normal
  || args.dependency_categories.contains( &DependencyCategory ::Dev ) && d.kind() == DependencyKind ::Development
  || args.dependency_categories.contains( &DependencyCategory ::Build ) && d.kind() == DependencyKind ::Build
 )
   &&
   (
  args.dependency_sources.contains( &DependencySource ::Remote ) && d.crate_dir().is_none()
  || args.dependency_sources.contains( &DependencySource ::Local ) && d.crate_dir().is_some()
 )
 };

  let packages = workspace.packages();
  let packages_map: collection ::HashMap< package ::PackageName, collection ::HashSet< package ::PackageName > > = packages ::filter
  (
   packages.clone(),
   packages ::FilterMapOptions
   {
  dependency_filter: Some( Box ::new( dep_filter ) ),
  ..Default ::default()
 }
 );

  let graph = graph ::construct( &packages_map );

  let sorted = toposort( &graph, None )
  .map_err
  (
   | e |
   {
  use std ::ops ::Index;
  format_err!
  (
   "Failed to process toposort for package: {:?}",
   graph.index( e.node_id() )
 )
 }
 )
  .err_with_report( &report )?;
  let packages_info: collection ::HashMap< String, WorkspacePackageRef< '_ > > =
  packages.map( | p | ( p.name().to_string(), p ) ).collect();

  if root_crate.is_empty()
  {
   let names: Vec< String > = sorted
   .into_iter()
   .rev()
   .map( | dep_idx | graph.node_weight( dep_idx ).unwrap() )
   .map
   (
  | name: &&package ::PackageName |
  {
   let mut name: String = name.to_string();
   if let Some( p ) = packages_info.get( &name[ .. ] )
   {
  if args.info.contains( &PackageAdditionalInfo ::Version )
  {
   name.push( ' ' );
   name.push_str( &p.version().to_string() );
 }
  if args.info.contains( &PackageAdditionalInfo ::Path )
  {
   name.push( ' ' );
   name.push_str( &p.manifest_file()?.to_string() );
   // aaa: is it safe to use unwrap here? // aaa: should be safe, but now returns an error
 }
 }
   std ::result ::Result :: < String, crate ::entity ::files ::PathError > ::Ok( name )
 }
 )
   .collect :: < Result< _, _ > >()
   .err_with_report( &report )?;

   report = ListReport ::List( names );
 }
  else
  {
   let node = graph
   .node_indices()
   .find( | n | graph.node_weight( *n ).unwrap().as_str() == root_crate )
   .unwrap();
   let mut dfs = Dfs ::new( &graph, node );
   let mut subgraph = Graph ::new();
   let mut node_map = collection ::HashMap ::new();
   while let Some( n )= dfs.next( &graph )
   {
  node_map.insert( n, subgraph.add_node( graph[ n ] ) );
 }

   for e in graph.edge_references()
   {
  if let ( Some( &s ), Some( &t ) ) =
  (
   node_map.get( &e.source() ),
   node_map.get( &e.target() )
 )
  {
   subgraph.add_edge( s, t, () );
 }
 }

   let mut topo = Topo ::new( &subgraph );
   let mut names = Vec ::new();
   while let Some( n ) = topo.next( &subgraph )
   {
  let mut name: String = subgraph[ n ].to_string();
  if let Some( p ) = packages_info.get( &name[ .. ] )
  {
   if args.info.contains( &PackageAdditionalInfo ::Version )
   {
  name.push( ' ' );
  name.push_str( &p.version().to_string() );
 }
   if args.info.contains( &PackageAdditionalInfo ::Path )
   {
  name.push( ' ' );
  name.push_str( &p.manifest_file().unwrap().to_string() );
 }
 }
  names.push( name );
 }
   names.reverse();

   report = ListReport ::List( names );
 }
 }
 }

  Result ::Ok( report )
 }

  fn merge_build_dependencies( mut report: Vec< tool ::TreePrinter > ) -> Vec< tool ::TreePrinter >
  {
  let mut build_dependencies = vec![];
  for node_report in &mut report
  {
   build_dependencies = merge_build_dependencies_impl
   (
  &mut node_report.info,
  build_dependencies
 );
 }
  if let Some( last_report ) = report.last_mut()
  {
   last_report.info.build_dependencies = build_dependencies;
 }

  report
 }

  fn merge_build_dependencies_impl
  (
  report: &mut tool ::ListNodeReport,
  mut build_deps_acc: Vec< tool ::ListNodeReport >
 )
  -> Vec< tool ::ListNodeReport >
  {
  for dep in report.normal_dependencies.iter_mut()
  .chain( report.dev_dependencies.iter_mut() )
  .chain( report.build_dependencies.iter_mut() )
  {
   build_deps_acc = merge_build_dependencies_impl( dep, build_deps_acc );
 }

  for dep in std ::mem ::take( &mut report.build_dependencies )
  {
   if !build_deps_acc.contains( &dep )
   {
  build_deps_acc.push( dep );
 }
 }

  build_deps_acc
 }

  fn merge_dev_dependencies( mut report: Vec< tool ::ListNodeReport > ) -> Vec< tool ::TreePrinter >
  {
  let mut dev_dependencies = vec![];
  for node_report in &mut report
  {
   dev_dependencies = merge_dev_dependencies_impl( node_report, dev_dependencies );
 }
  if let Some( last_report ) = report.last_mut()
  {
   last_report.dev_dependencies = dev_dependencies;
 }
  let printer: Vec< TreePrinter > = report
  .iter()
  .map( TreePrinter ::new )
  .collect();
  printer
 }

  fn merge_dev_dependencies_impl
  (
  report: &mut ListNodeReport,
  mut dev_deps_acc: Vec< ListNodeReport >
 ) -> Vec< ListNodeReport >
  {
  for dep in report.normal_dependencies.iter_mut()
  .chain( report.dev_dependencies.iter_mut() )
  .chain( report.build_dependencies.iter_mut() )
  {
   dev_deps_acc = merge_dev_dependencies_impl( dep, dev_deps_acc );
 }

  for dep in std ::mem ::take( &mut report.dev_dependencies )
  {
   if !dev_deps_acc.contains( &dep )
   {
  dev_deps_acc.push( dep );
 }
 }

  dev_deps_acc
 }

  fn rearrange_duplicates( mut report: Vec< tool ::ListNodeReport > ) -> Vec< tool ::TreePrinter >
  {
  let mut required_normal: collection ::HashMap< usize, Vec< tool ::ListNodeReport > > = collection ::HashMap ::new();
  for ( i, report ) in report.iter_mut().enumerate()
  {
   let ( required, exist ) : ( Vec< _ >, Vec< _ > ) = std ::mem ::take
   (
  &mut report.normal_dependencies
 )
   .into_iter()
   .partition( | d | d.duplicate );
   report.normal_dependencies = exist;
   required_normal.insert( i, required );
 }

  rearrange_duplicates_resolver( &mut report, &mut required_normal );
  for ( i, deps ) in required_normal
  {
   report[ i ].normal_dependencies.extend( deps );
 }

  let printer: Vec< TreePrinter > = report
  .iter()
  .map( TreePrinter ::new )
  .collect();

  printer
 }

  fn rearrange_duplicates_resolver
  (
  report: &mut [ ListNodeReport ],
  required: &mut collection ::HashMap< usize, Vec< ListNodeReport > >
 )
  {
  for node in report
  {
   rearrange_duplicates_resolver( &mut node.normal_dependencies, required );
   rearrange_duplicates_resolver( &mut node.dev_dependencies, required );
   rearrange_duplicates_resolver( &mut node.build_dependencies, required );

   if !node.duplicate
   {
  if let Some( r ) = required.iter_mut().flat_map( | ( _, v ) | v )
  .find
  (
   | r | r.name == node.name && r.version == node.version && r.crate_dir == node.crate_dir
 )
  {
   std ::mem ::swap( r, node );
 }
 }
 }
 }
}

//

crate ::mod_interface!
{
  /// Arguments for `list` action.
  own use ListOptions;
  /// Additional information to include in a package report.
  own use PackageAdditionalInfo;
  /// Represents where a dependency located.
  own use DependencySource;
  /// Represents the category of a dependency.
  own use DependencyCategory;
  /// Argument for `list` action. Sets the output format.
  own use ListFormat;
  /// Argument for `list` action. Sets filter(local or all) packages should be in the output.
  own use ListFilter;
  /// Contains output of the action.
  own use ListReport;
  /// Contains output of a single node of the action.
  // own use ListNodeReport;
  /// List packages in workspace.
  orphan use list_all;
}