surrealdb-core-nightly 2.1.20250202

A nightly release of the surrealdb-core crate
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
438
439
440
441
442
443
use crate::err::Error;
use crate::sql::value::Value;
use crate::sql::{
	Array, Bytes, Closure, Datetime, Duration, Geometry, Kind, Number, Object, Regex, Strand,
	Thing, Uuid,
};
use std::vec::IntoIter;

/// Implemented by types that are commonly used, in a certain way, as arguments.
pub trait FromArg: Sized {
	fn from_arg(arg: Value) -> Result<Self, Error>;
}

impl FromArg for Value {
	fn from_arg(arg: Value) -> Result<Self, Error> {
		Ok(arg)
	}
}

impl FromArg for Closure {
	fn from_arg(arg: Value) -> Result<Self, Error> {
		arg.coerce_to_function()
	}
}

impl FromArg for Regex {
	fn from_arg(arg: Value) -> Result<Self, Error> {
		arg.coerce_to_regex()
	}
}

impl FromArg for String {
	fn from_arg(arg: Value) -> Result<Self, Error> {
		arg.coerce_to_string()
	}
}

impl FromArg for Strand {
	fn from_arg(arg: Value) -> Result<Self, Error> {
		arg.coerce_to_strand()
	}
}

impl FromArg for Number {
	fn from_arg(arg: Value) -> Result<Self, Error> {
		arg.coerce_to_number()
	}
}

impl FromArg for Datetime {
	fn from_arg(arg: Value) -> Result<Self, Error> {
		arg.coerce_to_datetime()
	}
}

impl FromArg for Duration {
	fn from_arg(arg: Value) -> Result<Self, Error> {
		arg.coerce_to_duration()
	}
}

impl FromArg for Geometry {
	fn from_arg(arg: Value) -> Result<Self, Error> {
		arg.coerce_to_geometry()
	}
}

impl FromArg for Thing {
	fn from_arg(arg: Value) -> Result<Self, Error> {
		arg.coerce_to_record()
	}
}

impl FromArg for Array {
	fn from_arg(arg: Value) -> Result<Self, Error> {
		arg.coerce_to_array()
	}
}

impl FromArg for Object {
	fn from_arg(arg: Value) -> Result<Self, Error> {
		arg.coerce_to_object()
	}
}

impl FromArg for Bytes {
	fn from_arg(arg: Value) -> Result<Self, Error> {
		arg.coerce_to_bytes()
	}
}

impl FromArg for i64 {
	fn from_arg(arg: Value) -> Result<Self, Error> {
		arg.coerce_to_i64()
	}
}

impl FromArg for u64 {
	fn from_arg(arg: Value) -> Result<Self, Error> {
		arg.coerce_to_u64()
	}
}

impl FromArg for f64 {
	fn from_arg(arg: Value) -> Result<Self, Error> {
		arg.coerce_to_f64()
	}
}

impl FromArg for isize {
	fn from_arg(arg: Value) -> Result<Self, Error> {
		Ok(arg.coerce_to_i64()? as isize)
	}
}

impl FromArg for usize {
	fn from_arg(arg: Value) -> Result<Self, Error> {
		Ok(arg.coerce_to_u64()? as usize)
	}
}

impl FromArg for Uuid {
	fn from_arg(arg: Value) -> Result<Self, Error> {
		arg.coerce_to_uuid()
	}
}

impl FromArg for Vec<String> {
	fn from_arg(arg: Value) -> Result<Self, Error> {
		arg.coerce_to_array_type(&Kind::String)?.into_iter().map(Value::try_into).collect()
	}
}

impl FromArg for Vec<Number> {
	fn from_arg(arg: Value) -> Result<Self, Error> {
		arg.coerce_to_array_type(&Kind::Number)?.into_iter().map(Value::try_into).collect()
	}
}

impl FromArg for Vec<Datetime> {
	fn from_arg(arg: Value) -> Result<Self, Error> {
		arg.coerce_to_array_type(&Kind::Datetime)?.into_iter().map(Value::try_into).collect()
	}
}

pub trait FromArgs: Sized {
	/// Convert a collection of argument values into a certain argument format, failing if there are
	/// too many or too few arguments, or if one of the arguments could not be converted.
	fn from_args(name: &str, args: Vec<Value>) -> Result<Self, Error>;
}

// Take ownership of the raw arguments collection, and assume responsibility of validating the
// number of arguments and converting them as necessary.
impl FromArgs for Vec<Value> {
	fn from_args(_name: &str, args: Vec<Value>) -> Result<Self, Error> {
		Ok(args)
	}
}

impl FromArgs for Vec<Array> {
	fn from_args(name: &str, args: Vec<Value>) -> Result<Self, Error> {
		args.into_iter()
			.enumerate()
			.map(|(i, arg)| {
				arg.coerce_to_array_type(&Kind::Any).map_err(|e| Error::InvalidArguments {
					name: name.to_owned(),
					message: format!("Argument {} was the wrong type. {e}", i + 1),
				})
			})
			.collect()
	}
}

