2019-11-17 15:58:01 +01:00
|
|
|
|
Intermediate problem:
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
pub struct Equals<'a> {
|
|
|
|
|
field: &'a str,
|
|
|
|
|
value: Value
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Equals<'_> {
|
|
|
|
|
pub fn from_args(mut args: ModuleArgs) -> Equals {
|
|
|
|
|
Equals {
|
|
|
|
|
field: match args.remove("field") {
|
|
|
|
|
Some(Value::Str(s)) => s,
|
|
|
|
|
_ => panic!("The Equals filter needs a field to filter in “field”")
|
|
|
|
|
},
|
|
|
|
|
value: args.remove("value").expect("The Equals filter needs a reference value in “value”")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Error on `Some(Value::Str(s)) => s` even though `args` is mutable and trashed at the end of the function.
|
|
|
|
|
|
|
|
|
|
<hr>
|
|
|
|
|
|
2019-11-17 15:30:48 +01:00
|
|
|
|
## setup
|
|
|
|
|
|
|
|
|
|
In module `modules`, I have:
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
pub trait Module {
|
|
|
|
|
fn run(&self, record: &mut Record) -> Result<bool, ()>;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
In module `filters`, I have:
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
pub struct Equals<'a> { … }
|
|
|
|
|
|
|
|
|
|
impl Equals<'_> {
|
|
|
|
|
pub fn from_args(mut args: ModuleArgs) -> Equals {
|
|
|
|
|
Equals { … }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Module for Equals<'_> {
|
|
|
|
|
fn run(&self, record: &mut Record) -> Result<bool, ()> { … }
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## goal, “naive-style”
|
|
|
|
|
|
|
|
|
|
What I really want (for a start) is that, in module `modules`:
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
struct Available {
|
|
|
|
|
name: &'static str,
|
|
|
|
|
cons: fn(ModuleArgs) -> dyn Module
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const available: (Available) = (
|
|
|
|
|
Available{ name: "equals", cons: move |a| filters::Equals::from_args(a) }
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
with `available` being a tuple of available modules, each having a name and a constructor.
|
|
|
|
|
|
|
|
|
|
## problem
|
|
|
|
|
|
|
|
|
|
The above code gives me two errors:
|
|
|
|
|
|
|
|
|
|
1. one I somewhat understand:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
error[E0277]: the size for values of type `(dyn modules::Module + 'static)` cannot be known at compilation time
|
|
|
|
|
--> src/modules.rs:10:45
|
|
|
|
|
|
|
|
|
|
|
10 | Available{ name: "equals", cons: move |a| filters::Equals::from_args(a) }
|
|
|
|
|
| ^ doesn't have a size known at compile-time
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
2. and one I don’t understand:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
error[E0308]: mismatched types
|
|
|
|
|
--> src/modules.rs:10:45
|
|
|
|
|
|
|
|
|
|
|
10 | Available{ name: "equals", cons: move |a| filters::Equals::from_args(a) }
|
|
|
|
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait modules::Module, found struct `filters::Equals`
|
|
|
|
|
```
|