1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//! Dynamic value type for document fields and SQL parameters.
//!
//! Covers the value types needed for AI agent workloads: strings, numbers,
//! booleans, binary blobs (embeddings), arrays, and nested objects.
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use crate::datetime::{NdbDateTime, NdbDuration};
use crate::geometry::Geometry;
/// A dynamic value that can represent any field type in a document
/// or any parameter in a SQL query.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Value {
/// SQL NULL / missing value.
Null,
/// Boolean.
Bool(bool),
/// Signed 64-bit integer.
Integer(i64),
/// 64-bit floating point.
Float(f64),
/// UTF-8 string.
String(String),
/// Raw bytes (embeddings, serialized blobs).
Bytes(Vec<u8>),
/// Ordered array of values.
Array(Vec<Value>),
/// Nested key-value object.
Object(HashMap<String, Value>),
/// UUID (any version, stored as 36-char hyphenated string).
Uuid(String),
/// ULID (26-char Crockford Base32).
Ulid(String),
/// UTC timestamp with microsecond precision.
DateTime(NdbDateTime),
/// Duration with microsecond precision (signed).
Duration(NdbDuration),
/// Arbitrary-precision decimal (financial calculations, exact arithmetic).
Decimal(rust_decimal::Decimal),
/// GeoJSON-compatible geometry (Point, LineString, Polygon, etc.).
Geometry(Geometry),
/// Ordered set of unique values (auto-deduplicated, maintains insertion order).
Set(Vec<Value>),
/// Compiled regex pattern (stored as pattern string).
Regex(String),
/// A range of values with optional bounds.
Range {
/// Start bound (None = unbounded).
start: Option<Box<Value>>,
/// End bound (None = unbounded).
end: Option<Box<Value>>,
/// Whether the end bound is inclusive (`..=` vs `..`).
inclusive: bool,
},
/// A typed reference to another record: `table:id`.
Record {
/// The table/collection name.
table: String,
/// The record's document ID.
id: String,
},
}
impl Value {
/// Returns true if this value is `Null`.
pub fn is_null(&self) -> bool {
matches!(self, Value::Null)
}
/// Try to extract as a string reference.
pub fn as_str(&self) -> Option<&str> {
match self {
Value::String(s) => Some(s),
_ => None,
}
}
/// Try to extract as i64.
pub fn as_i64(&self) -> Option<i64> {
match self {
Value::Integer(i) => Some(*i),
_ => None,
}
}
/// Try to extract as f64.
pub fn as_f64(&self) -> Option<f64> {
match self {
Value::Float(f) => Some(*f),
Value::Integer(i) => Some(*i as f64),
_ => None,
}
}
/// Try to extract as bool.
pub fn as_bool(&self) -> Option<bool> {
match self {
Value::Bool(b) => Some(*b),
_ => None,
}
}
/// Try to extract as byte slice (for embeddings).
pub fn as_bytes(&self) -> Option<&[u8]> {
match self {
Value::Bytes(b) => Some(b),
_ => None,
}
}
/// Try to extract as UUID string.
pub fn as_uuid(&self) -> Option<&str> {
match self {
Value::Uuid(s) => Some(s),
_ => None,
}
}
/// Try to extract as ULID string.
pub fn as_ulid(&self) -> Option<&str> {
match self {
Value::Ulid(s) => Some(s),
_ => None,
}
}
/// Try to extract as DateTime.
pub fn as_datetime(&self) -> Option<&NdbDateTime> {
match self {
Value::DateTime(dt) => Some(dt),
_ => None,
}
}
/// Try to extract as Duration.
pub fn as_duration(&self) -> Option<&NdbDuration> {
match self {
Value::Duration(d) => Some(d),
_ => None,
}
}
/// Try to extract as Decimal.
pub fn as_decimal(&self) -> Option<&rust_decimal::Decimal> {
match self {
Value::Decimal(d) => Some(d),
_ => None,
}
}
/// Try to extract as Geometry.
pub fn as_geometry(&self) -> Option<&Geometry> {
match self {
Value::Geometry(g) => Some(g),
_ => None,
}
}
/// Try to extract as a set (deduplicated array).
pub fn as_set(&self) -> Option<&[Value]> {
match self {
Value::Set(s) => Some(s),
_ => None,
}
}
/// Try to extract as regex pattern string.
pub fn as_regex(&self) -> Option<&str> {
match self {
Value::Regex(r) => Some(r),
_ => None,
}
}
/// Try to extract as a record reference (table, id).
pub fn as_record(&self) -> Option<(&str, &str)> {
match self {
Value::Record { table, id } => Some((table, id)),
_ => None,
}
}
/// Return the type name of this value as a string.
pub fn type_name(&self) -> &'static str {
match self {
Value::Null => "null",
Value::Bool(_) => "bool",
Value::Integer(_) => "int",
Value::Float(_) => "float",
Value::String(_) => "string",
Value::Bytes(_) => "bytes",
Value::Array(_) => "array",
Value::Object(_) => "object",
Value::Uuid(_) => "uuid",
Value::Ulid(_) => "ulid",
Value::DateTime(_) => "datetime",
Value::Duration(_) => "duration",
Value::Decimal(_) => "decimal",
Value::Geometry(_) => "geometry",
Value::Set(_) => "set",
Value::Regex(_) => "regex",
Value::Range { .. } => "range",
Value::Record { .. } => "record",
}
}
}
/// Convenience conversions.
impl From<&str> for Value {
fn from(s: &str) -> Self {
Value::String(s.to_owned())
}
}
impl From<String> for Value {
fn from(s: String) -> Self {
Value::String(s)
}
}
impl From<i64> for Value {
fn from(i: i64) -> Self {
Value::Integer(i)
}
}
impl From<f64> for Value {
fn from(f: f64) -> Self {
Value::Float(f)
}
}
impl From<bool> for Value {
fn from(b: bool) -> Self {
Value::Bool(b)
}
}
impl From<Vec<u8>> for Value {
fn from(b: Vec<u8>) -> Self {
Value::Bytes(b)
}
}
impl From<NdbDateTime> for Value {
fn from(dt: NdbDateTime) -> Self {
Value::DateTime(dt)
}
}
impl From<NdbDuration> for Value {
fn from(d: NdbDuration) -> Self {
Value::Duration(d)
}
}
impl From<rust_decimal::Decimal> for Value {
fn from(d: rust_decimal::Decimal) -> Self {
Value::Decimal(d)
}
}
impl From<Geometry> for Value {
fn from(g: Geometry) -> Self {
Value::Geometry(g)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn value_type_checks() {
assert!(Value::Null.is_null());
assert!(!Value::Bool(true).is_null());
assert_eq!(Value::String("hi".into()).as_str(), Some("hi"));
assert_eq!(Value::Integer(42).as_i64(), Some(42));
assert_eq!(Value::Float(2.78).as_f64(), Some(2.78));
assert_eq!(Value::Integer(10).as_f64(), Some(10.0));
assert_eq!(Value::Bool(true).as_bool(), Some(true));
assert_eq!(Value::Bytes(vec![1, 2]).as_bytes(), Some(&[1, 2][..]));
}
#[test]
fn from_conversions() {
let s: Value = "hello".into();
assert_eq!(s.as_str(), Some("hello"));
let i: Value = 42i64.into();
assert_eq!(i.as_i64(), Some(42));
let f: Value = 2.78f64.into();
assert_eq!(f.as_f64(), Some(2.78));
}
#[test]
fn nested_value() {
let nested = Value::Object({
let mut m = HashMap::new();
m.insert(
"inner".into(),
Value::Array(vec![Value::Integer(1), Value::Integer(2)]),
);
m
});
assert!(!nested.is_null());
}
}