Skip to main content

rudof_rdf/rdf_core/
matcher.rs

1/// A trait for pattern matching against RDF components.
2///
3/// This trait enables flexible matching of RDF terms, subjects, and predicates
4/// in queries and triple patterns. It combines matching logic with value
5/// extraction, allowing both specific matches and wildcard patterns.
6pub trait Matcher<T>: PartialEq<T> {
7    /// Returns the underlying value if this matcher represents a specific value.
8    ///
9    /// Returns `None` for wildcard matchers that match any value,
10    /// and `Some(value)` for matchers that represent a specific RDF component.
11    fn value(&self) -> Option<&T>;
12}
13
14/// A wildcard matcher that matches any RDF component.
15///
16/// `Any` implements the `Matcher` trait to enable pattern matching in SPARQL-like
17/// queries where certain positions in a triple pattern can match any value.
18#[derive(Debug, Clone, Eq)]
19pub struct Any;
20
21impl<T> Matcher<T> for Any {
22    /// Always returns `None` since `Any` matches everything without a specific value.
23    fn value(&self) -> Option<&T> {
24        None
25    }
26}
27
28impl<T> PartialEq<T> for Any {
29    /// Implements equality comparison where `Any` always equals any value.
30    fn eq(&self, _other: &T) -> bool {
31        true
32    }
33}