Skip to content

Commit

Permalink
Use <future> instead of fn::future syntax for SQL futures
Browse files Browse the repository at this point in the history
Closes #9
  • Loading branch information
tobiemh committed Jul 24, 2022
1 parent e9476b9 commit a71562d
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions lib/src/sql/function.rs
Expand Up @@ -154,7 +154,7 @@ impl Function {
impl fmt::Display for Function {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Function::Future(ref e) => write!(f, "fn::future -> {{ {} }}", e),
Function::Future(ref e) => write!(f, "<future> {{ {} }}", e),
Function::Cast(ref s, ref e) => write!(f, "<{}> {}", s, e),
Function::Script(ref s, ref e) => write!(
f,
Expand All @@ -173,21 +173,7 @@ impl fmt::Display for Function {
}

pub fn function(i: &str) -> IResult<&str, Function> {
alt((future, normal, script, cast))(i)
}

fn future(i: &str) -> IResult<&str, Function> {
let (i, _) = tag("fn::future")(i)?;
let (i, _) = mightbespace(i)?;
let (i, _) = char('-')(i)?;
let (i, _) = char('>')(i)?;
let (i, _) = mightbespace(i)?;
let (i, _) = char('{')(i)?;
let (i, _) = mightbespace(i)?;
let (i, v) = value(i)?;
let (i, _) = mightbespace(i)?;
let (i, _) = char('}')(i)?;
Ok((i, Function::Future(v)))
alt((normal, script, future, cast))(i)
}

fn normal(i: &str) -> IResult<&str, Function> {
Expand Down Expand Up @@ -218,6 +204,19 @@ fn script(i: &str) -> IResult<&str, Function> {
Ok((i, Function::Script(v, a)))
}

fn future(i: &str) -> IResult<&str, Function> {
let (i, _) = char('<')(i)?;
let (i, _) = tag("future")(i)?;
let (i, _) = char('>')(i)?;
let (i, _) = mightbespace(i)?;
let (i, _) = char('{')(i)?;
let (i, _) = mightbespace(i)?;
let (i, v) = value(i)?;
let (i, _) = mightbespace(i)?;
let (i, _) = char('}')(i)?;
Ok((i, Function::Future(v)))
}

fn cast(i: &str) -> IResult<&str, Function> {
let (i, _) = char('<')(i)?;
let (i, s) = function_casts(i)?;
Expand Down Expand Up @@ -503,11 +502,11 @@ mod tests {

#[test]
fn function_future_expression() {
let sql = "fn::future -> { 1.2345 + 5.4321 }";
let sql = "<future> { 1.2345 + 5.4321 }";
let res = function(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!("fn::future -> { 1.2345 + 5.4321 }", format!("{}", out));
assert_eq!("<future> { 1.2345 + 5.4321 }", format!("{}", out));
assert_eq!(out, Function::Future(Value::from(Expression::parse("1.2345 + 5.4321"))));
}

Expand Down

0 comments on commit a71562d

Please sign in to comment.