Skip to main content

graphrecords_query/capabilities/numeric/
cube_root.rs

1use crate::{
2    Failure, IndexValue, QueryResult, Scalar, ValueDomain, error::numeric::NonNumericValue,
3};
4use graphrecords_core::graphrecord::GraphRecordValue;
5
6pub trait ValueCubeRoot: ValueDomain {
7    fn cube_root<'a>(label: &'static str, value: Self::Value<'a>) -> QueryResult<Self::Value<'a>>;
8}
9
10impl ValueCubeRoot for Scalar {
11    fn cube_root<'a>(label: &'static str, value: Self::Value<'a>) -> QueryResult<Self::Value<'a>> {
12        match value {
13            GraphRecordValue::Int(integer) => Ok(GraphRecordValue::Float((integer as f64).cbrt())),
14            GraphRecordValue::Float(float) => Ok(GraphRecordValue::Float(float.cbrt())),
15            value => Err(Failure::new(label, NonNumericValue::new(value))),
16        }
17    }
18}
19
20impl ValueCubeRoot for IndexValue<GraphRecordValue> {
21    fn cube_root<'a>(label: &'static str, value: Self::Value<'a>) -> QueryResult<Self::Value<'a>> {
22        match value {
23            GraphRecordValue::Int(integer) => Ok(GraphRecordValue::Float((integer as f64).cbrt())),
24            GraphRecordValue::Float(float) => Ok(GraphRecordValue::Float(float.cbrt())),
25            value => Err(Failure::new(label, NonNumericValue::new(value))),
26        }
27    }
28}