graph_api_lib/search/
vertex.rs

1use crate::element::Element;
2use crate::{Label, Value, ValueRange};
3use derivative::Derivative;
4use std::ops::Range;
5
6/// A search to apply to vertices when querying a graph.
7#[derive(Derivative)]
8#[derivative(Clone(bound = ""))]
9#[non_exhaustive]
10pub enum VertexSearch<'search, Graph>
11where
12    Graph: crate::Graph,
13{
14    Scan {
15        limit: Option<usize>,
16    },
17    Label {
18        label: <Graph::Vertex as Element>::Label,
19        limit: Option<usize>,
20    },
21    Index {
22        index: <<Graph::Vertex as Element>::Label as Label>::Index,
23        value: Value<'search>,
24        limit: Option<usize>,
25    },
26    Range {
27        index: <<Graph::Vertex as Element>::Label as Label>::Index,
28        range: Range<Value<'search>>,
29        limit: Option<usize>,
30    },
31    FullText {
32        index: <<Graph::Vertex as Element>::Label as Label>::Index,
33        search: Value<'search>,
34        limit: Option<usize>,
35    },
36}
37
38impl<'search, Graph> VertexSearch<'search, Graph>
39where
40    Graph: crate::Graph,
41{
42    pub fn scan() -> Self {
43        Self::Scan { limit: None }
44    }
45
46    pub fn label(label: <Graph::Vertex as Element>::Label) -> Self
47    where
48        Graph: crate::Graph + crate::SupportsVertexLabelIndex,
49    {
50        Self::Label { label, limit: None }
51    }
52
53    pub fn get<V>(index: <<Graph::Vertex as Element>::Label as Label>::Index, value: V) -> Self
54    where
55        V: Into<Value<'search>>,
56        Graph: crate::Graph + crate::SupportsVertexHashIndex,
57    {
58        Self::Index {
59            index,
60            value: value.into(),
61            limit: None,
62        }
63    }
64
65    pub fn range<R>(index: <<Graph::Vertex as Element>::Label as Label>::Index, range: R) -> Self
66    where
67        R: Into<ValueRange<'search>>,
68        Graph: crate::Graph + crate::SupportsVertexRangeIndex,
69    {
70        Self::Range {
71            index,
72            range: range.into().0,
73            limit: None,
74        }
75    }
76
77    pub fn full_text(
78        index: <<Graph::Vertex as Element>::Label as Label>::Index,
79        search: &'search str,
80    ) -> Self
81    where
82        Graph: crate::Graph + crate::SupportsVertexFullTextIndex,
83    {
84        Self::FullText {
85            index,
86            search: Value::Str(search),
87            limit: None,
88        }
89    }
90
91    /// The maximum number of vertices to return from this search
92    pub fn with_limit(mut self, new_limit: usize) -> Self {
93        match &mut self {
94            VertexSearch::Scan { limit } => *limit = Some(new_limit),
95            VertexSearch::Label { limit, .. } => *limit = Some(new_limit),
96            VertexSearch::Index { limit, .. } => *limit = Some(new_limit),
97            VertexSearch::Range { limit, .. } => *limit = Some(new_limit),
98            VertexSearch::FullText { limit, .. } => *limit = Some(new_limit),
99        }
100
101        self
102    }
103
104    /// Returns the maximum number of vertices to return from this search.
105    ///
106    /// If no limit was set, returns usize::MAX (effectively no limit).
107    ///
108    /// # Returns
109    /// The vertex limit, or usize::MAX if no limit was set
110    pub fn limit(&self) -> usize {
111        match self {
112            VertexSearch::Scan { limit, .. } => limit,
113            VertexSearch::Label { limit, .. } => limit,
114            VertexSearch::Index { limit, .. } => limit,
115            VertexSearch::Range { limit, .. } => limit,
116            VertexSearch::FullText { limit, .. } => limit,
117        }
118        .unwrap_or(usize::MAX)
119    }
120}