surreal-client 0.4.3

CBOR-based SurrealDB client for the Vantage data framework
Documentation
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::fmt;

/// Represents a SurrealDB record ID
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct RecordId {
    pub table: String,
    pub id: RecordIdValue,
}

/// The value part of a record ID
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(untagged)]
pub enum RecordIdValue {
    String(String),
    Number(i64),
    Object(Value),
    Array(Vec<Value>),
}

impl RecordId {
    /// Create a new record ID
    pub fn new(table: impl Into<String>, id: impl Into<RecordIdValue>) -> Self {
        Self {
            table: table.into(),
            id: id.into(),
        }
    }

    /// Create a record ID with a string ID
    pub fn string(table: impl Into<String>, id: impl Into<String>) -> Self {
        Self::new(table, RecordIdValue::String(id.into()))
    }

    /// Create a record ID with a numeric ID
    pub fn number(table: impl Into<String>, id: i64) -> Self {
        Self::new(table, RecordIdValue::Number(id))
    }

    /// Create a record ID with an object ID
    pub fn object(table: impl Into<String>, id: Value) -> Self {
        Self::new(table, RecordIdValue::Object(id))
    }

    /// Create a record ID with an array ID
    pub fn array(table: impl Into<String>, id: Vec<Value>) -> Self {
        Self::new(table, RecordIdValue::Array(id))
    }

    /// Parse a record ID from a string format "table:id"
    pub fn parse(input: &str) -> Result<Self, RecordParseError> {
        let parts: Vec<&str> = input.splitn(2, ':').collect();
        if parts.len() != 2 {
            return Err(RecordParseError::InvalidFormat);
        }

        let table = parts[0].to_string();
        let id_str = parts[1];

        // Try to parse as number first
        if let Ok(num) = id_str.parse::<i64>() {
            return Ok(Self::number(table, num));
        }

        // Try to parse as JSON object/array
        if (id_str.starts_with('{') || id_str.starts_with('['))
            && let Ok(value) = serde_json::from_str::<Value>(id_str)
        {
            match value {
                Value::Object(_) => return Ok(Self::object(table, value)),
                Value::Array(arr) => return Ok(Self::array(table, arr)),
                _ => {}
            }
        }

        // Default to string
        Ok(Self::string(table, id_str))
    }

    /// Convert to SurrealQL string representation
    pub fn to_surql(&self) -> String {
        let table = escape_identifier(&self.table);
        let id = match &self.id {
            RecordIdValue::String(s) => escape_identifier(s),
            RecordIdValue::Number(n) => n.to_string(),
            RecordIdValue::Object(obj) => obj.to_string(),
            RecordIdValue::Array(arr) => {
                format!(
                    "[{}]",
                    arr.iter()
                        .map(|v| v.to_string())
                        .collect::<Vec<_>>()
                        .join(", ")
                )
            }
        };
        format!("{}:{}", table, id)
    }

    /// Get the table name
    pub fn table(&self) -> &str {
        &self.table
    }

    /// Get the ID value
    pub fn id(&self) -> &RecordIdValue {
        &self.id
    }
}

impl fmt::Display for RecordId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.to_surql())
    }
}

impl From<&str> for RecordId {
    fn from(s: &str) -> Self {
        Self::parse(s).unwrap_or_else(|_| Self::string("unknown", s))
    }
}

impl From<String> for RecordId {
    fn from(s: String) -> Self {
        Self::from(s.as_str())
    }
}

impl From<RecordId> for Value {
    fn from(record_id: RecordId) -> Self {
        Value::String(record_id.to_string())
    }
}

impl From<&RecordId> for Value {
    fn from(record_id: &RecordId) -> Self {
        Value::String(record_id.to_string())
    }
}

impl From<String> for RecordIdValue {
    fn from(s: String) -> Self {
        RecordIdValue::String(s)
    }
}

impl From<&str> for RecordIdValue {
    fn from(s: &str) -> Self {
        RecordIdValue::String(s.to_string())
    }
}

impl From<i64> for RecordIdValue {
    fn from(n: i64) -> Self {
        RecordIdValue::Number(n)
    }
}

impl From<Value> for RecordIdValue {
    fn from(v: Value) -> Self {
        match v {
            Value::String(s) => RecordIdValue::String(s),
            Value::Number(n) => {
                if let Some(i) = n.as_i64() {
                    RecordIdValue::Number(i)
                } else {
                    RecordIdValue::Object(Value::Number(n))
                }
            }
            Value::Array(arr) => RecordIdValue::Array(arr),
            other => RecordIdValue::Object(other),
        }
    }
}

/// Table reference for queries
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Table {
    pub name: String,
}

impl Table {
    /// Create a new table reference
    pub fn new(name: impl Into<String>) -> Self {
        Self { name: name.into() }
    }

    /// Get the table name
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Convert to SurrealQL string representation
    pub fn to_surql(&self) -> String {
        escape_identifier(&self.name)
    }
}

impl fmt::Display for Table {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.to_surql())
    }
}

impl From<&str> for Table {
    fn from(s: &str) -> Self {
        Self::new(s)
    }
}

impl From<String> for Table {
    fn from(s: String) -> Self {
        Self::new(s)
    }
}

impl From<Table> for Value {
    fn from(table: Table) -> Self {
        Value::String(table.name)
    }
}

impl From<&Table> for Value {
    fn from(table: &Table) -> Self {
        Value::String(table.name.clone())
    }
}

