pub type Result<T> = std::result::Result<T, PackageError>;
#[derive(Debug, thiserror::Error)]
pub enum PackageError {
#[error("serialization error: {0}")]
Serialize(#[from] serde_json::Error),
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("digest mismatch: expected {expected}, got {actual}")]
DigestMismatch { expected: String, actual: String },
#[error("malformed digest: {reason}")]
MalformedDigest { reason: String },
#[error("allowlist violation: {resource}")]
AllowlistViolation { resource: String },
#[error("invalid reference: {reason}")]
InvalidReference { reason: String },
#[error("slot conflict on '{slot}': tested={tested}, proposed={proposed}")]
SlotConflict {
slot: String,
tested: String,
proposed: String,
},
#[error("OCI layout error: {reason}")]
Layout { reason: String },
#[error("config slot violation on '{key}': {reason}")]
ConfigSlotViolation { key: String, reason: String },
#[error(
"attestation subject mismatch: the attestation names {supplied}, but this package's \
unattested manifest digest is {computed}"
)]
AttestationSubjectMismatch {
supplied: String,
computed: String,
},
#[error("attestation annotation '{annotation}' is not representable: {reason}")]
AttestationAnnotationInvalid {
annotation: String,
reason: String,
},
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn digest_mismatch_display_contains_structured_fields() {
let err = PackageError::DigestMismatch {
expected: "sha256:aaaa".to_string(),
actual: "sha256:bbbb".to_string(),
};
let msg = err.to_string();
assert!(msg.contains("sha256:aaaa"), "message was: {msg}");
assert!(msg.contains("sha256:bbbb"), "message was: {msg}");
}
#[test]
fn slot_conflict_display_contains_structured_fields() {
let err = PackageError::SlotConflict {
slot: "llm-provider".to_string(),
tested: "anthropic".to_string(),
proposed: "openai".to_string(),
};
let msg = err.to_string();
assert!(msg.contains("llm-provider"), "message was: {msg}");
assert!(msg.contains("anthropic"), "message was: {msg}");
assert!(msg.contains("openai"), "message was: {msg}");
}
#[test]
fn serialize_variant_wraps_serde_json_error_via_from() {
let json_err = serde_json::from_str::<serde_json::Value>("{not valid json")
.expect_err("must fail to parse");
let err: PackageError = json_err.into();
assert!(matches!(err, PackageError::Serialize(_)));
}
}