arch-toolkit 0.3.0

Complete Rust toolkit for Arch Linux package management
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
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
//! Dependency-related data types for dependency resolution operations.

use serde::{Deserialize, Serialize};

// === Enums ===

/// Status of a dependency relative to the current system state.
///
/// This enum represents the installation status and requirements for a dependency,
/// used throughout the dependency resolution process to track what actions are needed.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum DependencyStatus {
    /// Already installed and version matches requirement.
    Installed {
        /// Installed version of the package.
        version: String,
    },
    /// Not installed, needs to be installed.
    ToInstall,
    /// Installed but outdated, needs upgrade.
    ToUpgrade {
        /// Current installed version.
        current: String,
        /// Required version for upgrade.
        required: String,
    },
    /// Conflicts with existing packages.
    Conflict {
        /// Reason for the conflict.
        reason: String,
    },
    /// Cannot be found in configured repositories or AUR.
    Missing,
}

impl DependencyStatus {
    /// What: Check if the dependency is already installed.
    ///
    /// Inputs:
    /// - `self`: The dependency status to check.
    ///
    /// Output:
    /// - Returns `true` if the dependency is installed (regardless of version).
    ///
    /// Details:
    /// - Returns `true` for both `Installed` and `ToUpgrade` variants.
    #[must_use]
    pub const fn is_installed(&self) -> bool {
        matches!(self, Self::Installed { .. } | Self::ToUpgrade { .. })
    }

    /// What: Check if the dependency needs action (install or upgrade).
    ///
    /// Inputs:
    /// - `self`: The dependency status to check.
    ///
    /// Output:
    /// - Returns `true` if the dependency needs to be installed or upgraded.
    ///
    /// Details:
    /// - Returns `true` for `ToInstall` and `ToUpgrade` variants.
    #[must_use]
    pub const fn needs_action(&self) -> bool {
        matches!(self, Self::ToInstall | Self::ToUpgrade { .. })
    }

    /// What: Check if there's a conflict with this dependency.
    ///
    /// Inputs:
    /// - `self`: The dependency status to check.
    ///
    /// Output:
    /// - Returns `true` if the dependency has a conflict.
    ///
    /// Details:
    /// - Returns `true` only for the `Conflict` variant.
    #[must_use]
    pub const fn is_conflict(&self) -> bool {
        matches!(self, Self::Conflict { .. })
    }

    /// What: Get a priority value for sorting (lower = more urgent).
    ///
    /// Inputs:
    /// - `self`: The dependency status to get priority for.
    ///
    /// Output:
    /// - Returns a numeric priority where lower numbers indicate higher urgency.
    ///
    /// Details:
    /// - Priority order: Conflict (0) < Missing (1) < `ToInstall` (2) < `ToUpgrade` (3) < Installed (4).
    #[must_use]
    pub const fn priority(&self) -> u8 {
        match self {
            Self::Conflict { .. } => 0,
            Self::Missing => 1,
            Self::ToInstall => 2,
            Self::ToUpgrade { .. } => 3,
            Self::Installed { .. } => 4,
        }
    }
}

impl std::fmt::Display for DependencyStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Installed { version } => write!(f, "Installed ({version})"),
            Self::ToInstall => write!(f, "To Install"),
            Self::ToUpgrade { current, required } => {
                write!(f, "To Upgrade ({current} -> {required})")
            }
            Self::Conflict { reason } => write!(f, "Conflict: {reason}"),
            Self::Missing => write!(f, "Missing"),
        }
    }
}

/// Source of a dependency package.
///
/// Indicates where a dependency package comes from, which affects how it's resolved
/// and installed.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum DependencySource {
    /// Official repository package.
    Official {
        /// Repository name (e.g., "core", "extra", "community").
        repo: String,
    },
    /// AUR package.
    Aur,
    /// Local package (not in repos).
    Local,
}

impl std::fmt::Display for DependencySource {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Official { repo } => write!(f, "Official ({repo})"),
            Self::Aur => write!(f, "AUR"),
            Self::Local => write!(f, "Local"),
        }
    }
}

