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
47
48
49
50
use crate::{
    core::entities::{edges::edge_store::EdgeStore, LayerIds},
    db::api::view::internal::Base,
};
use std::sync::Arc;

pub fn extend_filter(
    old: Option<EdgeFilter>,
    filter: impl Fn(&EdgeStore, &LayerIds) -> bool + Send + Sync + 'static,
) -> EdgeFilter {
    match old {
        Some(f) => Arc::new(move |e, l| f(e, l) && filter(e, l)),
        None => Arc::new(filter),
    }
}

pub type EdgeFilter = Arc<dyn Fn(&EdgeStore, &LayerIds) -> bool + Send + Sync>;

pub trait EdgeFilterOps {
    /// Return the optional edge filter for the graph

    fn edge_filter(&self) -> Option<&EdgeFilter>;
}

pub trait InheritEdgeFilterOps: Base {}

impl<G: InheritEdgeFilterOps> DelegateEdgeFilterOps for G
where
    G::Base: EdgeFilterOps,
{
    type Internal = G::Base;

    #[inline]
    fn graph(&self) -> &Self::Internal {
        self.base()
    }
}

pub trait DelegateEdgeFilterOps {
    type Internal: EdgeFilterOps + ?Sized;

    fn graph(&self) -> &Self::Internal;
}

impl<G: DelegateEdgeFilterOps> EdgeFilterOps for G {
    #[inline]
    fn edge_filter(&self) -> Option<&EdgeFilter> {
        self.graph().edge_filter()
    }
}