surrealdb_core/rpc/
args.rs

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
use crate::sql::Array;
use crate::sql::Value;

use super::rpc_error::RpcError;

pub trait Take {
	fn needs_one(self) -> Result<Value, RpcError>;
	fn needs_two(self) -> Result<(Value, Value), RpcError>;
	fn needs_three(self) -> Result<(Value, Value, Value), RpcError>;
	fn needs_one_or_two(self) -> Result<(Value, Value), RpcError>;
	fn needs_one_two_or_three(self) -> Result<(Value, Value, Value), RpcError>;
	fn needs_three_or_four(self) -> Result<(Value, Value, Value, Value), RpcError>;
}

impl Take for Array {
	/// Convert the array to one argument
	fn needs_one(self) -> Result<Value, RpcError> {
		if self.len() != 1 {
			return Err(RpcError::InvalidParams);
		}
		let mut x = self.into_iter();
		match x.next() {
			Some(a) => Ok(a),
			None => Ok(Value::None),
		}
	}
	/// Convert the array to two arguments
	fn needs_two(self) -> Result<(Value, Value), RpcError> {
		if self.len() != 2 {
			return Err(RpcError::InvalidParams);
		}
		let mut x = self.into_iter();
		match (x.next(), x.next()) {
			(Some(a), Some(b)) => Ok((a, b)),
			(Some(a), None) => Ok((a, Value::None)),
			(_, _) => Ok((Value::None, Value::None)),
		}
	}
	/// Convert the array to three arguments
	fn needs_three(self) -> Result<(Value, Value, Value), RpcError> {
		if self.len() != 3 {
			return Err(RpcError::InvalidParams);
		}
		let mut x = self.into_iter();
		match (x.next(), x.next(), x.next()) {
			(Some(a), Some(b), Some(c)) => Ok((a, b, c)),
			_ => Err(RpcError::InvalidParams),
		}
	}
	/// Convert the array to two arguments
	fn needs_one_or_two(self) -> Result<(Value, Value), RpcError> {
		if self.is_empty() || self.len() > 2 {
			return Err(RpcError::InvalidParams);
		}
		let mut x = self.into_iter();
		match (x.next(), x.next()) {
			(Some(a), Some(b)) => Ok((a, b)),
			(Some(a), None) => Ok((a, Value::None)),
			(_, _) => Ok((Value::None, Value::None)),
		}
	}
	/// Convert the array to three arguments
	fn needs_one_two_or_three(self) -> Result<(Value, Value, Value), RpcError> {
		if self.is_empty() || self.len() > 3 {
			return Err(RpcError::InvalidParams);
		}
		let mut x = self.into_iter();
		match (x.next(), x.next(), x.next()) {
			(Some(a), Some(b), Some(c)) => Ok((a, b, c)),
			(Some(a), Some(b), None) => Ok((a, b, Value::None)),
			(Some(a), None, None) => Ok((a, Value::None, Value::None)),
			(_, _, _) => Ok((Value::None, Value::None, Value::None)),
		}
	}
	/// Convert the array to four arguments
	fn needs_three_or_four(self) -> Result<(Value, Value, Value, Value), RpcError> {
		if self.len() < 3 || self.len() > 4 {
			return Err(RpcError::InvalidParams);
		}
		let mut x = self.into_iter();
		match (x.next(), x.next(), x.next(), x.next()) {
			(Some(a), Some(b), Some(c), Some(d)) => Ok((a, b, c, d)),
			(Some(a), Some(b), Some(c), None) => Ok((a, b, c, Value::None)),
			(_, _, _, _) => Ok((Value::None, Value::None, Value::None, Value::None)),
		}
	}
}