Skip to main content

graphrecords_query/index/
mod.rs

1mod edge_endpoint;
2mod entity;
3mod expanded;
4mod key;
5
6use crate::{AttributeName, FailureKind};
7pub use edge_endpoint::EdgeEndpointRole;
8pub use entity::{EntityAttributes, IndicesInGroup};
9pub use expanded::{ExpandedChild, ExpandedIndex, ExpandedIndexOwned, ExpandedIndexReference};
10use graphrecords_core::{
11    GraphRecord,
12    errors::GraphRecordResult,
13    graphrecord::{EdgeIndex, GraphRecordAttribute, GraphRecordValue, NodeIndex},
14};
15pub use key::GroupKey;
16use std::{
17    any::Any,
18    fmt::{Debug, Display},
19    hash::Hash,
20};
21
22pub type Position = usize;
23
24pub trait OwnedIndex: Any + Debug + Display + Send + Sync {}
25
26impl<T: Any + Debug + Display + Send + Sync> OwnedIndex for T {}
27
28pub trait IndexDomain: 'static + Clone {
29    type Owned: 'static + Clone + Eq + Hash + OwnedIndex;
30
31    type Index<'a>: Clone + Eq + Hash
32    where
33        Self: 'a;
34
35    fn to_owned(index: &Self::Index<'_>) -> Self::Owned;
36
37    fn from_owned(owned: &Self::Owned) -> Self::Index<'_>;
38}
39
40pub trait EntityDomain: IndexDomain {
41    fn resolve_index<'a>(
42        graphrecord: &'a GraphRecord,
43        index: &Self::Owned,
44    ) -> GraphRecordResult<Self::Index<'a>>;
45}
46
47#[derive(Clone, Debug)]
48pub struct Positional;
49
50impl IndexDomain for Positional {
51    type Index<'a> = Position;
52    type Owned = Position;
53
54    fn to_owned(index: &Self::Index<'_>) -> Self::Owned {
55        *index
56    }
57
58    fn from_owned(owned: &Self::Owned) -> Self::Index<'_> {
59        *owned
60    }
61}
62
63impl IndexDomain for EdgeIndex {
64    type Index<'a> = &'a Self;
65    type Owned = Self;
66
67    fn to_owned(index: &Self::Index<'_>) -> Self::Owned {
68        **index
69    }
70
71    fn from_owned(owned: &Self::Owned) -> Self::Index<'_> {
72        owned
73    }
74}
75
76impl IndexDomain for NodeIndex {
77    type Index<'a> = &'a Self;
78    type Owned = Self;
79
80    fn to_owned(index: &Self::Index<'_>) -> Self::Owned {
81        (*index).clone()
82    }
83
84    fn from_owned(owned: &Self::Owned) -> Self::Index<'_> {
85        owned
86    }
87}
88
89impl IndexDomain for FailureKind {
90    type Index<'a> = Self;
91    type Owned = Self;
92
93    fn to_owned(index: &Self::Index<'_>) -> Self::Owned {
94        *index
95    }
96
97    fn from_owned(owned: &Self::Owned) -> Self::Index<'_> {
98        *owned
99    }
100}
101
102impl IndexDomain for GraphRecordValue {
103    type Index<'a> = Self;
104    type Owned = Self;
105
106    fn to_owned(index: &Self::Index<'_>) -> Self::Owned {
107        index.clone()
108    }
109
110    fn from_owned(owned: &Self::Owned) -> Self::Index<'_> {
111        owned.clone()
112    }
113}
114
115impl IndexDomain for AttributeName {
116    type Index<'a> = GraphRecordAttribute;
117    type Owned = GraphRecordAttribute;
118
119    fn to_owned(index: &Self::Index<'_>) -> Self::Owned {
120        index.clone()
121    }
122
123    fn from_owned(owned: &Self::Owned) -> Self::Index<'_> {
124        owned.clone()
125    }
126}
127
128impl IndexDomain for bool {
129    type Index<'a> = Self;
130    type Owned = Self;
131
132    fn to_owned(index: &Self::Index<'_>) -> Self::Owned {
133        *index
134    }
135
136    fn from_owned(owned: &Self::Owned) -> Self::Index<'_> {
137        *owned
138    }
139}
140
141impl EntityDomain for EdgeIndex {
142    fn resolve_index<'a>(
143        graphrecord: &'a GraphRecord,
144        index: &Self::Owned,
145    ) -> GraphRecordResult<Self::Index<'a>> {
146        graphrecord.resolve_edge_index(index)
147    }
148}
149
150impl EntityDomain for NodeIndex {
151    fn resolve_index<'a>(
152        graphrecord: &'a GraphRecord,
153        index: &Self::Owned,
154    ) -> GraphRecordResult<Self::Index<'a>> {
155        graphrecord.resolve_node_index(index)
156    }
157}