graphrecords_query/capabilities/
string.rs1use crate::{
2 AttributeName, Failure, IndexValue, QueryResult, Scalar, ValueDomain,
3 error::string::NonStringValue,
4};
5use graphrecords_core::graphrecord::{GraphRecordAttribute, GraphRecordValue, NodeIndex};
6
7pub trait StringValue: ValueDomain {
8 fn into_string(label: &'static str, value: Self::Value<'_>) -> QueryResult<String>;
9
10 fn from_string<'a>(role: &Self::Value<'_>, value: String) -> Self::Value<'a>;
11}
12
13fn string_from_value(label: &'static str, value: GraphRecordValue) -> QueryResult<String> {
14 match value {
15 GraphRecordValue::String(value) => Ok(value),
16 value => Err(Failure::new(label, NonStringValue::new(value))),
17 }
18}
19
20fn string_from_attribute(label: &'static str, value: GraphRecordAttribute) -> QueryResult<String> {
21 match value {
22 GraphRecordAttribute::String(value) => Ok(value),
23 value @ GraphRecordAttribute::Int(_) => {
24 Err(Failure::new(label, NonStringValue::new(value)))
25 }
26 }
27}
28
29impl StringValue for Scalar {
30 fn into_string(label: &'static str, value: Self::Value<'_>) -> QueryResult<String> {
31 string_from_value(label, value)
32 }
33
34 fn from_string<'a>(_role: &Self::Value<'_>, value: String) -> Self::Value<'a> {
35 GraphRecordValue::String(value)
36 }
37}
38
39impl StringValue for IndexValue<GraphRecordValue> {
40 fn into_string(label: &'static str, value: Self::Value<'_>) -> QueryResult<String> {
41 string_from_value(label, value)
42 }
43
44 fn from_string<'a>(_role: &Self::Value<'_>, value: String) -> Self::Value<'a> {
45 GraphRecordValue::String(value)
46 }
47}
48
49impl StringValue for AttributeName {
50 fn into_string(label: &'static str, value: Self::Value<'_>) -> QueryResult<String> {
51 string_from_attribute(label, value)
52 }
53
54 fn from_string<'a>(_role: &Self::Value<'_>, value: String) -> Self::Value<'a> {
55 GraphRecordAttribute::String(value)
56 }
57}
58
59impl StringValue for IndexValue<NodeIndex> {
60 fn into_string(label: &'static str, value: Self::Value<'_>) -> QueryResult<String> {
61 string_from_attribute(label, value)
62 }
63
64 fn from_string<'a>(_role: &Self::Value<'_>, value: String) -> Self::Value<'a> {
65 GraphRecordAttribute::String(value)
66 }
67}
68
69impl StringValue for IndexValue<AttributeName> {
70 fn into_string(label: &'static str, value: Self::Value<'_>) -> QueryResult<String> {
71 string_from_attribute(label, value)
72 }
73
74 fn from_string<'a>(_role: &Self::Value<'_>, value: String) -> Self::Value<'a> {
75 GraphRecordAttribute::String(value)
76 }
77}