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}
30
31// Convenience constructors for PropertyValue
32impl PropertyValue {
33    /// Create a boolean value
34    pub fn boolean(b: bool) -> Self {
35        PropertyValue::Boolean(b)
36    }
37    /// Create an integer value
38    pub fn integer(i: i64) -> Self {
39        PropertyValue::Integer(i)
40    }
41    /// Create a float value
42    pub fn float(f: f64) -> Self {
43        PropertyValue::Float(f)
44    }
45    /// Create a string value
46    pub fn string(s: impl Into<String>) -> Self {
47        PropertyValue::String(s.into())
48    }
49    /// Create an array value
50    pub fn array(arr: Vec<PropertyValue>) -> Self {
51        PropertyValue::Array(arr)
52    }
53    /// Create a map value
54    pub fn map(m: HashMap<String, PropertyValue>) -> Self {
55        PropertyValue::Map(m)
56    }
57}
58
59// From implementations for convenient property value creation
60impl From<bool> for PropertyValue {
61    fn from(b: bool) -> Self {
62        PropertyValue::Boolean(b)
63    }
64}
65
66impl From<i64> for PropertyValue {
67    fn from(i: i64) -> Self {
68        PropertyValue::Integer(i)
69    }
70}
71
72impl From<i32> for PropertyValue {
73    fn from(i: i32) -> Self {
74        PropertyValue::Integer(i as i64)
75    }
76}
77
78impl From<f64> for PropertyValue {
79    fn from(f: f64) -> Self {
80        PropertyValue::Float(f)
81    }
82}
83
84impl From<f32> for PropertyValue {
85    fn from(f: f32) -> Self {
86        PropertyValue::Float(f as f64)
87    }
88}
89
90impl From<String> for PropertyValue {
91    fn from(s: String) -> Self {
92        PropertyValue::String(s)
93    }
94}
95
96impl From<&str> for PropertyValue {
97    fn from(s: &str) -> Self {
98        PropertyValue::String(s.to_string())
99    }
100}
101
102impl<T: Into<PropertyValue>> From<Vec<T>> for PropertyValue {
103    fn from(v: Vec<T>) -> Self {
104        PropertyValue::Array(v.into_iter().map(Into::into).collect())
105    }
106}
107
108impl From<HashMap<String, PropertyValue>> for PropertyValue {
109    fn from(m: HashMap<String, PropertyValue>) -> Self {
110        PropertyValue::Map(m)
111    }
112}
113
114pub type Properties = HashMap<String, PropertyValue>;
115
116#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Encode, Decode)]
117pub struct Label {
118    pub name: String,
119}
120
121impl Label {
122    pub fn new(name: impl Into<String>) -> Self {
123        Self { name: name.into() }
124    }
125}
126
127#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)]
128pub struct RelationType {
129    pub name: String,
130}
131
132impl RelationType {
133    pub fn new(name: impl Into<String>) -> Self {
134        Self { name: name.into() }
135    }
136}