/// Record range for selecting multiple records
#[derive(Debug, Clone, PartialEq)]
pub struct RecordRange {
    pub table: String,
    pub start: Option<RecordIdValue>,
    pub end: Option<RecordIdValue>,
    pub start_inclusive: bool,
    pub end_inclusive: bool,
}

impl RecordRange {
    /// Create a new record range
    pub fn new(table: impl Into<String>) -> Self {
        Self {
            table: table.into(),
            start: None,
            end: None,
            start_inclusive: true,
            end_inclusive: true,
        }
    }

    /// Set the start of the range
    pub fn start(mut self, start: impl Into<RecordIdValue>, inclusive: bool) -> Self {
        self.start = Some(start.into());
        self.start_inclusive = inclusive;
        self
    }

    /// Set the end of the range
    pub fn end(mut self, end: impl Into<RecordIdValue>, inclusive: bool) -> Self {
        self.end = Some(end.into());
        self.end_inclusive = inclusive;
        self
    }

    /// Convert to SurrealQL string representation
    pub fn to_surql(&self) -> String {
        let table = escape_identifier(&self.table);

        let start_str = match &self.start {
            Some(start) => {
                let start_val = match start {
                    RecordIdValue::String(s) => escape_identifier(s),
                    RecordIdValue::Number(n) => n.to_string(),
                    RecordIdValue::Object(obj) => obj.to_string(),
                    RecordIdValue::Array(arr) => {
                        format!(
                            "[{}]",
                            arr.iter()
                                .map(|v| v.to_string())
                                .collect::<Vec<_>>()
                                .join(", ")
                        )
                    }
                };
                if self.start_inclusive {
                    start_val
                } else {
                    format!(">{}", start_val)
                }
            }
            None => String::new(),
        };

        let end_str = match &self.end {
            Some(end) => {
                let end_val = match end {
                    RecordIdValue::String(s) => escape_identifier(s),
                    RecordIdValue::Number(n) => n.to_string(),
                    RecordIdValue::Object(obj) => obj.to_string(),
                    RecordIdValue::Array(arr) => {
                        format!(
                            "[{}]",
                            arr.iter()
                                .map(|v| v.to_string())
                                .collect::<Vec<_>>()
                                .join(", ")
                        )
                    }
                };
                if self.end_inclusive {
                    end_val
                } else {
                    format!("={}", end_val)
                }
            }
            None => String::new(),
        };

        if start_str.is_empty() && end_str.is_empty() {
            table
        } else {
            format!("{}:{}..{}", table, start_str, end_str)
        }
    }
}

impl fmt::Display for RecordRange {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.to_surql())
    }
}

/// Error type for record ID parsing
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RecordParseError {
    InvalidFormat,
    InvalidId,
}

impl fmt::Display for RecordParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            RecordParseError::InvalidFormat => write!(f, "Invalid record ID format"),
            RecordParseError::InvalidId => write!(f, "Invalid record ID value"),
        }
    }
}

impl std::error::Error for RecordParseError {}

/// Escape a SurrealDB identifier if needed
fn escape_identifier(ident: &str) -> String {
    // Check if identifier needs escaping
    if ident.is_empty() {
        return "⟨⟩".to_string();
    }

    // Check if it's numeric
    if ident.parse::<i64>().is_ok() || ident.parse::<f64>().is_ok() {
        return format!("{}", ident);
    }

    // Check if it contains special characters or starts with a number
    if ident.chars().next().unwrap().is_ascii_digit()
        || ident.chars().any(|c| !c.is_alphanumeric() && c != '_')
    {
        return format!("{}", ident.replace('', "\\"));
    }

    ident.to_string()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_record_id_creation() {
        let record = RecordId::string("user", "john");
        assert_eq!(record.table, "user");
        assert_eq!(record.id, RecordIdValue::String("john".to_string()));
    }

    #[test]
    fn test_record_id_parsing() {
        let record = RecordId::parse("user:123").unwrap();
        assert_eq!(record.table, "user");
        assert_eq!(record.id, RecordIdValue::Number(123));

        let record = RecordId::parse("user:john").unwrap();
        assert_eq!(record.table, "user");
        assert_eq!(record.id, RecordIdValue::String("john".to_string()));
    }

    #[test]
    fn test_record_id_surql() {
        let record = RecordId::string("user", "john");
        assert_eq!(record.to_surql(), "user:john");

        let record = RecordId::number("user", 123);
        assert_eq!(record.to_surql(), "user:123");
    }

    #[test]
    fn test_table_creation() {
        let table = Table::new("users");
        assert_eq!(table.name(), "users");
        assert_eq!(table.to_surql(), "users");
    }

    #[test]
    fn test_escape_identifier() {
        assert_eq!(escape_identifier("normal"), "normal");
        assert_eq!(escape_identifier("123"), "⟨123⟩");
        assert_eq!(escape_identifier("with-dash"), "⟨with-dash⟩");
        assert_eq!(escape_identifier(""), "⟨⟩");
    }

    #[test]
    fn test_record_range() {
        let range = RecordRange::new("user").start("a", true).end("z", false);

        assert_eq!(range.to_surql(), "user:a..=z");
    }

    #[test]
    fn test_conversions() {
        let record = RecordId::string("user", "john");
        let value: Value = record.into();
        assert_eq!(value, Value::String("user:john".to_string()));

        let table = Table::new("users");
        let value: Value = table.into();
        assert_eq!(value, Value::String("users".to_string()));
    }
}