/// Package source for dependency resolution input.
///
/// Used when specifying packages to resolve dependencies for, indicating whether
/// the package is from an official repository or AUR.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum PackageSource {
    /// Official repository.
    Official {
        /// Repository name (e.g., "core", "extra", "community").
        repo: String,
        /// Target architecture (e.g., `"x86_64"`).
        arch: String,
    },
    /// AUR package.
    Aur,
}

impl std::fmt::Display for PackageSource {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Official { repo, arch } => write!(f, "Official ({repo}/{arch})"),
            Self::Aur => write!(f, "AUR"),
        }
    }
}

// === Core Structs ===

/// Information about a single dependency.
///
/// Contains all metadata about a dependency including its status, source, and
/// relationships to other packages.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Dependency {
    /// Package name.
    pub name: String,
    /// Required version constraint (e.g., ">=1.2.3" or empty if no constraint).
    pub version_req: String,
    /// Current status of this dependency.
    pub status: DependencyStatus,
    /// Source repository or origin.
    pub source: DependencySource,
    /// Packages that require this dependency.
    pub required_by: Vec<String>,
    /// Packages that this dependency depends on (transitive dependencies).
    pub depends_on: Vec<String>,
    /// Whether this is a core repository package.
    pub is_core: bool,
    /// Whether this is a critical system package.
    pub is_system: bool,
}

/// Package reference for dependency resolution input.
///
/// Used to specify packages for which dependencies should be resolved.
/// This is a simplified representation compared to full package details.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct PackageRef {
    /// Package name.
    pub name: String,
    /// Package version.
    pub version: String,
    /// Package source (official or AUR).
    pub source: PackageSource,
}

impl PackageRef {
    /// What: Create a reference to an official repository package.
    ///
    /// Inputs:
    /// - `name`: Package name.
    /// - `version`: Package version.
    /// - `repo`: Repository name (e.g., "core", "extra").
    /// - `arch`: Target architecture (e.g., `x86_64`, `any`).
    ///
    /// Output:
    /// - `PackageRef` with `PackageSource::Official`.
    ///
    /// Details:
    /// - Convenience constructor for resolution and install-planning inputs.
    #[must_use]
    pub fn official(
        name: impl Into<String>,
        version: impl Into<String>,
        repo: impl Into<String>,
        arch: impl Into<String>,
    ) -> Self {
        Self {
            name: name.into(),
            version: version.into(),
            source: PackageSource::Official {
                repo: repo.into(),
                arch: arch.into(),
            },
        }
    }

    /// What: Create a reference to an AUR package.
    ///
    /// Inputs:
    /// - `name`: Package name.
    /// - `version`: Package version.
    ///
    /// Output:
    /// - `PackageRef` with `PackageSource::Aur`.
    ///
    /// Details:
    /// - Convenience constructor for resolution and install-planning inputs.
    #[must_use]
    pub fn aur(name: impl Into<String>, version: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            version: version.into(),
            source: PackageSource::Aur,
        }
    }
}

/// Parsed dependency specification (name with optional version requirement).
///
/// Result of parsing a dependency string like "python>=3.12" or "glibc".
#[derive(Clone, Debug, PartialEq, Eq, Default)]
pub struct DependencySpec {
    /// Package name.
    pub name: String,
    /// Version constraint (may be empty if no constraint specified).
    pub version_req: String,
}

impl DependencySpec {
    /// What: Create a new dependency spec with just a name.
    ///
    /// Inputs:
    /// - `name`: Package name (will be converted to String).
    ///
    /// Output:
    /// - Returns a new `DependencySpec` with empty version requirement.
    ///
    /// Details:
    /// - Convenience constructor for dependencies without version constraints.
    #[must_use]
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            version_req: String::new(),
        }
    }

    /// What: Create a new dependency spec with name and version requirement.
    ///
    /// Inputs:
    /// - `name`: Package name (will be converted to String).
    /// - `version_req`: Version requirement string (e.g., ">=1.2.3").
    ///
    /// Output:
    /// - Returns a new `DependencySpec` with both name and version requirement.
    ///
    /// Details:
    /// - Convenience constructor for dependencies with version constraints.
    #[must_use]
    pub fn with_version(name: impl Into<String>, version_req: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            version_req: version_req.into(),
        }
    }

    /// What: Check if this spec has a version requirement.
    ///
    /// Inputs:
    /// - `self`: The dependency spec to check.
    ///
    /// Output:
    /// - Returns `true` if a version requirement is specified.
    ///
    /// Details:
    /// - Checks if `version_req` is non-empty.
    #[must_use]
    pub const fn has_version_req(&self) -> bool {
        !self.version_req.is_empty()
    }
}

