2022-07-04 01:03:26 +00:00
|
|
|
use surrealdb::sql::Array;
|
|
|
|
use surrealdb::sql::Value;
|
|
|
|
|
|
|
|
pub trait Take {
|
2022-10-25 13:31:14 +00:00
|
|
|
fn needs_one(self) -> Result<Value, ()>;
|
|
|
|
fn needs_two(self) -> Result<(Value, Value), ()>;
|
|
|
|
fn needs_one_or_two(self) -> Result<(Value, Value), ()>;
|
2022-07-04 01:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Take for Array {
|
2022-11-23 09:42:59 +00:00
|
|
|
/// Convert the array to one argument
|
2022-10-25 13:31:14 +00:00
|
|
|
fn needs_one(self) -> Result<Value, ()> {
|
2023-08-27 10:40:49 +00:00
|
|
|
if self.is_empty() {
|
2022-10-25 13:31:14 +00:00
|
|
|
return Err(());
|
|
|
|
}
|
2022-07-04 01:03:26 +00:00
|
|
|
let mut x = self.into_iter();
|
|
|
|
match x.next() {
|
2022-10-25 13:31:14 +00:00
|
|
|
Some(a) => Ok(a),
|
|
|
|
None => Ok(Value::None),
|
2022-07-04 01:03:26 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-23 09:42:59 +00:00
|
|
|
/// Convert the array to two arguments
|
2022-10-25 13:31:14 +00:00
|
|
|
fn needs_two(self) -> Result<(Value, Value), ()> {
|
|
|
|
if self.len() < 2 {
|
|
|
|
return Err(());
|
|
|
|
}
|
2022-07-04 01:03:26 +00:00
|
|
|
let mut x = self.into_iter();
|
|
|
|
match (x.next(), x.next()) {
|
2022-10-25 13:31:14 +00:00
|
|
|
(Some(a), Some(b)) => Ok((a, b)),
|
|
|
|
(Some(a), None) => Ok((a, Value::None)),
|
|
|
|
(_, _) => Ok((Value::None, Value::None)),
|
2022-07-04 01:03:26 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-23 09:42:59 +00:00
|
|
|
/// Convert the array to two arguments
|
2022-10-25 13:31:14 +00:00
|
|
|
fn needs_one_or_two(self) -> Result<(Value, Value), ()> {
|
2023-08-27 10:40:49 +00:00
|
|
|
if self.is_empty() {
|
2022-10-25 13:31:14 +00:00
|
|
|
return Err(());
|
|
|
|
}
|
2022-07-04 01:03:26 +00:00
|
|
|
let mut x = self.into_iter();
|
2022-10-25 13:31:14 +00:00
|
|
|
match (x.next(), x.next()) {
|
|
|
|
(Some(a), Some(b)) => Ok((a, b)),
|
|
|
|
(Some(a), None) => Ok((a, Value::None)),
|
|
|
|
(_, _) => Ok((Value::None, Value::None)),
|
2022-07-04 01:03:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|