1use serde::{Deserialize, Serialize};
2
3use crate::GateValidationError;
4
5#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize)]
9pub struct ActorRef {
10 pub kind: String,
11 pub id: String,
12}
13
14#[derive(Deserialize)]
16struct RawActorRef {
17 kind: String,
18 id: String,
19}
20
21impl TryFrom<RawActorRef> for ActorRef {
22 type Error = GateValidationError;
23
24 fn try_from(raw: RawActorRef) -> Result<Self, Self::Error> {
25 Self::try_new(raw.kind, raw.id)
26 }
27}
28
29impl<'de> Deserialize<'de> for ActorRef {
30 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
31 where
32 D: serde::Deserializer<'de>,
33 {
34 let raw = RawActorRef::deserialize(deserializer)?;
35 ActorRef::try_from(raw).map_err(serde::de::Error::custom)
36 }
37}
38
39impl ActorRef {
40 pub fn try_new(
42 kind: impl Into<String>,
43 id: impl Into<String>,
44 ) -> Result<Self, GateValidationError> {
45 let kind = kind.into();
46 let id = id.into();
47 if kind.is_empty() {
48 return Err(GateValidationError::EmptyActorKind);
49 }
50 if id.is_empty() {
51 return Err(GateValidationError::EmptyActorId);
52 }
53 Ok(Self { kind, id })
54 }
55
56 pub fn new(kind: impl Into<String>, id: impl Into<String>) -> Self {
58 Self::try_new(kind, id).expect("ActorRef::new: kind and id must not be empty")
59 }
60
61 pub fn anonymous() -> Self {
63 Self {
64 kind: "anonymous".into(),
65 id: "local".into(),
66 }
67 }
68
69 pub fn is_anonymous(&self) -> bool {
71 self.kind == "anonymous"
72 }
73
74 pub fn binding_id(&self) -> Option<&str> {
79 if self.is_anonymous() {
80 None
81 } else {
82 Some(self.id.as_str())
83 }
84 }
85}