1 unstable release

0.1.0 Aug 21, 2024

#1517 in Rust patterns

MIT/Apache

6KB

Bloc (Business Logic Component)

Bloc is a library for building scalable and maintainable business logic.

This crate currently only exposes the EnumHandler derive marcro.

// 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) -> ();
}

License

Licensed under either of

at your option.

Dependencies

~0.6–1.1MB
~25K SLoC