use actr_protocol::ActrId;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Dest {
Shell,
Local,
Actor(ActrId),
}
impl Dest {
#[inline]
pub fn shell() -> Self {
Dest::Shell
}
#[inline]
pub fn local() -> Self {
Dest::Local
}
#[inline]
pub fn actor(id: ActrId) -> Self {
Dest::Actor(id)
}
#[inline]
pub fn is_shell(&self) -> bool {
matches!(self, Dest::Shell)
}
#[inline]
pub fn is_local(&self) -> bool {
matches!(self, Dest::Local)
}
#[inline]
pub fn is_actor(&self) -> bool {
matches!(self, Dest::Actor(_))
}
#[inline]
pub fn as_actor_id(&self) -> Option<&ActrId> {
match self {
Dest::Actor(id) => Some(id),
_ => None,
}
}
}
impl From<ActrId> for Dest {
#[inline]
fn from(id: ActrId) -> Self {
Dest::Actor(id)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_dest_creation() {
let shell_dest = Dest::shell();
assert!(shell_dest.is_shell());
assert!(!shell_dest.is_local());
assert!(!shell_dest.is_actor());
let local_dest = Dest::local();
assert!(!local_dest.is_shell());
assert!(local_dest.is_local());
assert!(!local_dest.is_actor());
let id = ActrId::default();
let actor_dest = Dest::actor(id);
assert!(!actor_dest.is_shell());
assert!(!actor_dest.is_local());
assert!(actor_dest.is_actor());
}
#[test]
fn test_dest_hash() {
use std::collections::HashMap;
let id1 = ActrId::default();
let id2 = ActrId {
serial_number: 1,
..Default::default()
};
let mut map = HashMap::new();
map.insert(Dest::shell(), "shell");
map.insert(Dest::local(), "local");
map.insert(Dest::actor(id1), "actor1");
map.insert(Dest::actor(id2), "actor2");
assert_eq!(map.len(), 4);
}
#[test]
fn test_dest_as_actor_id() {
let shell_dest = Dest::shell();
assert_eq!(shell_dest.as_actor_id(), None);
let local_dest = Dest::local();
assert_eq!(local_dest.as_actor_id(), None);
let id = ActrId::default();
let actor_dest = Dest::actor(id.clone());
assert_eq!(actor_dest.as_actor_id(), Some(&id));
}
}