Last active
August 16, 2016 22:03
-
-
Save tobz/87b803977d52686107f6f8716407b7d8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
error[E0308]: mismatched types | |
--> src/filter.rs:26:37 | |
| | |
26 | FilterApplicator { filters: filters } | |
| ^^^^^^^ expected fn pointer, found fn item | |
| | |
= note: expected type `std::vec::Vec<(fn(&card::Card, &std::string::String) -> bool, std::string::String)>` | |
= note: found type `std::vec::Vec<(fn(&card::Card, &std::string::String) -> bool {filters::filter_text}, std::string::String)>` | |
error[E0308]: mismatched types | |
--> src/filter.rs:52:54 | |
| | |
52 | let applicator = FilterApplicator { filters: filters }; | |
| ^^^^^^^ expected fn pointer, found fn item | |
| | |
= note: expected type `std::vec::Vec<(fn(&card::Card, &std::string::String) -> bool, std::string::String)>` | |
= note: found type `std::vec::Vec<(fn(&card::Card, &std::string::String) -> bool {filter::test::always_match_filter}, std::string::String)>` | |
error[E0308]: mismatched types | |
--> src/filter.rs:62:54 | |
| | |
62 | let applicator = FilterApplicator { filters: filters }; | |
| ^^^^^^^ expected fn pointer, found fn item | |
| | |
= note: expected type `std::vec::Vec<(fn(&card::Card, &std::string::String) -> bool, std::string::String)>` | |
= note: found type `std::vec::Vec<(fn(&card::Card, &std::string::String) -> bool {filter::test::never_match_filter}, std::string::String)>` | |
error[E0308]: mismatched types | |
--> src/filter.rs:71:23 | |
| | |
71 | filters.push((never_match_filter, "".to_string())); | |
| ^^^^^^^^^^^^^^^^^^ expected fn item, found a different fn item | |
| | |
= note: expected type `fn(&card::Card, &std::string::String) -> bool {filter::test::always_match_filter}` | |
= note: found type `fn(&card::Card, &std::string::String) -> bool {filter::test::never_match_filter}` | |
error[E0308]: mismatched types | |
--> src/filter.rs:73:54 | |
| | |
73 | let applicator = FilterApplicator { filters: filters }; | |
| ^^^^^^^ expected fn pointer, found fn item | |
| | |
= note: expected type `std::vec::Vec<(fn(&card::Card, &std::string::String) -> bool, std::string::String)>` | |
= note: found type `std::vec::Vec<(fn(&card::Card, &std::string::String) -> bool {filter::test::always_match_filter}, std::string::String)>` | |
error: aborting due to 5 previous errors |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
use card::Card; | |
use filters as defined_filters; | |
#[derive(Serialize, Deserialize, Debug)] | |
struct FilterCriteria { | |
filters: HashMap<String, String> | |
} | |
struct FilterApplicator { | |
filters: Vec<(fn(&Card, &String) -> bool, String)> | |
} | |
impl FilterApplicator { | |
pub fn from_criteria(criteria: FilterCriteria) -> FilterApplicator { | |
let filters = Vec::new(); | |
for (filter_name, filter_value) in criteria.filters { | |
let filter_fn = match filter_name.as_str() { | |
"text" => defined_filters::filter_text, | |
_ => panic!("unknown filter type '{}'", filter_name) | |
}; | |
filters.push((filter_fn, filter_value)) | |
} | |
FilterApplicator { filters: filters } | |
} | |
pub fn apply_to_target(&self, target: &Card) -> bool { | |
self.filters.iter().any(|&(filter_fn, filter_options)| filter_fn(target, &filter_options) ) | |
} | |
} | |
#[cfg(test)] | |
mod test { | |
use super::FilterApplicator; | |
use card::Card; | |
fn always_match_filter(_item: &Card, _value: &String) -> bool { | |
true | |
} | |
fn never_match_filter(_item: &Card, _value: &String) -> bool { | |
false | |
} | |
#[test] | |
fn test_single_positive_filter() { | |
let mut filters = Vec::new(); | |
filters.push((always_match_filter, "".to_string())); | |
let applicator = FilterApplicator { filters: filters }; | |
let dummy_item = Card::new(); | |
assert!(applicator.apply_to_target(&dummy_item)) | |
} | |
#[test] | |
fn test_single_negative_filter() { | |
let mut filters = Vec::new(); | |
filters.push((never_match_filter, "".to_string())); | |
let applicator = FilterApplicator { filters: filters }; | |
let dummy_item = Card::new(); | |
assert!(!applicator.apply_to_target(&dummy_item)) | |
} | |
#[test] | |
fn test_multiple_filters() { | |
let mut filters = Vec::new(); | |
filters.push((always_match_filter, "".to_string())); | |
filters.push((never_match_filter, "".to_string())); | |
let applicator = FilterApplicator { filters: filters }; | |
let dummy_item = Card::new(); | |
assert!(applicator.apply_to_target(&dummy_item)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment