Skip to main content

ruvector_graph/
types.rs

1//! Core types for graph database
2
3use bincode::{Decode, Encode};
4use serde::{Deserialize, Serialize};
5use std::collections::HashMap;
6
7pub type NodeId = String;
8pub type EdgeId = String;
9
10/// Property value types for graph nodes and edges
11#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Encode, Decode)]
12pub enum PropertyValue {
13    /// Null value
14    Null,
15    /// Boolean value
16    Boolean(bool),
17    /// 64-bit integer
18    Integer(i64),
19    /// 64-bit floating point
20    Float(f64),
21    /// UTF-8 string
22    String(String),
23    /// Array of values
24    Array(Vec<PropertyValue>),
25    /// List of values (alias for Array)
26    List(Vec<PropertyValue>),
27    /// Map of string keys to values
28    Map(HashMap<String, PropertyValue>),
29    /// Contiguous f32 array (e.g., embeddings) — avoids per-element PropertyValue overhead
30    FloatArray(Vec<f32>),
31}
32
33// Convenience constructors for PropertyValue
34impl PropertyValue {
35    /// Create a boolean value
36    pub fn boolean(b: bool) -> Self {
37        PropertyValue::Boolean(b)
38    }
39    /// Create an integer value
40    pub fn integer(i: i64) -> Self {
41        PropertyValue::Integer(i)
42    }
43    /// Create a float value
44    pub fn float(f: f64) -> Self {
45        PropertyValue::Float(f)
46    }
47    /// Create a string value
48    pub fn string(s: impl Into<String>) -> Self {
49        PropertyValue::String(s.into())
50    }
51    /// Create an array value
52    pub fn array(arr: Vec<PropertyValue>) -> Self {
53        PropertyValue::Array(arr)
54    }
55    /// Create a map value
56    pub fn map(m: HashMap<String, PropertyValue>) -> Self {
57        PropertyValue::Map(m)
58    }
59    /// Create a contiguous f32 array (e.g., for embeddings)
60    pub fn float_array(arr: Vec<f32>) -> Self {
61        PropertyValue::FloatArray(arr)
62    }
63}
64
65// From implementations for convenient property value creation
66impl From<bool> for PropertyValue {
67    fn from(b: bool) -> Self {
68        PropertyValue::Boolean(b)
69    }
70}
71
72impl From<i64> for PropertyValue {
73    fn from(i: i64) -> Self {
74        PropertyValue::Integer(i)
75    }
76}
77
78impl From<i32> for PropertyValue {
79    fn from(i: i32) -> Self {
80        PropertyValue::Integer(i as i64)
81    }
82}
83
84impl From<f64> for PropertyValue {
85    fn from(f: f64) -> Self {
86        PropertyValue::Float(f)
87    }
88}
89
90impl From<f32> for PropertyValue {
91    fn from(f: f32) -> Self {
92        PropertyValue::Float(f as f64)
93    }
94}
95
96impl From<String> for PropertyValue {
97    fn from(s: String) -> Self {
98        PropertyValue::String(s)
99    }
100}
101
102impl From<&str> for PropertyValue {
103    fn from(s: &str) -> Self {
104        PropertyValue::String(s.to_string())
105    }
106}
107
108impl<T: Into<PropertyValue>> From<Vec<T>> for PropertyValue {
109    fn from(v: Vec<T>) -> Self {
110        PropertyValue::Array(v.into_iter().map(Into::into).collect())
111    }
112}
113
114impl From<HashMap<String, PropertyValue>> for PropertyValue {
115    fn from(m: HashMap<String, PropertyValue>) -> Self {
116        PropertyValue::Map(m)
117    }
118}
119
120pub type Properties = HashMap<String, PropertyValue>;
121
122#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Encode, Decode)]
123pub struct Label {
124    pub name: String,
125}
126
127impl Label {
128    pub fn new(name: impl Into<String>) -> Self {
129        Self { name: name.into() }
130    }
131}
132
133#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)]
134pub struct RelationType {
135    pub name: String,
136}
137
138impl RelationType {
139    pub fn new(name: impl Into<String>) -> Self {
140        Self { name: name.into() }
141    }
142}