2019-11-24 18:59:44 +01:00
## my issue
2019-11-17 16:50:48 +01:00
2019-11-17 15:30:48 +01:00
In module `modules` , I have:
```rust
2019-11-24 18:59:44 +01:00
pub trait Action {
fn act(& self, record: & mut Record) -> Result< (), ()>;
2019-11-17 15:30:48 +01:00
}
2019-11-24 18:59:44 +01:00
pub trait Filter {
fn filter(& self, record: & mut Record) -> bool;
}
2019-11-17 15:30:48 +01:00
2019-11-24 18:59:44 +01:00
pub enum ModuleKind { Action, Filter }
2019-11-17 15:30:48 +01:00
2019-11-24 18:59:44 +01:00
pub struct Available {
name: & 'static str,
kind: ModuleKind,
cons: fn(ModuleArgs) -> Box< dyn Module >
2019-11-17 15:30:48 +01:00
}
2019-11-24 18:59:44 +01:00
pub enum Module {
Action(& Action),
Filter(& Filter)
2019-11-17 15:30:48 +01:00
}
2019-11-24 18:59:44 +01:00
impl Module {
fn run(& self, record: & mut Record) -> Result< bool , ( ) > {
match self {
Module::Action(a) => match a.act(record) {
Ok(()) => Ok(true),
Err(()) => Err(())
},
Module::Filter(f) => Ok(f.filter(record))
}
}
2019-11-17 15:30:48 +01:00
}
```
2019-11-24 18:59:44 +01:00
To give some context, I intend to use `Available` with the [Inventory ](https://crates.io/crates/inventory ) crate: each module will register itself, either as a “Filter” or as an “Action”.
2019-11-17 16:16:25 +01:00
2019-11-24 18:59:44 +01:00
The 2 things I do not like in this code are:
2019-11-17 16:16:25 +01:00
2019-11-24 18:59:44 +01:00
* I feel like there is redundancy between the `Module` enum and the `ModuleKind` enum.
* I need, that:
- an `Available` with `kind` being a `ModuleKind::Action` has a `cons` that produces a `Module::Action`
- an `Available` with `kind` being a `ModuleKind::Filter` has a `cons` that produces a `Module::Filter`
2019-11-17 16:16:25 +01:00
2019-11-24 18:59:44 +01:00
or even better, that I could read the variant produced by `cons` , and `kind` wouldn’ t be needed any more.