matrix_sdk_ui/room_list_service/filters/
not.rs

1// Copyright 2024 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::ops::Not;
16
17use super::{BoxedFilterFn, Filter};
18
19/// Create a new filter that will negate the inner filter. It returns `false` if
20/// the inner filter returns `true`, otherwise it returns `true`.
21pub fn new_filter(filter: BoxedFilterFn) -> impl Filter {
22    move |room| -> bool { filter(room).not() }
23}
24
25#[cfg(test)]
26mod tests {
27    use std::ops::Not;
28
29    use matrix_sdk::test_utils::logged_in_client_with_server;
30    use matrix_sdk_test::async_test;
31    use ruma::room_id;
32
33    use super::{super::new_rooms, *};
34
35    #[async_test]
36    async fn test_true() {
37        let (client, server) = logged_in_client_with_server().await;
38        let [room] = new_rooms([room_id!("!a:b.c")], &client, &server).await;
39
40        let filter = Box::new(|_: &_| true);
41        let not = new_filter(filter);
42
43        assert!(not(&room).not());
44    }
45
46    #[async_test]
47    async fn test_false() {
48        let (client, server) = logged_in_client_with_server().await;
49        let [room] = new_rooms([room_id!("!a:b.c")], &client, &server).await;
50
51        let filter = Box::new(|_: &_| false);
52        let not = new_filter(filter);
53
54        assert!(not(&room));
55    }
56}