My goal is to create a Table struct that looks like
Table {
data: vec![
Data {
id: 1,
name: "John".to_string(),
currency: Currency::Usd,
},
Data {
id: 2,
name: "Mary".to_string(),
currency: Currency::Aud,
},
],
columns: vec![id, name, currency],
}
So there are two rows of data and the 3 columns on each.
The columns could have a filter but it is optionally.
// I don't need a filter
let id = Column::new("Id", Field::Id);
// I need a filter that is String
let name = Column::new("Name", Field::Name).filter(Filter {
list: vec!["John".to_string()],
onfilter: |name: String, data: Data| -> bool { data.name == name },
});
// I need a filter that is an enum
let currency = Column::new("Currency", Field::Currency).filter(Filter {
list: vec![Currency::Usd, Currency::Aud],
onfilter: |currency: Currency, data: Data| -> bool { data.currency == currency },
});
Problem 1
However, due to the nature of the filters(have different types), I cannot get it to work. I considered using trait object(Vec<Box<dyn WHAT?>>
) but I don't know what type to use.
error[E0308]: mismatched types
--> src/main.rs:113:33
|
91 | onfilter: |name: String, data: Data| -> bool { data.name == name },
| ---------------------------------- the expected closure
...
97 | onfilter: |currency: Currency, data: Data| -> bool { data.currency == currency },
| ---------------------------------------- the found closure
...
113 | columns: vec![id, name, currency],
| ^^^^^^^^ expected `String`, found `Currency`
|
= note: expected struct `Column<&_, _, Html::Filter<Vec<String>, {closure@src/main.rs:91:19: 91:53}>>`
found struct `Column<&_, _, Html::Filter<Vec<Currency>, {closure@src/main.rs:97:19: 97:59}>>`
Problem 2
Because my Filter
struct looks like
pub struct Filter<L, F> {
// A list of value to choose from, for example
// let list = vec!["USD", "AUD"];
list: L,
// A closure to apply the filter,
// FnMut() -> bool {}
onfilter: F,
}
When I don't specify a filter(the filter is optional and not needed) to a column, I need to annotate it. Is there way to get way with it?
error[E0282]: type annotations needed for `Column<&str, Field, _>`
--> src/main.rs:86:9
|
86 | let id = Column::new("Id", Field::Id);
| ^^ ---------------------------- type must be known at this point
|
help: consider giving `id` an explicit type, where the type for type parameter `L` is specified
|
86 | let id: Column<&str, Field, L> = Column::new("Id", Field::Id);
| ++++++++++++++++++++++++