orbtk_utils/
filter.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/// Used to filter stuff such as the `on_changed` callback.
#[derive(Debug, Clone, PartialEq)]
pub enum Filter {
    // Everting will be filtered. No element will be available.
    Complete,

    // Nothing will be filtered, all elements will be available.
    Nothing,

    /// Define a list of filtered element.
    List(Vec<String>),
}

impl From<&str> for Filter {
    fn from(s: &str) -> Self {
        match s {
            "nothing" | "Nothing" => Filter::Nothing,
            _ => Filter::Complete,
        }
    }
}

impl From<String> for Filter {
    fn from(s: String) -> Self {
        Filter::from(s.as_str())
    }
}

impl From<Vec<String>> for Filter {
    fn from(v: Vec<String>) -> Self {
        Filter::List(v)
    }
}

impl From<Vec<&str>> for Filter {
    fn from(v: Vec<&str>) -> Self {
        let vec: Vec<String> = v.iter().map(|s| s.to_string()).collect();
        Filter::from(vec)
    }
}

impl Default for Filter {
    fn default() -> Self {
        Filter::Complete
    }
}