Skip to main content

graphrecords_query/capabilities/
grouping.rs

1use crate::{
2    AttributeName, EntityDomain, EntityReference, FailureKind, FailureKindValue, IndexDomain,
3    IndexValue, Mask, Scalar, ValueDomain, index::GroupKey,
4};
5use graphrecords_core::graphrecord::GraphRecordValue;
6
7#[diagnostic::on_unimplemented(
8    message = "`{Self}` values cannot be used as grouping keys",
9    note = "implement `GroupingValue` for `{Self}` to give it a group key domain"
10)]
11pub trait GroupingValue: ValueDomain {
12    type Key: GroupKey;
13
14    fn to_group_key(value: &Self::Value<'_>) -> <Self::Key as IndexDomain>::Owned;
15}
16
17impl GroupingValue for Scalar {
18    type Key = GraphRecordValue;
19
20    fn to_group_key(value: &Self::Value<'_>) -> <Self::Key as IndexDomain>::Owned {
21        value.clone()
22    }
23}
24
25impl GroupingValue for Mask {
26    type Key = bool;
27
28    fn to_group_key(value: &Self::Value<'_>) -> <Self::Key as IndexDomain>::Owned {
29        *value
30    }
31}
32
33impl GroupingValue for AttributeName {
34    type Key = Self;
35
36    fn to_group_key(value: &Self::Value<'_>) -> <Self::Key as IndexDomain>::Owned {
37        value.clone()
38    }
39}
40
41impl GroupingValue for FailureKindValue {
42    type Key = FailureKind;
43
44    fn to_group_key(value: &Self::Value<'_>) -> <Self::Key as IndexDomain>::Owned {
45        *value
46    }
47}
48
49impl<I: GroupKey> GroupingValue for IndexValue<I> {
50    type Key = I;
51
52    fn to_group_key(value: &Self::Value<'_>) -> <Self::Key as IndexDomain>::Owned {
53        value.clone()
54    }
55}
56
57impl<E: EntityDomain + GroupKey> GroupingValue for EntityReference<E> {
58    type Key = E;
59
60    fn to_group_key(value: &Self::Value<'_>) -> <Self::Key as IndexDomain>::Owned {
61        E::to_owned(value)
62    }
63}