use std::any::Any;
use std::sync::Arc;
use laravel::model::Model;
use crate::action::{Action, Pending, Reference, Settings};
use crate::arguments::{Arguments, IntoArguments};
use crate::error::ActionError;
use crate::registry::{self, Call, Registration};
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Actions(Vec<(&'static str, &'static str)>);
impl Actions {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with<A: Action>(mut self, key: &'static str) -> Self {
self.0.push((key, A::NAME));
self
}
#[must_use]
pub fn get(&self, key: &str) -> Option<&'static str> {
self.0.iter().find(|(listed, _)| *listed == key).map(|(_, name)| *name)
}
}
#[macro_export]
macro_rules! actions {
($($key:expr => $action:ty),* $(,)?) => {
$crate::Actions::new()$(.with::<$action>($key))*
};
}
pub trait HasActions: Model {
#[must_use]
fn actions() -> Actions {
Actions::new()
}
#[must_use]
fn action<A: Action>(&self) -> Pending<A> {
A::create().on(self)
}
fn action_named(&self, key: &str) -> laravel::Result<NamedAction> {
let name = Self::actions().get(key).unwrap_or(key);
let registration = registry::find(name).map_err(|_| ActionError::NotDefined { key: key.to_owned() })?;
Ok(NamedAction {
registration,
settings: registration.settings(),
actor: None,
actionable: Some(Reference::of(self)),
model: Some(Arc::new(self.clone())),
})
}
}
pub trait IsActor: Model {
#[must_use]
fn act<A: Action>(&self) -> Pending<A> {
A::create().actor(self)
}
fn act_named(&self, name: &str) -> laravel::Result<NamedAction> {
let registration = registry::find(name)?;
Ok(NamedAction {
registration,
settings: registration.settings(),
actor: Some(Reference::of(self)),
actionable: None,
model: None,
})
}
}
pub struct NamedAction {
registration: &'static Registration,
settings: Settings,
actor: Option<Reference>,
actionable: Option<Reference>,
model: Option<Arc<dyn Any + Send + Sync>>,
}
impl NamedAction {
#[must_use]
pub fn actor<M: Model>(mut self, actor: &M) -> Self {
self.actor = Some(Reference::of(actor));
self
}
#[must_use]
pub fn retry(mut self, times: u32) -> Self {
self.settings.tries = times;
self
}
#[must_use]
pub fn delay(mut self, seconds: u64) -> Self {
self.settings.delay = Some(seconds);
self
}
#[must_use]
pub fn queue(mut self, queue: impl Into<String>) -> Self {
self.settings.queue = Some(queue.into());
self
}
#[must_use]
pub fn trace(mut self) -> Self {
self.settings.tracing = true;
self
}
#[must_use]
pub fn enable_logging(mut self) -> Self {
self.settings.logging = true;
self
}
fn call(&self, arguments: Arguments) -> Call {
Call {
settings: self.settings.clone(),
actor: self.actor.clone(),
actionable: self.actionable.clone(),
model: self.model.clone(),
arguments,
}
}
pub async fn run(&self, arguments: impl IntoArguments) -> laravel::Result<Box<dyn Any + Send>> {
self.registration
.run_named(self.call(arguments.into_arguments()?))
.await
}
pub async fn dispatch(&self, arguments: impl IntoArguments) -> laravel::Result<laravel::queue::JobId> {
self.registration
.dispatch_named(self.call(arguments.into_arguments()?))
.await
}
}