impl std::fmt::Display for DependencySpec {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.version_req.is_empty() {
            write!(f, "{}", self.name)
        } else {
            write!(f, "{}{}", self.name, self.version_req)
        }
    }
}

/// Reverse dependency analysis result.
///
/// Contains the list of packages that depend on the target packages, along with
/// summary statistics for each target package.
#[derive(Clone, Debug, Default)]
pub struct ReverseDependencyReport {
    /// Packages that depend on the target packages.
    pub dependents: Vec<Dependency>,
    /// Per-package summary statistics.
    pub summaries: Vec<ReverseDependencySummary>,
}

/// Summary statistics for a single package's reverse dependencies.
///
/// Used in reverse dependency analysis to summarize how many packages depend
/// on a given package, broken down by direct and transitive dependents.
#[derive(Clone, Debug, Default)]
pub struct ReverseDependencySummary {
    /// Package name.
    pub package: String,
    /// Number of packages that directly depend on this package (depth 1).
    pub direct_dependents: usize,
    /// Number of packages that depend on this package through other packages (depth ≥ 2).
    pub transitive_dependents: usize,
    /// Total number of dependents (direct + transitive).
    pub total_dependents: usize,
}

/// Parsed .SRCINFO file data.
///
/// Contains all dependency-related fields extracted from a .SRCINFO file,
/// which is the machine-readable format generated from PKGBUILD files.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct SrcinfoData {
    /// Package base name (may differ from pkgname for split packages).
    pub pkgbase: String,
    /// Package name (may differ from pkgbase for split packages).
    pub pkgname: String,
    /// Package version.
    pub pkgver: String,
    /// Package release number.
    pub pkgrel: String,
    /// Runtime dependencies.
    pub depends: Vec<String>,
    /// Build-time dependencies.
    pub makedepends: Vec<String>,
    /// Test dependencies.
    pub checkdepends: Vec<String>,
    /// Optional dependencies.
    pub optdepends: Vec<String>,
    /// Conflicting packages.
    pub conflicts: Vec<String>,
    /// Packages this package provides.
    pub provides: Vec<String>,
    /// Packages this package replaces.
    pub replaces: Vec<String>,
}

/// What: Carry raw `.SRCINFO` metadata returned by an injected graph metadata provider.
///
/// Inputs:
/// - Requested name, selected actual package, verified source, and raw `.SRCINFO` text.
///
/// Output:
/// - Supplies enough information to resolve direct and virtual dependencies without a crate-level
///   AUR or HTTP dependency.
///
/// Details:
/// - `package_name` must name the selected split-package output. If it differs from
///   `requested_name`, the resolver verifies that the selected output provides the requested name.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DependencyMetadata {
    /// Package or virtual name queried from the provider.
    pub requested_name: String,
    /// Actual selected package name, including a split-package output when applicable.
    pub package_name: String,
    /// Verified source supplied by the metadata provider.
    pub source: DependencySource,
    /// Raw `.SRCINFO` text for the containing package base.
    pub srcinfo: String,
}

impl DependencyMetadata {
    /// What: Construct injected raw metadata for one requested package or provider.
    ///
    /// Inputs:
    /// - `requested_name`: Queried package or virtual name.
    /// - `package_name`: Selected actual package output.
    /// - `source`: Verified source of the selected package.
    /// - `srcinfo`: Raw `.SRCINFO` package-base metadata.
    ///
    /// Output:
    /// - Returns a metadata record suitable for `DependencyMetadataProvider`.
    ///
    /// Details:
    /// - This constructor performs no I/O or parsing so deterministic fixtures can use it directly.
    #[must_use]
    pub fn new(
        requested_name: impl Into<String>,
        package_name: impl Into<String>,
        source: DependencySource,
        srcinfo: impl Into<String>,
    ) -> Self {
        Self {
            requested_name: requested_name.into(),
            package_name: package_name.into(),
            source,
            srcinfo: srcinfo.into(),
        }
    }
}

