#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(test)]
extern crate alloc;
#[cfg(feature = "embassy")]
#[doc(hidden)]
pub use embassy_executor;
#[macro_export]
macro_rules! mediator {
(
$vis:vis struct $name:ident {
event_queue_capacity: $capacity:expr;
event_workers: $workers:expr;
modules: [$first:ident $(, $rest:ident)* $(,)?];
$(decorators: [$($decorators:path),* $(,)?];)?
$(event_failure_reporter: $reporter:ty;)?
}
) => {
$first!($crate::__medi_rs_collect_modules, {
$vis struct $name;
event_queue_capacity: $capacity;
event_workers: $workers;
modules: [];
decorators: [$($($decorators),*)?];
event_failure_reporter: [$($reporter)?];
count: [];
remaining: [$($rest),*];
});
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __medi_rs_collect_modules {
(
$vis:vis struct $name:ident;
event_queue_capacity: $capacity:expr;
event_workers: $workers:expr;
modules: [$($modules:tt)*];
decorators: [$($decorators:path),*];
event_failure_reporter: [$($reporter:ty)?];
count: [$($count:tt)*];
remaining: [];
) => {
$crate::__medi_rs_finalize_composition! {
$vis struct $name;
event_queue_capacity: $capacity;
event_workers: $workers;
modules: [$($modules)*];
decorators: [$($decorators),*];
event_failure_reporter: [$($reporter)?];
count: [$($count)*];
}
};
(
$vis:vis struct $name:ident;
event_queue_capacity: $capacity:expr;
event_workers: $workers:expr;
modules: [$($modules:tt)*];
decorators: [$($decorators:path),*];
event_failure_reporter: [$($reporter:ty)?];
count: [$($count:tt)*];
remaining: [$next:ident $(, $rest:ident)*];
) => {
$next!($crate::__medi_rs_collect_modules, {
$vis struct $name;
event_queue_capacity: $capacity;
event_workers: $workers;
modules: [$($modules)*];
decorators: [$($decorators),*];
event_failure_reporter: [$($reporter)?];
count: [$($count)*];
remaining: [$($rest),*];
});
};
}
#[cfg(any(
all(feature = "tokio", feature = "wasm"),
all(feature = "tokio", feature = "embassy"),
all(feature = "wasm", feature = "embassy")
))]
compile_error!("features `tokio`, `wasm`, and `embassy` are mutually exclusive; enable at most one runtime adapter");
pub mod adapters;
mod bus;
mod error;
mod event;
mod resource;
#[doc(hidden)]
pub mod tlist;
pub use adapters::lifecycle::Lifecycle;
pub use adapters::queue::EventQueue;
pub use adapters::shutdown::ShutdownSignal;
pub use error::*;
pub use medi_rs_macros::{__medi_rs_finalize_composition, MediCommand, medi_handler, medi_module, medi_task};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct EventHandlerFailure {
event_name: &'static str,
handler_name: &'static str,
}
impl EventHandlerFailure {
#[doc(hidden)]
pub const fn new(event_name: &'static str, handler_name: &'static str) -> Self {
Self {
event_name,
handler_name,
}
}
pub const fn event_name(self) -> &'static str {
self.event_name
}
pub const fn handler_name(self) -> &'static str {
self.handler_name
}
}
pub trait EventFailureReporter: Send + Sync + 'static {
fn report(&self, failure: EventHandlerFailure) -> impl core::future::Future<Output = ()> + Send;
}
pub trait DecoratorNext<C>: Send {
type Response: Send;
type Error: Send;
fn call(
self,
command: C,
) -> impl core::future::Future<Output = core::result::Result<Self::Response, Self::Error>> + Send;
}
impl<C, F, Fut, Response, Error> DecoratorNext<C> for F
where
F: FnOnce(C) -> Fut + Send,
Fut: core::future::Future<Output = core::result::Result<Response, Error>> + Send,
Response: Send,
Error: Send,
{
type Response = Response;
type Error = Error;
fn call(self, command: C) -> impl core::future::Future<Output = core::result::Result<Response, Error>> + Send {
self(command)
}
}
pub trait Command
where
Self: Send + Sync + 'static,
{
type Response: Send + Sync + 'static;
type Error: Send;
}
#[doc(hidden)]
pub trait StaticSendCommand<M>: Command + Sized {
fn send(
self,
mediator: &M,
) -> impl core::future::Future<Output = core::result::Result<Self::Response, Self::Error>> + Send;
}
#[doc(hidden)]
pub trait StaticPublish<M>: Sized {
fn publish(self, mediator: &M) -> impl core::future::Future<Output = Result<()>> + Send;
}
#[doc(hidden)]
pub trait StaticTryPublish<M>: Sized {
fn try_publish(self, mediator: &M) -> core::result::Result<(), TryPublishError<Self>>;
}