Skip to main content

graphrecords_query/index/
key.rs

1use crate::{
2    AttributeName, Failure, FailureKind, QueryResult,
3    index::{EntityDomain, ExpandedIndex, ExpandedIndexReference, IndexDomain, Positional},
4};
5use graphrecords_core::{
6    GraphRecord,
7    graphrecord::{EdgeIndex, GraphRecordValue, NodeIndex},
8};
9
10pub trait GroupKey: IndexDomain {
11    fn resolve_key<'a>(
12        label: &'static str,
13        graphrecord: &'a GraphRecord,
14        key: &Self::Owned,
15    ) -> QueryResult<Self::Index<'a>>;
16}
17
18impl GroupKey for Positional {
19    fn resolve_key<'a>(
20        _label: &'static str,
21        _graphrecord: &'a GraphRecord,
22        key: &Self::Owned,
23    ) -> QueryResult<Self::Index<'a>> {
24        Ok(*key)
25    }
26}
27
28impl GroupKey for EdgeIndex {
29    fn resolve_key<'a>(
30        label: &'static str,
31        graphrecord: &'a GraphRecord,
32        key: &Self::Owned,
33    ) -> QueryResult<Self::Index<'a>> {
34        Self::resolve_index(graphrecord, key)
35            .map_err(|error| Failure::new_at::<Self, _>(label, error, &Self::from_owned(key)))
36    }
37}
38
39impl GroupKey for NodeIndex {
40    fn resolve_key<'a>(
41        label: &'static str,
42        graphrecord: &'a GraphRecord,
43        key: &Self::Owned,
44    ) -> QueryResult<Self::Index<'a>> {
45        Self::resolve_index(graphrecord, key)
46            .map_err(|error| Failure::new_at::<Self, _>(label, error, &Self::from_owned(key)))
47    }
48}
49
50impl GroupKey for FailureKind {
51    fn resolve_key<'a>(
52        _label: &'static str,
53        _graphrecord: &'a GraphRecord,
54        key: &Self::Owned,
55    ) -> QueryResult<Self::Index<'a>> {
56        Ok(*key)
57    }
58}
59
60impl GroupKey for GraphRecordValue {
61    fn resolve_key<'a>(
62        _label: &'static str,
63        _graphrecord: &'a GraphRecord,
64        key: &Self::Owned,
65    ) -> QueryResult<Self::Index<'a>> {
66        Ok(key.clone())
67    }
68}
69
70impl GroupKey for AttributeName {
71    fn resolve_key<'a>(
72        _label: &'static str,
73        _graphrecord: &'a GraphRecord,
74        key: &Self::Owned,
75    ) -> QueryResult<Self::Index<'a>> {
76        Ok(key.clone())
77    }
78}
79
80impl GroupKey for bool {
81    fn resolve_key<'a>(
82        _label: &'static str,
83        _graphrecord: &'a GraphRecord,
84        key: &Self::Owned,
85    ) -> QueryResult<Self::Index<'a>> {
86        Ok(*key)
87    }
88}
89
90impl<P: GroupKey, C: GroupKey> GroupKey for ExpandedIndex<P, C> {
91    fn resolve_key<'a>(
92        label: &'static str,
93        graphrecord: &'a GraphRecord,
94        key: &Self::Owned,
95    ) -> QueryResult<Self::Index<'a>> {
96        let parent = P::resolve_key(label, graphrecord, key.parent_index())?;
97
98        match key.child_index() {
99            None => Ok(ExpandedIndexReference::source(parent)),
100            Some(child) => Ok(ExpandedIndexReference::child(
101                parent,
102                C::resolve_key(label, graphrecord, child)?,
103            )),
104        }
105    }
106}