/// What: Describe a batched injected metadata-provider result.
///
/// Inputs:
/// - A requested name and either returned metadata, an absence reason, or a retrieval failure.
///
/// Output:
/// - Lets graph resolution retain partial results and report actionable diagnostics.
///
/// Details:
/// - Provider failures are non-fatal for sibling branches and never cause fallback AUR inference.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DependencyMetadataResponse {
    /// Verified metadata was returned for a requested package or virtual dependency.
    Found(DependencyMetadata),
    /// No verified metadata exists for the requested name.
    Missing {
        /// Requested package or virtual name.
        requested_name: String,
        /// Actionable absence reason.
        reason: String,
    },
    /// Metadata retrieval failed through a network, helper, or provider-specific error.
    Failure {
        /// Requested package or virtual name.
        requested_name: String,
        /// Actionable provider error message.
        message: String,
    },
}

/// What: Identify the source and requested/provider identity behind a graph node.
///
/// Inputs:
/// - The requested dependency name, optional verified source, and optional selected provider.
///
/// Output:
/// - Lets callers distinguish direct packages, virtual providers, and unresolved names.
///
/// Details:
/// - `source` is `None` only when metadata is absent or failed. The resolver never infers AUR
///   provenance merely because an unknown name was not found elsewhere.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct DependencyProvenance {
    /// Original dependency name requested by the parent package.
    pub requested_name: String,
    /// Verified source reported by the metadata provider, if metadata was available.
    pub source: Option<DependencySource>,
    /// Actual package selected to satisfy a virtual request, if different from the request.
    pub provider: Option<String>,
}

/// What: Represent one inclusive or exclusive edge of an intersected version range.
///
/// Inputs:
/// - A version string and whether equality is permitted at that edge.
///
/// Output:
/// - Supplies a lower or upper bound for `DependencyConstraintRange`.
///
/// Details:
/// - Version ordering uses the dependency resolver's epoch/pkgver/pkgrel comparator.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct DependencyVersionBound {
    /// Bound version including epoch and pkgrel when present.
    pub version: String,
    /// Whether the bound includes its version value.
    pub inclusive: bool,
}

/// What: Store the deterministic intersection of dependency version requirements.
///
/// Inputs:
/// - Zero or more `=`, `>`, `>=`, `<`, or `<=` requirements for one resolved package.
///
/// Output:
/// - Exposes the most restrictive compatible lower and upper bounds.
///
/// Details:
/// - Equal requirements are represented by equal inclusive lower and upper bounds. An absent
///   bound means no requirement on that side of the interval.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct DependencyConstraintRange {
    /// Most restrictive compatible lower bound, if any.
    pub lower: Option<DependencyVersionBound>,
    /// Most restrictive compatible upper bound, if any.
    pub upper: Option<DependencyVersionBound>,
}

/// What: Describe the resolution state of a graph node.
///
/// Inputs:
/// - Metadata, provider, and conflict observations made during one graph run.
///
/// Output:
/// - Lets callers identify resolved, missing, and conflicting graph nodes.
///
/// Details:
/// - Missing nodes retain a `DependencyProvenance` with no source rather than being labelled AUR.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum DependencyGraphNodeStatus {
    /// Metadata was parsed and the package can participate in dependency traversal.
    #[default]
    Resolved,
    /// Metadata was unavailable or failed validation for the requested package.
    Missing,
    /// The package conflicts with another resolved graph node.
    Conflicting,
}

