Skip to main content

graphrecords_query/
value.rs

1use crate::{EntityDomain, Failure, FailureKind, IndexDomain};
2use graphrecords_core::graphrecord::{GraphRecordAttribute, GraphRecordValue};
3use std::marker::PhantomData;
4
5pub trait ValueDomain: 'static {
6    type Owned: 'static + Clone;
7
8    type Value<'a>: 'a + Clone
9    where
10        Self: 'a;
11
12    fn into_owned(value: Self::Value<'_>) -> Self::Owned;
13
14    fn from_owned(owned: &Self::Owned) -> Self::Value<'_>;
15}
16
17pub trait BareValueDomain: ValueDomain {}
18
19pub trait ReturnValueDomain: ValueDomain {}
20
21pub struct Scalar;
22pub struct Mask;
23#[derive(Clone)]
24pub struct AttributeName;
25pub struct Unit;
26pub struct IndexValue<I: IndexDomain>(PhantomData<I>);
27pub struct EntityReference<E: EntityDomain>(PhantomData<E>);
28pub struct FailureValue;
29pub struct FailureKindValue;
30
31impl ValueDomain for Scalar {
32    type Owned = GraphRecordValue;
33    type Value<'a> = GraphRecordValue;
34
35    fn into_owned(value: Self::Value<'_>) -> Self::Owned {
36        value
37    }
38
39    fn from_owned(owned: &Self::Owned) -> Self::Value<'_> {
40        owned.clone()
41    }
42}
43impl ValueDomain for Mask {
44    type Owned = bool;
45    type Value<'a> = bool;
46
47    fn into_owned(value: Self::Value<'_>) -> Self::Owned {
48        value
49    }
50
51    fn from_owned(owned: &Self::Owned) -> Self::Value<'_> {
52        *owned
53    }
54}
55impl ValueDomain for AttributeName {
56    type Owned = GraphRecordAttribute;
57    type Value<'a> = GraphRecordAttribute;
58
59    fn into_owned(value: Self::Value<'_>) -> Self::Owned {
60        value
61    }
62
63    fn from_owned(owned: &Self::Owned) -> Self::Value<'_> {
64        owned.clone()
65    }
66}
67impl ValueDomain for Unit {
68    type Owned = ();
69    type Value<'a> = ();
70
71    fn into_owned(_value: Self::Value<'_>) -> Self::Owned {}
72
73    fn from_owned(_owned: &Self::Owned) -> Self::Value<'_> {}
74}
75impl<I: IndexDomain> ValueDomain for IndexValue<I> {
76    type Owned = I::Owned;
77    type Value<'a> = I::Owned;
78
79    fn into_owned(value: Self::Value<'_>) -> Self::Owned {
80        value
81    }
82
83    fn from_owned(owned: &Self::Owned) -> Self::Value<'_> {
84        owned.clone()
85    }
86}
87impl<E: EntityDomain> ValueDomain for EntityReference<E> {
88    type Owned = E::Owned;
89    type Value<'a> = E::Index<'a>;
90
91    fn into_owned(value: Self::Value<'_>) -> Self::Owned {
92        E::to_owned(&value)
93    }
94
95    fn from_owned(owned: &Self::Owned) -> Self::Value<'_> {
96        E::from_owned(owned)
97    }
98}
99impl ValueDomain for FailureValue {
100    type Owned = Failure;
101    type Value<'a> = Failure;
102
103    fn into_owned(value: Self::Value<'_>) -> Self::Owned {
104        value
105    }
106
107    fn from_owned(owned: &Self::Owned) -> Self::Value<'_> {
108        owned.clone()
109    }
110}
111impl ValueDomain for FailureKindValue {
112    type Owned = FailureKind;
113    type Value<'a> = FailureKind;
114
115    fn into_owned(value: Self::Value<'_>) -> Self::Owned {
116        value
117    }
118
119    fn from_owned(owned: &Self::Owned) -> Self::Value<'_> {
120        *owned
121    }
122}
123
124impl BareValueDomain for Scalar {}
125impl BareValueDomain for Mask {}
126impl BareValueDomain for AttributeName {}
127impl<I: IndexDomain> BareValueDomain for IndexValue<I> {}
128impl<E: EntityDomain> BareValueDomain for EntityReference<E> {}
129impl BareValueDomain for FailureValue {}
130impl BareValueDomain for FailureKindValue {}
131
132impl ReturnValueDomain for Scalar {}
133impl ReturnValueDomain for Mask {}
134impl ReturnValueDomain for AttributeName {}
135impl<I: IndexDomain> ReturnValueDomain for IndexValue<I> {}
136impl<E: EntityDomain> ReturnValueDomain for EntityReference<E> {}
137impl ReturnValueDomain for FailureValue {}
138impl ReturnValueDomain for FailureKindValue {}