manifold_graph/
integration.rs

1//! Integration traits for external graph algorithm libraries.
2
3use crate::{AllEdgesIter, Edge, GraphTableRead};
4use manifold::StorageError;
5
6/// Trait for edge sources consumable by graph algorithm libraries.
7///
8/// This trait enables external graph algorithm libraries (BFS, DFS, `PageRank`, etc.)
9/// to efficiently iterate over all edges in the graph.
10pub trait EdgeSource {
11    /// Iterator type over edges
12    type Iter<'a>: Iterator<Item = Result<Edge, StorageError>>
13    where
14        Self: 'a;
15
16    /// Returns an iterator over all edges in the graph.
17    ///
18    /// The iterator provides access to all edges with their properties.
19    fn all_edges(&self) -> Result<Self::Iter<'_>, StorageError>;
20
21    /// Returns the number of edges.
22    fn edge_count(&self) -> Result<u64, StorageError>;
23
24    /// Returns true if empty.
25    fn is_empty(&self) -> Result<bool, StorageError> {
26        Ok(self.edge_count()? == 0)
27    }
28}
29
30impl EdgeSource for GraphTableRead {
31    type Iter<'a>
32        = AllEdgesIter<'a>
33    where
34        Self: 'a;
35
36    fn all_edges(&self) -> Result<Self::Iter<'_>, StorageError> {
37        GraphTableRead::all_edges(self)
38    }
39
40    fn edge_count(&self) -> Result<u64, StorageError> {
41        self.len()
42    }
43}