use serde::{Deserialize, Serialize};
use crate::GateValidationError;
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize)]
pub struct ActorRef {
pub kind: String,
pub id: String,
}
#[derive(Deserialize)]
struct RawActorRef {
kind: String,
id: String,
}
impl TryFrom<RawActorRef> for ActorRef {
type Error = GateValidationError;
fn try_from(raw: RawActorRef) -> Result<Self, Self::Error> {
Self::try_new(raw.kind, raw.id)
}
}
impl<'de> Deserialize<'de> for ActorRef {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let raw = RawActorRef::deserialize(deserializer)?;
ActorRef::try_from(raw).map_err(serde::de::Error::custom)
}
}
impl ActorRef {
pub fn try_new(
kind: impl Into<String>,
id: impl Into<String>,
) -> Result<Self, GateValidationError> {
let kind = kind.into();
let id = id.into();
if kind.is_empty() {
return Err(GateValidationError::EmptyActorKind);
}
if id.is_empty() {
return Err(GateValidationError::EmptyActorId);
}
Ok(Self { kind, id })
}
pub fn new(kind: impl Into<String>, id: impl Into<String>) -> Self {
Self::try_new(kind, id).expect("ActorRef::new: kind and id must not be empty")
}
pub fn anonymous() -> Self {
Self {
kind: "anonymous".into(),
id: "local".into(),
}
}
}