Skip to main content

rudof_rdf/rdf_core/
async_rdf.rs

1use async_trait::async_trait;
2use std::{collections::HashSet, fmt::Display, hash::Hash};
3
4/// An asynchronous trait for querying RDF graphs.
5///
6/// This trait provides a common interface for performing asynchronous queries
7/// on RDF graphs. It abstracts over the specific representation of RDF components,
8/// allowing implementations to use different backend storage systems or data structures.
9///
10/// # Associated Types
11///
12/// The trait defines several associated types that must be specified by implementors:
13///
14/// - [`Subject`](Self::Subject): Represents an RDF subject (typically an IRI or blank node)
15/// - [`IRI`](Self::IRI): Represents an Internationalized Resource Identifier
16/// - [`BNode`](Self::BNode): Represents a blank node
17/// - [`Literal`](Self::Literal): Represents a literal value (string, number, etc.)
18/// - [`Term`](Self::Term): Represents any RDF term (IRI, blank node, or literal)
19/// - [`Err`](Self::Err): The error type returned by operations
20///
21/// # Thread Safety
22///
23/// All associated types must implement `Sync + Send` to enable safe concurrent access
24/// across thread boundaries, which is essential for asynchronous operations.
25#[async_trait]
26pub trait AsyncRDF {
27    /// The type representing an RDF subject.
28    type Subject: Display + Sync + Send;
29    /// The type representing an Internationalized Resource Identifier (IRI).
30    type IRI: Display + Hash + Eq + Sync + Send;
31    /// The type representing an RDF blank node.
32    type BNode: Display + Sync + Send;
33    /// The type representing an RDF literal value.
34    type Literal: Display + Sync + Send;
35    /// The type representing any RDF term.
36    type Term: Display + Sync + Send;
37    /// The error type returned by trait operations.
38    type Err: Display + Sync + Send;
39
40    /// Retrieves all predicates associated with a given subject.
41    ///
42    /// # Arguments
43    ///
44    /// * `subject` - A reference to the subject whose predicates should be retrieved
45    async fn get_predicates_subject(&self, subject: &Self::Subject) -> Result<HashSet<Self::IRI>, Self::Err>;
46
47    /// Retrieves all objects for a given subject-predicate pair.
48    ///
49    /// # Arguments
50    ///
51    /// * `subject` - A reference to the subject of the triples
52    /// * `pred` - A reference to the predicate (property) of the triples
53    async fn get_objects_for_subject_predicate(
54        &self,
55        subject: &Self::Subject,
56        pred: &Self::IRI,
57    ) -> Result<HashSet<Self::Term>, Self::Err>;
58
59    /// Retrieves all subjects that have a specific object-predicate pair.
60    ///
61    /// # Arguments
62    ///
63    /// * `object` - A reference to the object (term) to search for
64    /// * `pred` - A reference to the predicate (property) to match
65    async fn get_subjects_for_object_predicate(
66        &self,
67        object: &Self::Term,
68        pred: &Self::IRI,
69    ) -> Result<HashSet<Self::Subject>, Self::Err>;
70}