use crate::Pid;
use thiserror::Error;
pub type Result<T> = std::result::Result<T, ActorError>;
#[derive(Error, Debug, Clone)]
pub enum ActorError {
#[error("Actor {0} not found")]
ActorNotFound(Pid),
#[error("Failed to send message to {0}")]
SendFailed(Pid),
#[error("Mailbox full for actor {0}")]
MailboxFull(Pid),
#[error("Actor {0} already stopped")]
ActorStopped(Pid),
#[error("Actor initialization failed: {0}")]
InitFailed(String),
#[error("Actor {0} panicked: {1}")]
ActorPanicked(Pid, String),
#[error("Supervisor error: {0}")]
SupervisorError(String),
#[error("Link/monitor operation failed: {0}")]
LinkError(String),
#[error("Operation timed out")]
Timeout,
#[error("Name '{0}' is already registered")]
NameAlreadyRegistered(String),
#[error("Name '{0}' is not registered")]
NameNotRegistered(String),
#[error("Invalid name: {0}")]
InvalidName(String),
#[error("Timer {0} not found")]
TimerNotFound(u64),
#[error("Actor error: {0}")]
Other(String),
}
impl ActorError {
pub fn other(msg: impl Into<String>) -> Self {
Self::Other(msg.into())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Pid;
#[test]
fn test_error_display() {
let pid = Pid::new();
let err = ActorError::ActorNotFound(pid);
assert!(format!("{}", err).contains("not found"));
}
#[test]
fn test_error_clone() {
let err1 = ActorError::Timeout;
let err2 = err1.clone();
assert!(matches!(err2, ActorError::Timeout));
}
#[test]
fn test_other_error() {
let err = ActorError::other("custom error");
assert!(matches!(err, ActorError::Other(_)));
}
}