/// Some functions take a fixed number of arguments.
/// The len must match the number of type idents that follow.
macro_rules! impl_tuple {
	($len:expr, $( $T:ident ),*) => {
		impl<$($T:FromArg),*> FromArgs for ($($T,)*) {
			#[allow(non_snake_case)]
			fn from_args(name: &str, args: Vec<Value>) -> Result<Self, Error> {
				let [$($T),*]: [Value; $len] = args.try_into().map_err(|_| Error::InvalidArguments {
					name: name.to_owned(),
					// This match will be optimized away.
					message: match $len {
						0 => String::from("Expected no arguments."),
						1 => String::from("Expected 1 argument."),
						_ => format!("Expected {} arguments.", $len),
					}
				})?;
				#[allow(unused_mut, unused_variables)]
				let mut i = 0;
				Ok((
					$({
						i += 1;
						$T::from_arg($T).map_err(|e| Error::InvalidArguments {
							name: name.to_owned(),
							message: format!("Argument {i} was the wrong type. {e}"),
						})?
					},)*
				))
			}
		}
	}
}

// It is possible to add larger sequences to support higher quantities of fixed arguments.
impl_tuple!(0,);
impl_tuple!(1, A);
impl_tuple!(2, A, B);
impl_tuple!(3, A, B, C);
impl_tuple!(4, A, B, C, D);

// Some functions take a single, optional argument, or no arguments at all.
impl<A: FromArg> FromArgs for (Option<A>,) {
	fn from_args(name: &str, args: Vec<Value>) -> Result<Self, Error> {
		let err = || Error::InvalidArguments {
			name: name.to_owned(),
			message: String::from("Expected 0 or 1 arguments."),
		};
		// Process the function arguments
		let mut args = args.into_iter();
		// Process the first function argument
		let a = match args.next() {
			Some(a) => Some(A::from_arg(a).map_err(|e| Error::InvalidArguments {
				name: name.to_owned(),
				message: format!("Argument 1 was the wrong type. {e}"),
			})?),
			None => None,
		};
		// Process additional function arguments
		if args.next().is_some() {
			// Too many arguments
			return Err(err());
		}
		Ok((a,))
	}
}

// Some functions take 1 or 2 arguments, so the second argument is optional.
impl<A: FromArg, B: FromArg> FromArgs for (A, Option<B>) {
	fn from_args(name: &str, args: Vec<Value>) -> Result<Self, Error> {
		let err = || Error::InvalidArguments {
			name: name.to_owned(),
			message: String::from("Expected 1 or 2 arguments."),
		};
		// Process the function arguments
		let mut args = args.into_iter();
		// Process the first argument
		let a = A::from_arg(args.next().ok_or_else(err)?).map_err(|e| Error::InvalidArguments {
			name: name.to_owned(),
			message: format!("Argument 1 was the wrong type. {e}"),
		})?;
		let b = match args.next() {
			Some(b) => Some(B::from_arg(b)?),
			None => None,
		};
		// Process additional function arguments
		if args.next().is_some() {
			// Too many arguments
			return Err(err());
		}
		Ok((a, b))
	}
}

// Some functions take 4 arguments, with the 3rd and 4th being optional.
impl<A: FromArg, B: FromArg, C: FromArg, D: FromArg> FromArgs for (A, B, Option<C>, Option<D>) {
	fn from_args(name: &str, args: Vec<Value>) -> Result<Self, Error> {
		let err = || Error::InvalidArguments {
			name: name.to_owned(),
			message: String::from("Expected 2, 3 or 4 arguments."),
		};
		// Process the function arguments
		let mut args = args.into_iter();
		// Process the first argument
		let a = A::from_arg(args.next().ok_or_else(err)?).map_err(|e| Error::InvalidArguments {
			name: name.to_owned(),
			message: format!("Argument 1 was the wrong type. {e}"),
		})?;
		let b = B::from_arg(args.next().ok_or_else(err)?).map_err(|e| Error::InvalidArguments {
			name: name.to_owned(),
			message: format!("Argument 2 was the wrong type. {e}"),
		})?;
		let c = match args.next() {
			Some(c) => Some(C::from_arg(c)?),
			None => None,
		};
		let d = match args.next() {
			Some(d) => Some(D::from_arg(d)?),
			None => None,
		};
		// Process additional function arguments
		if args.next().is_some() {
			// Too many arguments
			return Err(err());
		}
		Ok((a, b, c, d))
	}
}

#[inline]
fn get_arg<T: FromArg, E: Fn() -> Error>(
	name: &str,
	pos: usize,
	args: &mut IntoIter<Value>,
	err: E,
) -> Result<T, Error> {
	T::from_arg(args.next().ok_or_else(err)?).map_err(|e| Error::InvalidArguments {
		name: name.to_owned(),
		message: format!("Argument {pos} was the wrong type. {e}"),
	})
}

