use crate::{Mail, Msg};
use serde::{Deserialize, Serialize};
use std::any::{self, Any};
use std::fmt::{self, Debug, Formatter};
pub trait Actor: Any + Send + Sync {
fn receive(&mut self, mail: Mail) -> Option<Mail>;
fn type_name(&self) -> &'static str {
any::type_name::<Self>()
}
fn post_start(&mut self, _mail: Mail) -> Option<Mail> {
Some(Msg::from_text("Start up signal received").into())
}
fn pre_shutdown(&mut self, _mail: Mail) -> Option<Mail> {
Some(Msg::from_text("Shutdown signal received").into())
}
}
impl Debug for dyn Actor {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let debug_msg = format!("Actor impl({:?})", self.type_name());
write!(f, "{}", debug_msg)
}
}
#[typetag::serde]
pub trait Producer {
fn produce(&mut self) -> Box<dyn Actor>;
fn from_string(&self, content: String) -> std::io::Result<Box<dyn Producer>> {
let producer: Box<dyn Producer> = serde_json::from_str(&content)?;
Ok(producer)
}
}
#[derive(Debug, Serialize, Deserialize, Default)]
pub(crate) struct ProducerDeserializer;
#[typetag::serde(name = "producer_deserializer")]
impl Producer for ProducerDeserializer {
fn produce(&mut self) -> Box<dyn Actor> {
panic!("Should not be called on ProducerDeserializer");
}
}