Skip to main content

graphrecords_query/capabilities/numeric/
negate.rs

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