Attribute Macro cosmic_macros::handler

source ·
#[handler]
Expand description

This impl attribute creates a fn handler which receives directed waves and routes them to the first local method which the route selector matches. To implement:


use cosmic_space::err::SpaceErr;
use cosmic_space::hyper::HyperSubstance;
use cosmic_space::log::PointLogger;
use cosmic_space::substance::Substance;
use cosmic_space::substance::Substance::Text;
use cosmic_space::wave::core::ReflectedCore;
use cosmic_space::wave::exchange::asynch::InCtx;

#[derive(DirectedHandler)]
pub struct MyHandler {
  logger: PointLogger
}

#[handler]
impl MyHandler {
    /// the route attribute captures an ExtMethod implementing a custom `MyNameIs`
    /// notice that the InCtx will accept any valid cosmic_space::substance::Substance
    #[route("Ext<MyNameIs>")]
    pub async fn hello(&self, ctx: InCtx<'_, Text>) -> Result<String, SpaceErr> {
        /// also we can return any Substance in our Reflected wave
        Ok(format!("Hello, {}", ctx.input.to_string()))
    }

    /// if the function returns nothing then an Empty Ok Reflected will be returned unless
    /// the wave type is `Wave<Signal>`
    #[route("Ext<Bye>")]
    pub async fn bye(&self, ctx: InCtx<'_,()>) {
       self.logger.info("funny that! He left without saying a word!");
    }
}