use thiserror::Error;
#[derive(Error, Debug, Clone)]
pub enum TypesRegistryError {
#[error("Invalid GTS ID: {0}")]
InvalidGtsId(String),
#[error("Entity not found: {0}")]
NotFound(String),
#[error("Entity already exists: {0}")]
AlreadyExists(String),
#[error("Validation failed: {0}")]
ValidationFailed(String),
#[error("Not in ready mode")]
NotInReadyMode,
#[error("Internal error: {0}")]
Internal(String),
}
impl TypesRegistryError {
#[must_use]
pub fn invalid_gts_id(message: impl Into<String>) -> Self {
Self::InvalidGtsId(message.into())
}
#[must_use]
pub fn not_found(gts_id: impl Into<String>) -> Self {
Self::NotFound(gts_id.into())
}
#[must_use]
pub fn already_exists(gts_id: impl Into<String>) -> Self {
Self::AlreadyExists(gts_id.into())
}
#[must_use]
pub fn validation_failed(message: impl Into<String>) -> Self {
Self::ValidationFailed(message.into())
}
#[must_use]
pub const fn not_in_ready_mode() -> Self {
Self::NotInReadyMode
}
#[must_use]
pub fn internal(message: impl Into<String>) -> Self {
Self::Internal(message.into())
}
#[must_use]
pub const fn is_not_found(&self) -> bool {
matches!(self, Self::NotFound(_))
}
#[must_use]
pub const fn is_already_exists(&self) -> bool {
matches!(self, Self::AlreadyExists(_))
}
#[must_use]
pub const fn is_validation_failed(&self) -> bool {
matches!(self, Self::ValidationFailed(_))
}
#[must_use]
pub const fn is_invalid_gts_id(&self) -> bool {
matches!(self, Self::InvalidGtsId(_))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_constructors() {
let err = TypesRegistryError::invalid_gts_id("missing vendor");
assert!(err.is_invalid_gts_id());
assert!(err.to_string().contains("missing vendor"));
let err = TypesRegistryError::not_found("gts.acme.core.events.test.v1~");
assert!(err.is_not_found());
let err = TypesRegistryError::already_exists("gts.acme.core.events.test.v1~");
assert!(err.is_already_exists());
let err = TypesRegistryError::validation_failed("schema invalid");
assert!(err.is_validation_failed());
let err = TypesRegistryError::not_in_ready_mode();
assert!(matches!(err, TypesRegistryError::NotInReadyMode));
let err = TypesRegistryError::internal("database error");
assert!(matches!(err, TypesRegistryError::Internal(_)));
}
#[test]
fn test_error_display() {
let err = TypesRegistryError::InvalidGtsId("bad format".to_owned());
assert_eq!(err.to_string(), "Invalid GTS ID: bad format");
let err = TypesRegistryError::NotFound("gts.x.core.events.test.v1~".to_owned());
assert_eq!(
err.to_string(),
"Entity not found: gts.x.core.events.test.v1~"
);
let err = TypesRegistryError::AlreadyExists("gts.x.core.events.test.v1~".to_owned());
assert_eq!(
err.to_string(),
"Entity already exists: gts.x.core.events.test.v1~"
);
let err = TypesRegistryError::ValidationFailed("missing required field".to_owned());
assert_eq!(err.to_string(), "Validation failed: missing required field");
let err = TypesRegistryError::NotInReadyMode;
assert_eq!(err.to_string(), "Not in ready mode");
let err = TypesRegistryError::Internal("unexpected".to_owned());
assert_eq!(err.to_string(), "Internal error: unexpected");
}
}