/// What: Represent one deterministic dependency graph node.
///
/// Inputs:
/// - Verified metadata and merged requirements for one actual package.
///
/// Output:
/// - Stores stable node identity, source provenance, split-package base, and selected metadata.
///
/// Details:
/// - `name` is the actual selected package. `provenance.requested_name` retains the virtual or
///   direct dependency name that selected it.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct DependencyGraphNode {
    /// Stable actual package identity used by graph edges.
    pub name: String,
    /// Package-base name retained from `.SRCINFO`.
    pub pkgbase: Option<String>,
    /// Combined `epoch:pkgver-pkgrel` version, if metadata was parsed.
    pub version: Option<String>,
    /// Verified source and provider provenance.
    pub provenance: DependencyProvenance,
    /// Current graph node state.
    pub status: DependencyGraphNodeStatus,
    /// Intersected requirements targeting this node.
    pub constraints: DependencyConstraintRange,
    /// Virtual packages supplied by this node.
    pub provides: Vec<String>,
    /// Declared package or virtual conflicts for this node.
    pub conflicts: Vec<String>,
    /// Minimum lexical traversal depth at which this node was encountered.
    pub depth: usize,
}

/// What: Represent one directed dependency requirement between graph nodes.
///
/// Inputs:
/// - Parent and selected child package names, requested dependency name, and version requirement.
///
/// Output:
/// - Preserves dependency and virtual-provider provenance independently of rendering.
///
/// Details:
/// - Edges are sorted lexically by the resolver and can be rendered without triggering metadata
///   lookup or changing the resolution result.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct DependencyGraphEdge {
    /// Actual package that declares the dependency.
    pub from: String,
    /// Actual selected package, or the missing requested name when metadata is absent.
    pub to: String,
    /// Dependency or virtual package name written by the parent.
    pub requested_name: String,
    /// Version requirement written by the parent, if present.
    pub version_req: String,
}

/// What: Categorize non-fatal graph-resolution diagnostics.
///
/// Inputs:
/// - Metadata, bound, graph, and conflict events observed during resolution.
///
/// Output:
/// - Provides stable categories for actionable caller diagnostics.
///
/// Details:
/// - Diagnostics preserve partial graph results instead of silently omitting failed branches.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum DependencyGraphDiagnosticKind {
    /// Metadata was not available for a requested package or virtual dependency.
    MissingMetadata,
    /// The injected provider returned a transport, helper, or other retrieval failure.
    MetadataFailure,
    /// Returned `.SRCINFO` text did not contain the selected requested package output.
    MalformedSrcinfo,
    /// A dependency path returned to a package already in the active traversal path.
    Cycle,
    /// A child exceeded the configured transitive depth.
    DepthLimit,
    /// Adding a node exceeded the configured per-run node limit.
    NodeLimit,
    /// The provider exceeded the configured metadata timeout.
    Timeout,
    /// A dependency requirement used an unsupported operator or omitted its version.
    MalformedConstraint,
    /// Multiple valid requirements for one selected package have an empty intersection.
    IncompatibleConstraints,
    /// A declared package or virtual conflict matched another resolved node.
    Conflict,
    /// A provider returned no response or a response for an unrequested package.
    MetadataProtocol,
}

/// What: Record one actionable non-fatal graph-resolution event.
///
/// Inputs:
/// - A diagnostic kind, affected package, optional related package, and message.
///
/// Output:
/// - Lets callers surface partial-resolution limitations without parsing log output.
///
/// Details:
/// - Entries are sorted deterministically by kind, package, related package, and message.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct DependencyGraphDiagnostic {
    /// Stable category for the event.
    pub kind: DependencyGraphDiagnosticKind,
    /// Affected package or requested dependency name.
    pub package: String,
    /// Related package when the event concerns an edge, cycle, or conflict.
    pub related_package: Option<String>,
    /// Actionable detail suitable for caller display.
    pub message: String,
}

/// What: Return the bounded, deterministic output of one metadata graph-resolution run.
///
/// Inputs:
/// - Root package references, injected metadata, and graph resolution bounds.
///
/// Output:
/// - Contains lexical roots, nodes, edges, and structured diagnostics.
///
/// Details:
/// - The graph is independent from tree rendering and remains useful when metadata failures leave
///   partial branches unresolved.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct DependencyGraphResolution {
    /// Requested root package names in lexical order.
    pub roots: Vec<String>,
    /// Resolved and missing nodes in lexical order by actual package name.
    pub nodes: Vec<DependencyGraphNode>,
    /// Directed edges in lexical order.
    pub edges: Vec<DependencyGraphEdge>,
    /// Non-fatal diagnostic events in lexical order.
    pub diagnostics: Vec<DependencyGraphDiagnostic>,
}

