2021-03-29 15:43:37 +00:00
|
|
|
use crate::dbs;
|
|
|
|
use crate::dbs::Executor;
|
2021-03-31 12:10:13 +00:00
|
|
|
use crate::dbs::Runtime;
|
2021-03-29 15:43:37 +00:00
|
|
|
use crate::doc::Document;
|
|
|
|
use crate::err::Error;
|
2020-06-29 15:36:01 +00:00
|
|
|
use crate::sql::comment::mightbespace;
|
|
|
|
use crate::sql::common::commas;
|
2021-03-29 15:43:37 +00:00
|
|
|
use crate::sql::expression::expression;
|
|
|
|
use crate::sql::literal::Literal;
|
|
|
|
use crate::sql::value::Value;
|
2020-06-29 15:36:01 +00:00
|
|
|
use nom::bytes::complete::tag;
|
|
|
|
use nom::combinator::opt;
|
2021-03-29 15:43:37 +00:00
|
|
|
use nom::multi::separated_list0;
|
2020-06-29 15:36:01 +00:00
|
|
|
use nom::IResult;
|
2021-03-29 15:43:37 +00:00
|
|
|
use serde::ser::SerializeStruct;
|
2020-06-29 15:36:01 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::fmt;
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
const NAME: &'static str = "Array";
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Deserialize)]
|
|
|
|
pub struct Array {
|
|
|
|
pub value: Vec<Value>,
|
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
|
|
|
|
impl fmt::Display for Array {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"[{}]",
|
2021-03-29 15:43:37 +00:00
|
|
|
self.value.iter().map(|ref v| format!("{}", v)).collect::<Vec<_>>().join(", ")
|
2020-06-29 15:36:01 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
impl dbs::Process for Array {
|
|
|
|
fn process(
|
|
|
|
&self,
|
2021-03-31 12:10:13 +00:00
|
|
|
ctx: &Runtime,
|
2021-03-29 15:43:37 +00:00
|
|
|
exe: &Executor,
|
|
|
|
doc: Option<&Document>,
|
|
|
|
) -> Result<Literal, Error> {
|
|
|
|
self.value
|
|
|
|
.iter()
|
|
|
|
.map(|v| match v.process(ctx, exe, doc) {
|
|
|
|
Ok(v) => Ok(Value::from(v)),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>, _>>()
|
|
|
|
.map(|v| {
|
|
|
|
Literal::Array(Array {
|
|
|
|
value: v,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for Array {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: serde::Serializer,
|
|
|
|
{
|
|
|
|
if serializer.is_human_readable() {
|
|
|
|
serializer.serialize_some(&self.value)
|
|
|
|
} else {
|
|
|
|
let mut val = serializer.serialize_struct(NAME, 1)?;
|
|
|
|
val.serialize_field("value", &self.value)?;
|
|
|
|
val.end()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
pub fn array(i: &str) -> IResult<&str, Array> {
|
|
|
|
let (i, _) = tag("[")(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
2021-03-29 15:43:37 +00:00
|
|
|
let (i, v) = separated_list0(commas, item)(i)?;
|
2020-06-29 15:36:01 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, _) = opt(tag(","))(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, _) = tag("]")(i)?;
|
2021-03-29 15:43:37 +00:00
|
|
|
Ok((
|
|
|
|
i,
|
|
|
|
Array {
|
|
|
|
value: v,
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn item(i: &str) -> IResult<&str, Value> {
|
|
|
|
let (i, v) = expression(i)?;
|
|
|
|
Ok((i, Value::from(v)))
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn array_normal() {
|
|
|
|
let sql = "[1,2,3]";
|
|
|
|
let res = array(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("[1, 2, 3]", format!("{}", out));
|
2021-03-29 15:43:37 +00:00
|
|
|
assert_eq!(out.value.len(), 3);
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn array_commas() {
|
|
|
|
let sql = "[1,2,3,]";
|
|
|
|
let res = array(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("[1, 2, 3]", format!("{}", out));
|
2021-03-29 15:43:37 +00:00
|
|
|
assert_eq!(out.value.len(), 3);
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn array_expression() {
|
|
|
|
let sql = "[1,2,3+1]";
|
|
|
|
let res = array(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("[1, 2, 3 + 1]", format!("{}", out));
|
2021-03-29 15:43:37 +00:00
|
|
|
assert_eq!(out.value.len(), 3);
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|