#[inline]
fn get_opt_arg<T: FromArg>(
	name: &str,
	pos: usize,
	args: &mut IntoIter<Value>,
) -> Result<Option<T>, Error> {
	Ok(match args.next() {
		Some(v) => Some(T::from_arg(v).map_err(|e| Error::InvalidArguments {
			name: name.to_owned(),
			message: format!("Argument {pos} was the wrong type. {e}"),
		})?),
		None => None,
	})
}

// Some functions take 2 or 3 arguments, so the third argument is optional.
impl<A: FromArg, B: FromArg, C: FromArg> FromArgs for (A, B, Option<C>) {
	fn from_args(name: &str, args: Vec<Value>) -> Result<Self, Error> {
		let err = || Error::InvalidArguments {
			name: name.to_owned(),
			message: String::from("Expected 2 or 3 arguments."),
		};
		// Process the function arguments
		let mut args = args.into_iter();

		let a: A = get_arg(name, 1, &mut args, err)?;
		let b: B = get_arg(name, 2, &mut args, err)?;
		let c: Option<C> = get_opt_arg(name, 3, &mut args)?;

		// Process additional function arguments
		if args.next().is_some() {
			// Too many arguments
			return Err(err());
		}
		Ok((a, b, c))
	}
}

// Some functions take 3 or 4 arguments, so the fourth argument is optional.
impl<A: FromArg, B: FromArg, C: FromArg, D: FromArg> FromArgs for (A, B, C, Option<D>) {
	fn from_args(name: &str, args: Vec<Value>) -> Result<Self, Error> {
		let err = || Error::InvalidArguments {
			name: name.to_owned(),
			message: String::from("Expected 3 or 4 arguments."),
		};
		// Process the function arguments
		let mut args = args.into_iter();

		let a: A = get_arg(name, 1, &mut args, err)?;
		let b: B = get_arg(name, 2, &mut args, err)?;
		let c: C = get_arg(name, 3, &mut args, err)?;
		let d: Option<D> = get_opt_arg(name, 4, &mut args)?;

		// Process additional function arguments
		if args.next().is_some() {
			// Too many arguments
			return Err(err());
		}
		Ok((a, b, c, d))
	}
}

// Some functions take 0, 1, or 2 arguments, so both arguments are optional.
// It is safe to assume that, if the first argument is None, the second argument will also be None.
impl<A: FromArg, B: FromArg> FromArgs for (Option<A>, Option<B>) {
	fn from_args(name: &str, args: Vec<Value>) -> Result<Self, Error> {
		let err = || Error::InvalidArguments {
			name: name.to_owned(),
			message: String::from("Expected 0, 1, or 2 arguments."),
		};
		// Process the function arguments
		let mut args = args.into_iter();

		let a: Option<A> = get_opt_arg(name, 1, &mut args)?;
		let b: Option<B> = get_opt_arg(name, 2, &mut args)?;

		// Process additional function arguments
		if args.next().is_some() {
			// Too many arguments
			return Err(err());
		}
		Ok((a, b))
	}
}

// Some functions optionally take 2 arguments, or don't take any at all.
impl<A: FromArg, B: FromArg> FromArgs for (Option<(A, B)>,) {
	fn from_args(name: &str, args: Vec<Value>) -> Result<Self, Error> {
		let err = || Error::InvalidArguments {
			name: name.to_owned(),
			message: String::from("Expected 0 or 2 arguments."),
		};
		// Process the function arguments
		let mut args = args.into_iter();

		let a: Option<A> = get_opt_arg(name, 1, &mut args)?;
		let b: Option<B> = get_opt_arg(name, 2, &mut args)?;

		// Process additional function arguments
		if a.is_some() != b.is_some() || args.next().is_some() {
			// One argument, or too many arguments
			return Err(err());
		}
		Ok((a.zip(b),))
	}
}

// Some functions take 1, 2, or 3 arguments. It is safe to assume that, if the second argument is
// None, the third argument will also be None.
impl<A: FromArg, B: FromArg, C: FromArg> FromArgs for (A, Option<B>, Option<C>) {
	fn from_args(name: &str, args: Vec<Value>) -> Result<Self, Error> {
		let err = || Error::InvalidArguments {
			name: name.to_owned(),
			message: String::from("Expected 1, 2, or 3 arguments."),
		};
		// Process the function arguments
		let mut args = args.into_iter();

		let a: A = get_arg(name, 1, &mut args, err)?;
		let b: Option<B> = get_opt_arg(name, 2, &mut args)?;
		let c: Option<C> = get_opt_arg(name, 3, &mut args)?;

		// Process additional function arguments
		if args.next().is_some() {
			// Too many arguments
			return Err(err());
		}
		Ok((a, b, c))
	}
}