Skip to main content

graphrecords_query/capabilities/numeric/
absolute.rs

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