Skip to main content

bock_pkg/
error.rs

1//! Error types for the package manager.
2
3/// Errors that can occur during package management operations.
4#[derive(Debug, thiserror::Error)]
5pub enum PkgError {
6    /// Failed to parse the manifest file.
7    #[error("failed to parse manifest: {0}")]
8    ManifestParse(String),
9
10    /// Failed to parse a version or version requirement.
11    #[error("invalid version: {0}")]
12    InvalidVersion(String),
13
14    /// Dependency resolution failed.
15    #[error("dependency resolution failed: {0}")]
16    ResolutionFailed(String),
17
18    /// Unresolvable dependency constraints.
19    #[error("unresolvable dependency constraints:\n{}", format_conflicts(.0))]
20    UnresolvableConstraints(Vec<String>),
21
22    /// Package not found.
23    #[error("package not found: {0}")]
24    PackageNotFound(String),
25
26    /// I/O error.
27    #[error("I/O error: {0}")]
28    Io(String),
29
30    /// Lockfile parse error.
31    #[error("failed to parse lockfile: {0}")]
32    LockfileParse(String),
33
34    /// Network request failed (transport, status, or decode error).
35    #[error("network error: {0}")]
36    Network(String),
37
38    /// Downloaded artifact did not match the expected checksum.
39    #[error("checksum mismatch: expected {expected}, got {actual}")]
40    ChecksumMismatch {
41        /// SHA-256 hex expected from registry metadata.
42        expected: String,
43        /// SHA-256 hex actually computed from the downloaded bytes.
44        actual: String,
45    },
46}
47
48fn format_conflicts(conflicts: &[String]) -> String {
49    conflicts
50        .iter()
51        .map(|c| format!("  - {c}"))
52        .collect::<Vec<_>>()
53        .join("\n")
54}
55
56/// Result type for package operations.
57pub type PkgResult<T> = Result<T, PkgError>;