2022-10-31 23:12:41 +00:00
|
|
|
use crate::ctx::Context;
|
2023-07-06 14:57:42 +00:00
|
|
|
use crate::dbs::{Options, Transaction};
|
|
|
|
use crate::doc::CursorDoc;
|
2022-10-31 23:12:41 +00:00
|
|
|
use crate::err::Error;
|
2023-02-21 14:15:19 +00:00
|
|
|
use crate::sql::block::{block, Block};
|
2022-10-31 23:12:41 +00:00
|
|
|
use crate::sql::comment::mightbespace;
|
2023-05-10 02:08:09 +00:00
|
|
|
use crate::sql::common::{closechevron, openchevron};
|
2022-10-31 23:12:41 +00:00
|
|
|
use crate::sql::error::IResult;
|
2023-02-21 14:15:19 +00:00
|
|
|
use crate::sql::value::Value;
|
2022-10-31 23:12:41 +00:00
|
|
|
use nom::bytes::complete::tag;
|
2023-08-29 10:50:00 +00:00
|
|
|
use nom::combinator::cut;
|
2023-08-17 18:03:46 +00:00
|
|
|
use revision::revisioned;
|
2022-10-31 23:12:41 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::fmt;
|
|
|
|
|
2023-09-08 11:28:36 +00:00
|
|
|
use super::util::expect_delimited;
|
|
|
|
|
2023-03-30 10:41:44 +00:00
|
|
|
pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Future";
|
|
|
|
|
2023-04-29 15:58:22 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
|
|
|
|
#[serde(rename = "$surrealdb::private::sql::Future")]
|
2023-08-17 18:03:46 +00:00
|
|
|
#[revisioned(revision = 1)]
|
2023-02-21 14:15:19 +00:00
|
|
|
pub struct Future(pub Block);
|
|
|
|
|
|
|
|
impl From<Value> for Future {
|
|
|
|
fn from(v: Value) -> Self {
|
|
|
|
Future(Block::from(v))
|
|
|
|
}
|
|
|
|
}
|
2022-10-31 23:12:41 +00:00
|
|
|
|
|
|
|
impl Future {
|
2023-05-09 22:17:29 +00:00
|
|
|
/// Process this type returning a computed simple Value
|
2023-07-06 14:57:42 +00:00
|
|
|
pub(crate) async fn compute(
|
|
|
|
&self,
|
|
|
|
ctx: &Context<'_>,
|
|
|
|
opt: &Options,
|
|
|
|
txn: &Transaction,
|
|
|
|
doc: Option<&CursorDoc<'_>>,
|
|
|
|
) -> Result<Value, Error> {
|
2022-10-31 23:12:41 +00:00
|
|
|
// Process the future if enabled
|
|
|
|
match opt.futures {
|
2023-07-06 14:57:42 +00:00
|
|
|
true => self.0.compute(ctx, opt, txn, doc).await?.ok(),
|
2022-10-31 23:12:41 +00:00
|
|
|
false => Ok(self.clone().into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Future {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2023-02-21 14:15:19 +00:00
|
|
|
write!(f, "<future> {}", self.0)
|
2022-10-31 23:12:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn future(i: &str) -> IResult<&str, Future> {
|
2023-09-08 11:28:36 +00:00
|
|
|
let (i, _) = expect_delimited(openchevron, tag("future"), closechevron)(i)?;
|
2023-08-29 10:50:00 +00:00
|
|
|
cut(|i| {
|
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, v) = block(i)?;
|
|
|
|
Ok((i, Future(v)))
|
|
|
|
})(i)
|
2022-10-31 23:12:41 +00:00
|
|
|
}
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use crate::sql::expression::Expression;
|
|
|
|
use crate::sql::test::Parse;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn future_expression() {
|
2023-06-09 15:23:30 +00:00
|
|
|
let sql = "<future> { 5 + 10 }";
|
2022-10-31 23:12:41 +00:00
|
|
|
let res = future(sql);
|
|
|
|
let out = res.unwrap().1;
|
2023-06-09 15:23:30 +00:00
|
|
|
assert_eq!("<future> { 5 + 10 }", format!("{}", out));
|
|
|
|
assert_eq!(out, Future(Block::from(Value::from(Expression::parse("5 + 10")))));
|
2022-10-31 23:12:41 +00:00
|
|
|
}
|
|
|
|
}
|