use std::path::PathBuf;
use thiserror::Error;
pub type Result<T> = std::result::Result<T, PachaError>;
#[derive(Error, Debug)]
pub enum PachaError {
#[error("database error: {0}")]
Database(#[from] rusqlite::Error),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
#[error("TOML error: {0}")]
TomlDeserialize(#[from] toml::de::Error),
#[error("TOML serialization error: {0}")]
TomlSerialize(#[from] toml::ser::Error),
#[error("MessagePack error: {0}")]
MessagePack(String),
#[error("artifact not found: {kind} '{name}' version {version}")]
NotFound {
kind: String,
name: String,
version: String,
},
#[error("artifact already exists: {kind} '{name}' version {version}")]
AlreadyExists {
kind: String,
name: String,
version: String,
},
#[error("invalid version string: {0}")]
InvalidVersion(String),
#[error("content hash mismatch: expected {expected}, got {actual}")]
HashMismatch {
expected: String,
actual: String,
},
#[error("storage path error: {0}")]
StoragePath(PathBuf),
#[error("compression error: {0}")]
Compression(String),
#[error("invalid stage transition from {from} to {to}")]
InvalidStageTransition {
from: String,
to: String,
},
#[error("validation error: {0}")]
Validation(String),
#[error("registry not initialized at {0}")]
NotInitialized(PathBuf),
#[error("invalid URI: {0}")]
InvalidUri(String),
#[error("unsupported operation '{operation}': {reason}")]
UnsupportedOperation {
operation: String,
reason: String,
},
#[error("signature verification failed")]
SignatureInvalid,
#[error("remote registry error: {0}")]
RemoteRegistry(String),
#[error("invalid format: {0}")]
InvalidFormat(String),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display_not_found() {
let err = PachaError::NotFound {
kind: "model".to_string(),
name: "fraud-detector".to_string(),
version: "1.0.0".to_string(),
};
assert_eq!(
err.to_string(),
"artifact not found: model 'fraud-detector' version 1.0.0"
);
}
#[test]
fn test_error_display_hash_mismatch() {
let err = PachaError::HashMismatch {
expected: "abc123".to_string(),
actual: "def456".to_string(),
};
assert_eq!(
err.to_string(),
"content hash mismatch: expected abc123, got def456"
);
}
#[test]
fn test_error_display_invalid_stage() {
let err = PachaError::InvalidStageTransition {
from: "development".to_string(),
to: "archived".to_string(),
};
assert_eq!(
err.to_string(),
"invalid stage transition from development to archived"
);
}
}