| 1 | use regex::Regex; |
| 2 | |
| 3 | /// Filters used by widgets to filter out certain entries. |
| 4 | /// TODO: Move this out maybe? |
| 5 | #[derive(Debug, Clone)] |
| 6 | pub struct Filter { |
| 7 | /// Whether the filter _accepts_ all entries that match `list`, |
| 8 | /// or _denies_ any entries that match it. |
| 9 | is_list_ignored: bool, // TODO: Maybe change to "ignore_matches"? |
| 10 | |
| 11 | /// The list of regexes to match against. Whether it goes through |
| 12 | /// the filter or not depends on `is_list_ignored`. |
| 13 | list: Vec<Regex>, |
| 14 | } |
| 15 | |
| 16 | impl Filter { |
| 17 | /// Create a new filter. |
| 18 | #[inline] |
| 19 | pub(crate) fn new(ignore_matches: bool, list: Vec<Regex>) -> Self { |
| 20 | Self { |
| 21 | is_list_ignored: ignore_matches, |
| 22 | list, |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | /// Whether the filter should keep the entry or reject it. |
| 27 | #[inline] |
| 28 | pub(crate) fn should_keep(&self, entry: &str) -> bool { |
| 29 | if self.has_match(entry) { |
| 30 | // If a match is found, then if we wanted to ignore if we match, return false. |
| 31 | // If we want to keep if we match, return true. Thus, return the |
| 32 | // inverse of `is_list_ignored`. |
| 33 | !self.is_list_ignored |
| 34 | } else { |
| 35 | self.is_list_ignored |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /// Whether there is a filter that matches the result. |
| 40 | #[inline] |
| 41 | pub(crate) fn has_match(&self, value: &str) -> bool { |
| 42 | self.list.iter().any(|regex| regex.is_match(value)) |
| 43 | } |
| 44 | |
| 45 | /// Whether entries matching the list should be ignored or kept. |
| 46 | #[inline] |
| 47 | pub(crate) fn ignore_matches(&self) -> bool { |
| 48 | self.is_list_ignored |
| 49 | } |
| 50 | |
| 51 | /// Check a filter if it exists, otherwise accept if it is [`None`]. |
| 52 | #[inline] |
| 53 | pub(crate) fn optional_should_keep(filter: &Option<Self>, entry: &str) -> bool { |
| 54 | filter |
| 55 | .as_ref() |
| 56 | .map(|f| f.should_keep(entry)) |
| 57 | .unwrap_or(true) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | #[cfg(test)] |
| 62 | mod test { |
| 63 | use regex::Regex; |
| 64 | |
| 65 | use super::*; |
| 66 | |
| 67 | /// Quick test for filtering w/ regex. |
| 68 | #[test] |
| 69 | fn filter_simple() { |
| 70 | let values = ["eth0slab", "virbr0", "eth0", "wlan0", "lo"]; |
| 71 | |
| 72 | let filter = Filter { |
| 73 | is_list_ignored: false, |
| 74 | list: vec![Regex::new("virbr0").unwrap(), Regex::new("eth0").unwrap()], |
| 75 | }; |
| 76 | |
| 77 | assert_eq!( |
| 78 | values |
| 79 | .into_iter() |
| 80 | .filter(|r| filter.should_keep(r)) |
| 81 | .collect::<Vec<_>>(), |
| 82 | vec!["eth0slab", "virbr0", "eth0"] |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | /// Test based on the issue in <https://github.com/ClementTsang/bottom/pull/1037>. |
| 87 | #[test] |
| 88 | fn filter_is_list_ignored() { |
| 89 | let values = [ |
| 90 | "CPU socket temperature", |
| 91 | "wifi_0", |
| 92 | "motherboard temperature", |
| 93 | "amd gpu", |
| 94 | ]; |
| 95 | |
| 96 | let ignore_true = Filter { |
| 97 | is_list_ignored: true, |
| 98 | list: vec![Regex::new("temperature").unwrap()], |
| 99 | }; |
| 100 | |
| 101 | assert_eq!( |
| 102 | values |
| 103 | .into_iter() |
| 104 | .filter(|r| ignore_true.should_keep(r)) |
| 105 | .collect::<Vec<_>>(), |
| 106 | vec!["wifi_0", "amd gpu"] |
| 107 | ); |
| 108 | |
| 109 | let ignore_false = Filter { |
| 110 | is_list_ignored: false, |
| 111 | list: vec![Regex::new("temperature").unwrap()], |
| 112 | }; |
| 113 | |
| 114 | assert_eq!( |
| 115 | values |
| 116 | .into_iter() |
| 117 | .filter(|r| ignore_false.should_keep(r)) |
| 118 | .collect::<Vec<_>>(), |
| 119 | vec!["CPU socket temperature", "motherboard temperature"] |
| 120 | ); |
| 121 | |
| 122 | let multi_true = Filter { |
| 123 | is_list_ignored: true, |
| 124 | list: vec![ |
| 125 | Regex::new("socket").unwrap(), |
| 126 | Regex::new("temperature").unwrap(), |
| 127 | ], |
| 128 | }; |
| 129 | |
| 130 | assert_eq!( |
| 131 | values |
| 132 | .into_iter() |
| 133 | .filter(|r| multi_true.should_keep(r)) |
| 134 | .collect::<Vec<_>>(), |
| 135 | vec!["wifi_0", "amd gpu"] |
| 136 | ); |
| 137 | |
| 138 | let multi_false = Filter { |
| 139 | is_list_ignored: false, |
| 140 | list: vec![ |
| 141 | Regex::new("socket").unwrap(), |
| 142 | Regex::new("temperature").unwrap(), |
| 143 | ], |
| 144 | }; |
| 145 | |
| 146 | assert_eq!( |
| 147 | values |
| 148 | .into_iter() |
| 149 | .filter(|r| multi_false.should_keep(r)) |
| 150 | .collect::<Vec<_>>(), |
| 151 | vec!["CPU socket temperature", "motherboard temperature"] |
| 152 | ); |
| 153 | } |
| 154 | } |