1 个不稳定版本

0.1.0 2024年8月21日

#1187 in Rust 模式

MIT/Apache

6KB

Bloc (业务逻辑组件)

Bloc 是一个用于构建可扩展和维护的业务逻辑库。

此包目前仅公开了 EnumHandler derive 宏。

// You declare the CounterEvent enum:

use bloc::EnumHandler;

#[derive(EnumHandler)]
pub enum CounterEvent {
    Increment,
    Decrement,
    Reset,
    Set(i32),
}

// you can implement the CounterEventHandler trait for your struct:

pub struct Counter;

impl CounterEventHandler for Counter {
    fn on_increment(&self) {
        println!("Increment");
    }
    fn on_decrement(&self) {
        println!("Decrement");
    }
    fn on_reset(&self) {
        println!("Reset");
    }
    fn on_set(&self, set: i32) {
        println!("Set: {}", set);
    }
}


// and the enum_handler macro will generate the following code for you behind the scenes:

pub trait CounterEventHandler {
    fn on(&self, e: CounterEvent) -> () {
        match (e) {
            CounterEvent::Increment => self.on_increment(),
            CounterEvent::Decrement => self.on_decrement(),
            CounterEvent::Reset => self.on_reset(),
            CounterEvent::Set(arg) => self.on_set(arg),
        }
    }
    fn on_increment(&self) -> ();
    fn on_decrement(&self) -> ();
    fn on_reset(&self) -> ();
    fn on_set(&self, set: i32) -> ();
}

许可证

许可协议为以下之一:

由您选择。

依赖项

~0.6–1.1MB
~25K SLoC