sophia_api/term/matcher/
_graph_name_matcher.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use super::*;

/// Generic trait for matching [`GraphName`]s.
pub trait GraphNameMatcher {
    /// The type of term that this GraphNameMatcher contains
    type Term: Term + ?Sized;

    /// Check whether this matcher matches `t`.
    fn matches<T2: Term + ?Sized>(&self, graph_name: GraphName<&T2>) -> bool;

    /// Return `None`, unless this matcher can only match a single graph name,
    /// in which case this method may return that graph name.
    ///
    /// This method is provided for optimization purposes,
    /// so implementing it is optional.
    fn constant(&self) -> Option<GraphName<&Self::Term>> {
        None
    }

    /// Return a [`GraphNameMatcher`] that is actually just a reference to this one.
    fn matcher_ref(&self) -> MatcherRef<'_, Self> {
        MatcherRef(self)
    }
}

impl<T> GraphNameMatcher for Option<Option<T>>
where
    T: Term,
{
    type Term = T;

    fn matches<T2: Term + ?Sized>(&self, graph_name: GraphName<&T2>) -> bool {
        match self {
            Some(mine) => graph_name_eq(
                mine.as_ref().map(|gn| gn.borrow_term()),
                graph_name.map(|gn| gn.borrow_term()),
            ),
            None => false,
        }
    }
    fn constant(&self) -> Option<GraphName<&Self::Term>> {
        self.as_ref().map(GraphName::as_ref)
    }
}

impl<T, const N: usize> GraphNameMatcher for [GraphName<T>; N]
where
    T: Term,
{
    type Term = T;

    fn matches<T2: Term + ?Sized>(&self, graph_name: GraphName<&T2>) -> bool {
        self.iter().any(|mine| {
            graph_name_eq(
                mine.as_ref().map(|gn| gn.borrow_term()),
                graph_name.map(|gn| gn.borrow_term()),
            )
        })
    }
    fn constant(&self) -> Option<GraphName<&Self::Term>> {
        if N == 1 {
            Some(self[0].as_ref())
        } else {
            None
        }
    }
}

impl<T> GraphNameMatcher for &[GraphName<T>]
where
    T: Term,
{
    type Term = T;

    fn matches<T2: Term + ?Sized>(&self, graph_name: GraphName<&T2>) -> bool {
        self.iter().any(|mine| {
            graph_name_eq(
                mine.as_ref().map(|gn| gn.borrow_term()),
                graph_name.map(|gn| gn.borrow_term()),
            )
        })
    }
    fn constant(&self) -> Option<GraphName<&Self::Term>> {
        if self.len() == 1 {
            Some(self[0].as_ref())
        } else {
            None
        }
    }
}

impl GraphNameMatcher for Option<TermKind> {
    type Term = SimpleTerm<'static>; // not actually used

    fn matches<T2: Term + ?Sized>(&self, graph_name: GraphName<&T2>) -> bool {
        graph_name.map(Term::kind) == *self
    }
}

/// Matches only embedded triple whose components match the corresponding matchers.
impl<S, P, O> GraphNameMatcher for Option<(S, P, O)>
where
    S: TermMatcher,
    P: TermMatcher,
    O: TermMatcher,
{
    type Term = S::Term; // not actually used

    fn matches<T2: Term + ?Sized>(&self, graph_name: Option<&T2>) -> bool {
        match (self, graph_name.map(Term::triple)) {
            (None, None) => true,
            (Some((sm, pm, om)), Some(Some(t))) => {
                t.matched_by(sm.matcher_ref(), pm.matcher_ref(), om.matcher_ref())
            }
            _ => false,
        }
    }
}

impl<F> GraphNameMatcher for F
where
    F: Fn(GraphName<SimpleTerm>) -> bool + ?Sized,
{
    type Term = SimpleTerm<'static>; // not actually used

    fn matches<T2: Term + ?Sized>(&self, graph_name: GraphName<&T2>) -> bool {
        (self)(graph_name.map(Term::as_simple))
    }
}

impl GraphNameMatcher for Any {
    type Term = SimpleTerm<'static>;

    fn matches<T2: Term + ?Sized>(&self, _: GraphName<&T2>) -> bool {
        true
    }
}