Skip to main content

graphrecords_query/capabilities/
integer.rs

1use crate::{
2    AttributeName, Failure, IndexValue, Positional, QueryResult, Scalar, ValueDomain,
3    error::numeric::{IntegerOverflow, NonIntegerValue},
4};
5use graphrecords_core::graphrecord::{
6    EdgeIndex, GraphRecordAttribute, GraphRecordValue, NodeIndex,
7};
8
9pub trait IntValue: ValueDomain {
10    fn into_int(label: &'static str, value: Self::Value<'_>) -> QueryResult<i64>;
11}
12
13fn int_from_value(label: &'static str, value: GraphRecordValue) -> QueryResult<i64> {
14    match value {
15        GraphRecordValue::Int(value) => Ok(value),
16        value => Err(Failure::new(label, NonIntegerValue::new(value))),
17    }
18}
19
20fn int_from_attribute(label: &'static str, value: GraphRecordAttribute) -> QueryResult<i64> {
21    match value {
22        GraphRecordAttribute::Int(value) => Ok(value),
23        value @ GraphRecordAttribute::String(_) => {
24            Err(Failure::new(label, NonIntegerValue::new(value)))
25        }
26    }
27}
28
29impl IntValue for Scalar {
30    fn into_int(label: &'static str, value: Self::Value<'_>) -> QueryResult<i64> {
31        int_from_value(label, value)
32    }
33}
34
35impl IntValue for IndexValue<GraphRecordValue> {
36    fn into_int(label: &'static str, value: Self::Value<'_>) -> QueryResult<i64> {
37        int_from_value(label, value)
38    }
39}
40
41impl IntValue for AttributeName {
42    fn into_int(label: &'static str, value: Self::Value<'_>) -> QueryResult<i64> {
43        int_from_attribute(label, value)
44    }
45}
46
47impl IntValue for IndexValue<NodeIndex> {
48    fn into_int(label: &'static str, value: Self::Value<'_>) -> QueryResult<i64> {
49        int_from_attribute(label, value)
50    }
51}
52
53impl IntValue for IndexValue<AttributeName> {
54    fn into_int(label: &'static str, value: Self::Value<'_>) -> QueryResult<i64> {
55        int_from_attribute(label, value)
56    }
57}
58
59impl IntValue for IndexValue<EdgeIndex> {
60    fn into_int(_label: &'static str, value: Self::Value<'_>) -> QueryResult<i64> {
61        Ok(i64::from(value))
62    }
63}
64
65impl IntValue for IndexValue<Positional> {
66    fn into_int(label: &'static str, value: Self::Value<'_>) -> QueryResult<i64> {
67        i64::try_from(value).map_err(|_| Failure::new(label, IntegerOverflow::new(value)))
68    }
69}