Skip to main content

reddb_server/storage/unified/devx/
conversions.rs

1//! Value Conversions for Ergonomics
2//!
3//! From implementations for Value and MetadataValue for fluent API.
4
5use super::super::{MetadataValue, RefTarget};
6use super::refs::{NodeRef, TableRef, VectorRef};
7use crate::storage::schema::Value;
8
9// ============================================================================
10// Value Conversions
11// ============================================================================
12
13impl From<&str> for Value {
14    fn from(s: &str) -> Self {
15        Value::text(s.to_string())
16    }
17}
18
19impl From<String> for Value {
20    fn from(s: String) -> Self {
21        Value::text(s)
22    }
23}
24
25impl From<i32> for Value {
26    fn from(n: i32) -> Self {
27        Value::Integer(n as i64)
28    }
29}
30
31impl From<i64> for Value {
32    fn from(n: i64) -> Self {
33        Value::Integer(n)
34    }
35}
36
37impl From<f32> for Value {
38    fn from(n: f32) -> Self {
39        Value::Float(n as f64)
40    }
41}
42
43impl From<f64> for Value {
44    fn from(n: f64) -> Self {
45        Value::Float(n)
46    }
47}
48
49impl From<bool> for Value {
50    fn from(b: bool) -> Self {
51        Value::Boolean(b)
52    }
53}
54
55// ============================================================================
56// MetadataValue Conversions
57// ============================================================================
58
59impl From<&str> for MetadataValue {
60    fn from(s: &str) -> Self {
61        MetadataValue::String(s.to_string())
62    }
63}
64
65impl From<String> for MetadataValue {
66    fn from(s: String) -> Self {
67        MetadataValue::String(s)
68    }
69}
70
71impl From<i64> for MetadataValue {
72    fn from(n: i64) -> Self {
73        MetadataValue::Int(n)
74    }
75}
76
77impl From<f64> for MetadataValue {
78    fn from(n: f64) -> Self {
79        MetadataValue::Float(n)
80    }
81}
82
83impl From<bool> for MetadataValue {
84    fn from(b: bool) -> Self {
85        MetadataValue::Bool(b)
86    }
87}
88
89impl From<RefTarget> for MetadataValue {
90    fn from(r: RefTarget) -> Self {
91        MetadataValue::Reference(r)
92    }
93}
94
95impl From<Vec<RefTarget>> for MetadataValue {
96    fn from(refs: Vec<RefTarget>) -> Self {
97        MetadataValue::References(refs)
98    }
99}
100
101impl From<TableRef> for MetadataValue {
102    fn from(r: TableRef) -> Self {
103        MetadataValue::Reference(RefTarget::table(r.table, r.row_id))
104    }
105}
106
107impl From<NodeRef> for MetadataValue {
108    fn from(r: NodeRef) -> Self {
109        MetadataValue::Reference(RefTarget::node(r.collection, r.node_id))
110    }
111}
112
113impl From<VectorRef> for MetadataValue {
114    fn from(r: VectorRef) -> Self {
115        MetadataValue::Reference(RefTarget::vector(r.collection, r.vector_id))
116    }
117}