/// What: Configure bounded metadata graph resolution.
///
/// Inputs:
/// - Maximum transitive depth, graph-node count, metadata timeout, and provider batch concurrency.
///
/// Output:
/// - Limits resource use for one graph-resolution run.
///
/// Details:
/// - Defaults are depth 8, 256 nodes, 10-second metadata timeout, and one provider batch at a
///   time. The synchronous resolver passes the timeout to the injected provider and keeps only one
///   batch in flight; providers must honor the timeout for preemptive I/O cancellation.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct DependencyGraphConfig {
    /// Maximum edges from a root to a traversed child; zero resolves root metadata only.
    pub max_depth: usize,
    /// Maximum unique graph nodes, including roots and missing nodes.
    pub max_nodes: usize,
    /// Maximum duration supplied to each metadata provider batch.
    pub metadata_timeout: std::time::Duration,
    /// Maximum names supplied in one provider batch; the synchronous resolver runs batches serially.
    pub max_concurrency: usize,
}

impl Default for DependencyGraphConfig {
    /// What: Construct conservative bounds for graph resolution.
    ///
    /// Inputs:
    /// - None.
    ///
    /// Output:
    /// - Returns depth 8, 256 nodes, a 10-second timeout, and serial provider batching.
    ///
    /// Details:
    /// - These defaults constrain fixture and production providers without changing the legacy
    ///   direct-only `DependencyResolver::resolve` entry point.
    fn default() -> Self {
        Self {
            max_depth: 8,
            max_nodes: 256,
            metadata_timeout: std::time::Duration::from_secs(10),
            max_concurrency: 1,
        }
    }
}

/// Result of dependency resolution operation.
///
/// Contains all resolved dependencies along with any conflicts or missing packages
/// discovered during the resolution process.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct DependencyResolution {
    /// Resolved dependencies with status.
    pub dependencies: Vec<Dependency>,
    /// Packages that have conflicts.
    pub conflicts: Vec<String>,
    /// Packages that are missing.
    pub missing: Vec<String>,
}

/// Configuration for dependency resolution.
///
/// Controls various aspects of how dependencies are resolved, including which
/// types of dependencies to include and how deep to traverse the dependency tree.
///
/// Note: This struct does not implement `Clone` or `Debug` because it contains
/// a function pointer (`pkgbuild_cache`) that cannot be cloned or debugged.
#[allow(clippy::struct_excessive_bools, clippy::type_complexity)]
pub struct ResolverConfig {
    /// Whether to include optional dependencies.
    pub include_optdepends: bool,
    /// Whether to include make dependencies.
    pub include_makedepends: bool,
    /// Whether to include check dependencies.
    pub include_checkdepends: bool,
    /// Maximum depth for transitive dependency resolution (0 = direct only).
    pub max_depth: usize,
    /// Custom callback for fetching PKGBUILD from cache (optional).
    pub pkgbuild_cache: Option<Box<dyn Fn(&str) -> Option<String> + Send + Sync>>,
    /// Whether to check AUR for missing dependencies.
    pub check_aur: bool,
}

