| 1 | use regexrust-analyzer cargo regex 1.13.1 crate/::Regexrust-analyzer cargo regex 1.13.1 regex/string/Regex#; |
| 2 | |
| 3 | /// Filters used by widgets to filter out certain entries. |
| 4 | /// TODO: Move this out maybe? |
| 5 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 6 | pub struct Filterrust-analyzer cargo bottom 0.14.9 app/filter/Filter# { |
| 7 | /// Whether the filter _accepts_ all entries that match `list`, |
| 8 | /// or _denies_ any entries that match it. |
| 9 | is_list_ignoredrust-analyzer cargo bottom 0.14.9 app/filter/Filter#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 | listrust-analyzer cargo bottom 0.14.9 app/filter/Filter#list.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Regexrust-analyzer cargo regex 1.13.1 regex/string/Regex#>, |
| 14 | } |
| 15 | |
| 16 | impl Filterrust-analyzer cargo bottom 0.14.9 app/filter/Filter# { |
| 17 | /// Create a new filter. |
| 18 | #[inline] |
| 19 | pub(craterust-analyzer cargo bottom 0.14.9 crate/) fn newrust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]new().(ignore_matches: bool, list: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Regexrust-analyzer cargo regex 1.13.1 regex/string/Regex#>) -> Selfrust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter] { |
| 20 | Selfrust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter] { |
| 21 | is_list_ignoredrust-analyzer cargo bottom 0.14.9 app/filter/Filter#is_list_ignored.: ignore_matches, |
| 22 | listrust-analyzer cargo bottom 0.14.9 app/filter/Filter#list., |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | /// Whether the filter should keep the entry or reject it. |
| 27 | #[inline] |
| 28 | pub(craterust-analyzer cargo bottom 0.14.9 crate/) fn should_keeprust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]should_keep().(&self, entry: &str) -> bool { |
| 29 | if self.has_matchrust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]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 | !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.is_list_ignoredrust-analyzer cargo bottom 0.14.9 app/filter/Filter#is_list_ignored. |
| 34 | } else { |
| 35 | self.is_list_ignoredrust-analyzer cargo bottom 0.14.9 app/filter/Filter#is_list_ignored. |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /// Whether there is a filter that matches the result. |
| 40 | #[inline] |
| 41 | pub(craterust-analyzer cargo bottom 0.14.9 crate/) fn has_matchrust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]has_match().(&self, value: &str) -> bool { |
| 42 | self.listrust-analyzer cargo bottom 0.14.9 app/filter/Filter#list..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]any().(|regex| regex.is_matchrust-analyzer cargo regex 1.13.1 regex/string/impl#[Regex]is_match().(value)) |
| 43 | } |
| 44 | |
| 45 | /// Whether entries matching the list should be ignored or kept. |
| 46 | #[inline] |
| 47 | pub(craterust-analyzer cargo bottom 0.14.9 crate/) fn ignore_matchesrust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]ignore_matches().(&self) -> bool { |
| 48 | self.is_list_ignoredrust-analyzer cargo bottom 0.14.9 app/filter/Filter#is_list_ignored. |
| 49 | } |
| 50 | |
| 51 | /// Check a filter if it exists, otherwise accept if it is [`None`]. |
| 52 | #[inline] |
| 53 | pub(craterust-analyzer cargo bottom 0.14.9 crate/) fn optional_should_keeprust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]optional_should_keep().(filter: &Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]>, entry: &str) -> bool { |
| 54 | filter |
| 55 | .as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().() |
| 56 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|f| f.should_keeprust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]should_keep().(entry)) |
| 57 | .unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(true) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | #[cfg(test)] |
| 62 | mod testrust-analyzer cargo bottom 0.14.9 app/filter/test/ { |
| 63 | use regexrust-analyzer cargo regex 1.13.1 crate/::Regexrust-analyzer cargo regex 1.13.1 regex/string/Regex#; |
| 64 | |
| 65 | use superrust-analyzer cargo bottom 0.14.9 app/filter/::*; |
| 66 | |
| 67 | /// Quick test for filtering w/ regex. |
| 68 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 69 | fn filter_simplerust-analyzer cargo bottom 0.14.9 app/filter/test/filter_simple().() { |
| 70 | let values = ["eth0slab", "virbr0", "eth0", "wlan0", "lo"]; |
| 71 | |
| 72 | let filter = Filterrust-analyzer cargo bottom 0.14.9 app/filter/Filter# { |
| 73 | is_list_ignoredrust-analyzer cargo bottom 0.14.9 app/filter/Filter#is_list_ignored.: false, |
| 74 | listrust-analyzer cargo bottom 0.14.9 app/filter/Filter#list.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![Regexrust-analyzer cargo regex 1.13.1 regex/string/Regex#::newrust-analyzer cargo regex 1.13.1 regex/string/impl#[Regex]new().("virbr0").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), Regexrust-analyzer cargo regex 1.13.1 regex/string/Regex#::newrust-analyzer cargo regex 1.13.1 regex/string/impl#[Regex]new().("eth0").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()], |
| 75 | }; |
| 76 | |
| 77 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 78 | values |
| 79 | .into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().() |
| 80 | .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|r| filter.should_keeprust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]should_keep().(r)) |
| 81 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>(), |
| 82 | vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["eth0slab", "virbr0", "eth0"] |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | /// Test based on the issue in <https://github.com/ClementTsang/bottom/pull/1037>. |
| 87 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 88 | fn filter_is_list_ignoredrust-analyzer cargo bottom 0.14.9 app/filter/test/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 = Filterrust-analyzer cargo bottom 0.14.9 app/filter/Filter# { |
| 97 | is_list_ignoredrust-analyzer cargo bottom 0.14.9 app/filter/Filter#is_list_ignored.: true, |
| 98 | listrust-analyzer cargo bottom 0.14.9 app/filter/Filter#list.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![Regexrust-analyzer cargo regex 1.13.1 regex/string/Regex#::newrust-analyzer cargo regex 1.13.1 regex/string/impl#[Regex]new().("temperature").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()], |
| 99 | }; |
| 100 | |
| 101 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 102 | values |
| 103 | .into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().() |
| 104 | .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|r| ignore_true.should_keeprust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]should_keep().(r)) |
| 105 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>(), |
| 106 | vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["wifi_0", "amd gpu"] |
| 107 | ); |
| 108 | |
| 109 | let ignore_false = Filterrust-analyzer cargo bottom 0.14.9 app/filter/Filter# { |
| 110 | is_list_ignoredrust-analyzer cargo bottom 0.14.9 app/filter/Filter#is_list_ignored.: false, |
| 111 | listrust-analyzer cargo bottom 0.14.9 app/filter/Filter#list.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![Regexrust-analyzer cargo regex 1.13.1 regex/string/Regex#::newrust-analyzer cargo regex 1.13.1 regex/string/impl#[Regex]new().("temperature").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()], |
| 112 | }; |
| 113 | |
| 114 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 115 | values |
| 116 | .into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().() |
| 117 | .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|r| ignore_false.should_keeprust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]should_keep().(r)) |
| 118 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>(), |
| 119 | vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["CPU socket temperature", "motherboard temperature"] |
| 120 | ); |
| 121 | |
| 122 | let multi_true = Filterrust-analyzer cargo bottom 0.14.9 app/filter/Filter# { |
| 123 | is_list_ignoredrust-analyzer cargo bottom 0.14.9 app/filter/Filter#is_list_ignored.: true, |
| 124 | listrust-analyzer cargo bottom 0.14.9 app/filter/Filter#list.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![ |
| 125 | Regexrust-analyzer cargo regex 1.13.1 regex/string/Regex#::newrust-analyzer cargo regex 1.13.1 regex/string/impl#[Regex]new().("socket").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), |
| 126 | Regexrust-analyzer cargo regex 1.13.1 regex/string/Regex#::newrust-analyzer cargo regex 1.13.1 regex/string/impl#[Regex]new().("temperature").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), |
| 127 | ], |
| 128 | }; |
| 129 | |
| 130 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 131 | values |
| 132 | .into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().() |
| 133 | .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|r| multi_true.should_keeprust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]should_keep().(r)) |
| 134 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>(), |
| 135 | vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["wifi_0", "amd gpu"] |
| 136 | ); |
| 137 | |
| 138 | let multi_false = Filterrust-analyzer cargo bottom 0.14.9 app/filter/Filter# { |
| 139 | is_list_ignoredrust-analyzer cargo bottom 0.14.9 app/filter/Filter#is_list_ignored.: false, |
| 140 | listrust-analyzer cargo bottom 0.14.9 app/filter/Filter#list.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![ |
| 141 | Regexrust-analyzer cargo regex 1.13.1 regex/string/Regex#::newrust-analyzer cargo regex 1.13.1 regex/string/impl#[Regex]new().("socket").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), |
| 142 | Regexrust-analyzer cargo regex 1.13.1 regex/string/Regex#::newrust-analyzer cargo regex 1.13.1 regex/string/impl#[Regex]new().("temperature").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), |
| 143 | ], |
| 144 | }; |
| 145 | |
| 146 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 147 | values |
| 148 | .into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().() |
| 149 | .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|r| multi_false.should_keeprust-analyzer cargo bottom 0.14.9 app/filter/impl#[Filter]should_keep().(r)) |
| 150 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>(), |
| 151 | vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["CPU socket temperature", "motherboard temperature"] |
| 152 | ); |
| 153 | } |
| 154 | } |