graph_api_lib/search/
edge.rs

1use crate::Direction;
2use crate::element::Element;
3
4/// A search to apply to edges when querying a graph.
5/// This allows graph implementations to support vertex centric indexes.
6/// In the future, this will be expanded to support more complex filters than just label and direction.
7#[non_exhaustive]
8pub struct EdgeSearch<'search, Graph>
9where
10    Graph: crate::Graph,
11{
12    // Futureproof: edges may eventually be indexable
13    _phantom: std::marker::PhantomData<&'search ()>,
14
15    /// the label of the edges
16    pub label: Option<<Graph::Edge as Element>::Label>,
17
18    /// The required adjacent label
19    pub adjacent_label: Option<<Graph::Vertex as Element>::Label>,
20
21    /// The direction of the edge to match.
22    pub direction: Direction,
23
24    /// The maximum number of edges to return for the current vertex
25    pub limit: Option<usize>,
26}
27
28impl<Graph> Clone for EdgeSearch<'_, Graph>
29where
30    Graph: crate::Graph,
31{
32    fn clone(&self) -> Self {
33        EdgeSearch {
34            _phantom: Default::default(),
35            label: self.label,
36            adjacent_label: self.adjacent_label,
37            direction: self.direction,
38            limit: self.limit,
39        }
40    }
41}
42
43impl<Graph> Default for EdgeSearch<'_, Graph>
44where
45    Graph: crate::Graph,
46{
47    fn default() -> Self {
48        Self {
49            _phantom: Default::default(),
50            label: None,
51            adjacent_label: None,
52            direction: Direction::All,
53            limit: None,
54        }
55    }
56}
57
58impl<Graph> EdgeSearch<'_, Graph>
59where
60    Graph: crate::Graph,
61{
62    /// Creates a new edge search with default settings.
63    ///
64    /// This creates an edge search that will match all edges, regardless of label,
65    /// adjacent label, or direction.
66    ///
67    /// # Returns
68    /// A new EdgeSearch with default settings.
69    pub fn scan() -> Self {
70        Self::default()
71    }
72
73    /// Edges must match the label
74    pub fn label(label: <Graph::Edge as Element>::Label) -> Self {
75        Self {
76            _phantom: Default::default(),
77            label: Some(label),
78            adjacent_label: None,
79            direction: Direction::All,
80            limit: None,
81        }
82    }
83
84    /// Outgoing edges
85    pub fn outgoing(mut self) -> Self {
86        self.direction = Direction::Outgoing;
87        self
88    }
89
90    /// Outgoing edges
91    pub fn incoming(mut self) -> Self {
92        self.direction = Direction::Incoming;
93        self
94    }
95
96    /// The direction of the edges relative to the starting vertex
97    pub fn direction(mut self, direction: Direction) -> Self {
98        self.direction = direction;
99        self
100    }
101
102    /// The maximum number of edges to return relative to the starting vertex.
103    ///
104    /// The name `take` follows Rust's standard library's naming conventions.
105    pub fn take(mut self, n: usize) -> Self {
106        self.limit = Some(n);
107        self
108    }
109
110    /// The maximum number of edges to return relative to the starting vertex.
111    pub fn with_limit(mut self, limit: usize) -> Self {
112        self.limit = Some(limit);
113        self
114    }
115
116    /// Returns the maximum number of edges to return from this search.
117    ///
118    /// If no limit was set, returns usize::MAX (effectively no limit).
119    ///
120    /// # Returns
121    /// The edge limit, or usize::MAX if no limit was set
122    pub fn limit(&self) -> usize {
123        self.limit.unwrap_or(usize::MAX)
124    }
125
126    /// Adjacent vertex label must match
127    pub fn adjacent_labelled(mut self, adjacent_label: <Graph::Vertex as Element>::Label) -> Self
128    where
129        Graph: crate::Graph + crate::SupportsEdgeAdjacentLabelIndex,
130    {
131        self.adjacent_label = Some(adjacent_label);
132        self
133    }
134}