#[allow(clippy::derivable_impls)]
impl Default for ResolverConfig {
    fn default() -> Self {
        Self {
            include_optdepends: false,
            include_makedepends: false,
            include_checkdepends: false,
            max_depth: 0, // Direct dependencies only
            pkgbuild_cache: None,
            check_aur: false,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn dependency_status_priority_ordering() {
        let conflict = DependencyStatus::Conflict {
            reason: "test".to_string(),
        };
        let missing = DependencyStatus::Missing;
        let to_install = DependencyStatus::ToInstall;
        let to_upgrade = DependencyStatus::ToUpgrade {
            current: "1.0".to_string(),
            required: "2.0".to_string(),
        };
        let installed = DependencyStatus::Installed {
            version: "1.0".to_string(),
        };

        assert!(conflict.priority() < missing.priority());
        assert!(missing.priority() < to_install.priority());
        assert!(to_install.priority() < to_upgrade.priority());
        assert!(to_upgrade.priority() < installed.priority());
    }

    #[test]
    fn dependency_status_helper_methods() {
        let installed = DependencyStatus::Installed {
            version: "1.0".to_string(),
        };
        assert!(installed.is_installed());
        assert!(!installed.needs_action());
        assert!(!installed.is_conflict());

        let to_install = DependencyStatus::ToInstall;
        assert!(!to_install.is_installed());
        assert!(to_install.needs_action());
        assert!(!to_install.is_conflict());

        let conflict = DependencyStatus::Conflict {
            reason: "test".to_string(),
        };
        assert!(!conflict.is_installed());
        assert!(!conflict.needs_action());
        assert!(conflict.is_conflict());
    }

    #[test]
    fn dependency_spec_constructors() {
        let spec1 = DependencySpec::new("glibc");
        assert_eq!(spec1.name, "glibc");
        assert!(spec1.version_req.is_empty());
        assert!(!spec1.has_version_req());

        let spec2 = DependencySpec::with_version("python", ">=3.12");
        assert_eq!(spec2.name, "python");
        assert_eq!(spec2.version_req, ">=3.12");
        assert!(spec2.has_version_req());
    }

    #[test]
    fn dependency_spec_display() {
        let spec1 = DependencySpec::new("glibc");
        assert_eq!(spec1.to_string(), "glibc");

        let spec2 = DependencySpec::with_version("python", ">=3.12");
        assert_eq!(spec2.to_string(), "python>=3.12");
    }

    #[test]
    fn dependency_status_display() {
        let installed = DependencyStatus::Installed {
            version: "1.0".to_string(),
        };
        assert!(installed.to_string().contains("Installed"));
        assert!(installed.to_string().contains("1.0"));

        let to_install = DependencyStatus::ToInstall;
        assert_eq!(to_install.to_string(), "To Install");

        let to_upgrade = DependencyStatus::ToUpgrade {
            current: "1.0".to_string(),
            required: "2.0".to_string(),
        };
        assert!(to_upgrade.to_string().contains("To Upgrade"));
        assert!(to_upgrade.to_string().contains("1.0"));
        assert!(to_upgrade.to_string().contains("2.0"));

        let conflict = DependencyStatus::Conflict {
            reason: "test reason".to_string(),
        };
        assert!(conflict.to_string().contains("Conflict"));
        assert!(conflict.to_string().contains("test reason"));

        let missing = DependencyStatus::Missing;
        assert_eq!(missing.to_string(), "Missing");
    }

    #[test]
    fn dependency_source_display() {
        let official = DependencySource::Official {
            repo: "core".to_string(),
        };
        assert!(official.to_string().contains("Official"));
        assert!(official.to_string().contains("core"));

        let aur = DependencySource::Aur;
        assert_eq!(aur.to_string(), "AUR");

        let local = DependencySource::Local;
        assert_eq!(local.to_string(), "Local");
    }

    #[test]
    fn package_source_display() {
        let official = PackageSource::Official {
            repo: "extra".to_string(),
            arch: "x86_64".to_string(),
        };
        assert!(official.to_string().contains("Official"));
        assert!(official.to_string().contains("extra"));
        assert!(official.to_string().contains("x86_64"));

        let aur = PackageSource::Aur;
        assert_eq!(aur.to_string(), "AUR");
    }

    #[test]
    fn serde_roundtrip_dependency_status() {
        let statuses = vec![
            DependencyStatus::Installed {
                version: "1.0.0".to_string(),
            },
            DependencyStatus::ToInstall,
            DependencyStatus::ToUpgrade {
                current: "1.0.0".to_string(),
                required: "2.0.0".to_string(),
            },
            DependencyStatus::Conflict {
                reason: "test conflict".to_string(),
            },
            DependencyStatus::Missing,
        ];

        for status in statuses {
            let json = serde_json::to_string(&status).expect("serialization should succeed");
            let deserialized: DependencyStatus =
                serde_json::from_str(&json).expect("deserialization should succeed");
            assert_eq!(status, deserialized);
        }
    }

    #[test]
    fn serde_roundtrip_dependency_source() {
        let sources = vec![
            DependencySource::Official {
                repo: "core".to_string(),
            },
            DependencySource::Aur,
            DependencySource::Local,
        ];

        for source in sources {
            let json = serde_json::to_string(&source).expect("serialization should succeed");
            let deserialized: DependencySource =
                serde_json::from_str(&json).expect("deserialization should succeed");
            assert_eq!(source, deserialized);
        }
    }

    #[test]
    fn serde_roundtrip_dependency() {
        let dep = Dependency {
            name: "glibc".to_string(),
            version_req: ">=2.35".to_string(),
            status: DependencyStatus::Installed {
                version: "2.35".to_string(),
            },
            source: DependencySource::Official {
                repo: "core".to_string(),
            },
            required_by: vec!["firefox".to_string(), "chromium".to_string()],
            depends_on: vec!["linux-api-headers".to_string()],
            is_core: true,
            is_system: true,
        };

        let json = serde_json::to_string(&dep).expect("serialization should succeed");
        let deserialized: Dependency =
            serde_json::from_str(&json).expect("deserialization should succeed");
        assert_eq!(dep.name, deserialized.name);
        assert_eq!(dep.version_req, deserialized.version_req);
        assert_eq!(dep.status, deserialized.status);
        assert_eq!(dep.source, deserialized.source);
        assert_eq!(dep.required_by, deserialized.required_by);
        assert_eq!(dep.depends_on, deserialized.depends_on);
        assert_eq!(dep.is_core, deserialized.is_core);
        assert_eq!(dep.is_system, deserialized.is_system);
    }

    #[test]
    fn serde_roundtrip_srcinfo_data() {
        let srcinfo = SrcinfoData {
            pkgbase: "test-package".to_string(),
            pkgname: "test-package".to_string(),
            pkgver: "1.0.0".to_string(),
            pkgrel: "1".to_string(),
            depends: vec!["glibc".to_string(), "python>=3.12".to_string()],
            makedepends: vec!["make".to_string(), "gcc".to_string()],
            checkdepends: vec!["check".to_string()],
            optdepends: vec!["optional: optional-package".to_string()],
            conflicts: vec!["conflicting-pkg".to_string()],
            provides: vec!["provided-pkg".to_string()],
            replaces: vec!["replaced-pkg".to_string()],
        };

        let json = serde_json::to_string(&srcinfo).expect("serialization should succeed");
        let deserialized: SrcinfoData =
            serde_json::from_str(&json).expect("deserialization should succeed");
        assert_eq!(srcinfo.pkgbase, deserialized.pkgbase);
        assert_eq!(srcinfo.pkgname, deserialized.pkgname);
        assert_eq!(srcinfo.pkgver, deserialized.pkgver);
        assert_eq!(srcinfo.pkgrel, deserialized.pkgrel);
        assert_eq!(srcinfo.depends, deserialized.depends);
        assert_eq!(srcinfo.makedepends, deserialized.makedepends);
        assert_eq!(srcinfo.checkdepends, deserialized.checkdepends);
        assert_eq!(srcinfo.optdepends, deserialized.optdepends);
        assert_eq!(srcinfo.conflicts, deserialized.conflicts);
        assert_eq!(srcinfo.provides, deserialized.provides);
        assert_eq!(srcinfo.replaces, deserialized.replaces);
    }

    #[test]
    fn serde_roundtrip_package_ref() {
        let pkg_ref = PackageRef {
            name: "firefox".to_string(),
            version: "121.0".to_string(),
            source: PackageSource::Official {
                repo: "extra".to_string(),
                arch: "x86_64".to_string(),
            },
        };

        let json = serde_json::to_string(&pkg_ref).expect("serialization should succeed");
        let deserialized: PackageRef =
            serde_json::from_str(&json).expect("deserialization should succeed");
        assert_eq!(pkg_ref, deserialized);
    }
}