use std::any::type_name;
use std::fmt;
use std::future::Future;
use std::panic::Location;
use std::sync::Arc;
use crate::binding::{Instance, downcast_instance};
use crate::construct::Construct;
use crate::dependency::Dependencies;
use crate::resolver::Resolver;
use crate::scope::HookCapable;
use crate::signal::Signal;
use crate::timer::{BoxError, BoxFuture, Bound};
use crate::type_name::short_type_name;
pub trait OnModuleInit: Construct<Scope: HookCapable> {
const TIMEOUT: Bound = Bound::Default;
fn on_module_init(&self) -> impl Future<Output = Result<(), BoxError>> + Send;
}
pub trait OnApplicationBootstrap: Construct<Scope: HookCapable> {
const TIMEOUT: Bound = Bound::Default;
fn on_application_bootstrap(&self) -> impl Future<Output = Result<(), BoxError>> + Send;
}
pub trait OnModuleDestroy: Construct<Scope: HookCapable> {
const TIMEOUT: Bound = Bound::Default;
fn on_module_destroy(&self) -> impl Future<Output = ()> + Send;
}
pub trait BeforeApplicationShutdown: Construct<Scope: HookCapable> {
const TIMEOUT: Bound = Bound::Default;
fn before_application_shutdown(&self, signal: &Signal) -> impl Future<Output = ()> + Send;
}
pub trait OnApplicationShutdown: Construct<Scope: HookCapable> {
const TIMEOUT: Bound = Bound::Default;
fn on_application_shutdown(&self, signal: &Signal) -> impl Future<Output = ()> + Send;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum HookKind {
OnModuleInit,
OnApplicationBootstrap,
OnModuleDestroy,
BeforeApplicationShutdown,
OnApplicationShutdown,
}
impl fmt::Display for HookKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
HookKind::OnModuleInit => "OnModuleInit",
HookKind::OnApplicationBootstrap => "OnApplicationBootstrap",
HookKind::OnModuleDestroy => "OnModuleDestroy",
HookKind::BeforeApplicationShutdown => "BeforeApplicationShutdown",
HookKind::OnApplicationShutdown => "OnApplicationShutdown",
})
}
}
pub struct Hooks<T> {
pub(crate) slots: Vec<TraitHook<T>>,
}
impl<T> Hooks<T> {
pub(crate) fn new() -> Self {
Hooks { slots: Vec::new() }
}
pub fn on_module_init(&mut self)
where
T: OnModuleInit,
{
self.slots.push(TraitHook {
kind: HookKind::OnModuleInit,
bound: <T as OnModuleInit>::TIMEOUT,
run: TraitHookFn::Startup(Box::new(|t| Box::pin(t.on_module_init()))),
});
}
pub fn on_application_bootstrap(&mut self)
where
T: OnApplicationBootstrap,
{
self.slots.push(TraitHook {
kind: HookKind::OnApplicationBootstrap,
bound: <T as OnApplicationBootstrap>::TIMEOUT,
run: TraitHookFn::Startup(Box::new(|t| Box::pin(t.on_application_bootstrap()))),
});
}
pub fn on_module_destroy(&mut self)
where
T: OnModuleDestroy,
{
self.slots.push(TraitHook {
kind: HookKind::OnModuleDestroy,
bound: <T as OnModuleDestroy>::TIMEOUT,
run: TraitHookFn::Destroy(Box::new(|t| Box::pin(t.on_module_destroy()))),
});
}
pub fn before_application_shutdown(&mut self)
where
T: BeforeApplicationShutdown,
{
self.slots.push(TraitHook {
kind: HookKind::BeforeApplicationShutdown,
bound: <T as BeforeApplicationShutdown>::TIMEOUT,
run: TraitHookFn::Signalled(Box::new(|t, s| Box::pin(t.before_application_shutdown(s)))),
});
}
pub fn on_application_shutdown(&mut self)
where
T: OnApplicationShutdown,
{
self.slots.push(TraitHook {
kind: HookKind::OnApplicationShutdown,
bound: <T as OnApplicationShutdown>::TIMEOUT,
run: TraitHookFn::Signalled(Box::new(|t, s| Box::pin(t.on_application_shutdown(s)))),
});
}
}
pub(crate) struct TraitHook<T> {
pub(crate) kind: HookKind,
pub(crate) bound: Bound,
pub(crate) run: TraitHookFn<T>,
}
pub(crate) enum TraitHookFn<T> {
Startup(Box<dyn for<'a> Fn(&'a T) -> BoxFuture<'a, Result<(), BoxError>> + Send + Sync>),
Destroy(Box<dyn for<'a> Fn(&'a T) -> BoxFuture<'a, ()> + Send + Sync>),
Signalled(Box<dyn for<'a> Fn(&'a T, &'a Signal) -> BoxFuture<'a, ()> + Send + Sync>),
}
#[derive(Clone)]
pub(crate) struct HookRecord {
pub(crate) kind: HookKind,
pub(crate) bound: Bound,
pub(crate) bound_at: Option<&'static Location<'static>>,
pub(crate) run: HookFn,
pub(crate) dependencies: Dependencies,
pub(crate) location: &'static Location<'static>,
}
pub(crate) type HookFn = Arc<dyn for<'a> Fn(HookCx<'a>) -> BoxFuture<'a, Result<(), BoxError>> + Send + Sync>;
pub(crate) fn hook_fn<H>(hook: H) -> HookFn
where
H: for<'a> Fn(HookCx<'a>) -> BoxFuture<'a, Result<(), BoxError>> + Send + Sync + 'static,
{
Arc::new(hook)
}
pub(crate) struct HookCx<'a> {
pub(crate) resolver: &'a Resolver<'a>,
pub(crate) instance: Option<&'a Instance>,
pub(crate) signal: Option<&'a Signal>,
}
pub(crate) fn erase_trait_hooks<T: Construct>(location: &'static Location<'static>) -> Vec<HookRecord> {
let mut hooks = Hooks::<T>::new();
T::hooks(&mut hooks);
hooks
.slots
.into_iter()
.map(|hook| HookRecord {
kind: hook.kind,
bound: hook.bound,
bound_at: None,
run: erase_trait_hook(hook.kind, hook.run),
dependencies: Dependencies::default(),
location,
})
.collect()
}
fn erase_trait_hook<T: Construct>(kind: HookKind, run: TraitHookFn<T>) -> HookFn {
let run = Arc::new(run);
hook_fn(move |cx| {
let run = Arc::clone(&run);
let HookCx { instance, signal, .. } = cx;
let instance = instance.and_then(downcast_instance::<T>);
Box::pin(async move {
let Some(instance) = instance else {
return Err(not_run::<T>(kind, "instance"));
};
match &*run {
TraitHookFn::Startup(hook) => hook(&*instance).await,
TraitHookFn::Destroy(hook) => {
hook(&*instance).await;
Ok(())
}
TraitHookFn::Signalled(hook) => {
let Some(signal) = signal else {
return Err(not_run::<T>(kind, "shutdown signal"));
};
hook(&*instance, signal).await;
Ok(())
}
}
})
})
}
fn not_run<T>(kind: HookKind, missing: &str) -> BoxError {
format!("the {kind} hook of `{}` did not run: it was handed no {missing}", short_type_name(type_name::<T>())).into()
}
#[macro_export]
macro_rules! hooks {
($h:expr) => {{
#[allow(unused_imports)]
use $crate::__private::hooks::{
NoBootstrap as _, NoBeforeShutdown as _, NoDestroy as _, NoInit as _, NoShutdown as _,
RegisterBootstrap as _, RegisterBeforeShutdown as _, RegisterDestroy as _,
RegisterInit as _, RegisterShutdown as _,
};
let h = $h;
(&$crate::__private::hooks::Probe::of(&*h)).__ulo_register_init(&mut *h);
(&$crate::__private::hooks::Probe::of(&*h)).__ulo_register_bootstrap(&mut *h);
(&$crate::__private::hooks::Probe::of(&*h)).__ulo_register_destroy(&mut *h);
(&$crate::__private::hooks::Probe::of(&*h)).__ulo_register_before_shutdown(&mut *h);
(&$crate::__private::hooks::Probe::of(&*h)).__ulo_register_shutdown(&mut *h);
}};
}