diff --git a/lib/src/sql/function.rs b/lib/src/sql/function.rs index f9e82d09..4af690a4 100644 --- a/lib/src/sql/function.rs +++ b/lib/src/sql/function.rs @@ -83,7 +83,7 @@ impl fmt::Display for Function { } pub fn function(i: &str) -> IResult<&str, Function> { - alt((casts, langs, future, normal))(i) + alt((casts, embed, future, normal))(i) } fn future(i: &str) -> IResult<&str, Function> { @@ -99,7 +99,7 @@ fn future(i: &str) -> IResult<&str, Function> { Ok((i, Function::Future(v))) } -fn langs(i: &str) -> IResult<&str, Function> { +fn embed(i: &str) -> IResult<&str, Function> { let (i, _) = tag("fn::script")(i)?; let (i, _) = mightbespace(i)?; let (i, _) = tag("->")(i)?; @@ -418,22 +418,4 @@ mod tests { assert_eq!("fn::future -> { 1.2345 + 5.4321 }", format!("{}", out)); assert_eq!(out, Function::Future(Value::from(Expression::parse("1.2345 + 5.4321")))); } - - #[test] - fn function_script_expression() { - let sql = "fn::script -> { 1.2345 + 5.4321 }"; - let res = function(sql); - assert!(res.is_ok()); - let out = res.unwrap().1; - assert_eq!( - "fn::script -> { return this.tags.filter(t => { return t.length > 3; }); }", - format!("{}", out) - ); - assert_eq!( - out, - Function::Script(Script::from( - "return this.tags.filter(t => { return t.length > 3; });" - )) - ); - } } diff --git a/lib/src/sql/script.rs b/lib/src/sql/script.rs index 64087bfe..098bdc5a 100644 --- a/lib/src/sql/script.rs +++ b/lib/src/sql/script.rs @@ -1,127 +1,19 @@ use crate::sql::error::IResult; -use nom::branch::alt; -use nom::bytes::complete::escaped; -use nom::bytes::complete::is_not; -use nom::bytes::complete::tag; -use nom::character::complete::one_of; use serde::{Deserialize, Serialize}; use std::fmt; use std::str; -const SINGLE: &str = r#"'"#; -const SINGLE_ESC: &str = r#"\'"#; - -const DOUBLE: &str = r#"""#; -const DOUBLE_ESC: &str = r#"\""#; - #[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)] pub struct Script { pub value: String, } -impl From for Script { - fn from(s: String) -> Self { - Script { - value: s, - } - } -} - -impl<'a> From<&'a str> for Script { - fn from(s: &str) -> Self { - Script { - value: String::from(s), - } - } -} - -impl Script { - pub fn as_str(&self) -> &str { - self.value.as_str() - } -} - impl fmt::Display for Script { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "\"{}\"", self.value) } } -pub fn script(i: &str) -> IResult<&str, Script> { - let (i, v) = script_raw(i)?; - Ok((i, Script::from(v))) -} - -pub fn script_raw(i: &str) -> IResult<&str, String> { - alt((script_single, script_double))(i) -} - -fn script_single(i: &str) -> IResult<&str, String> { - let (i, _) = tag(SINGLE)(i)?; - let (i, v) = alt((escaped(is_not(SINGLE_ESC), '\\', one_of(SINGLE)), tag("")))(i)?; - let (i, _) = tag(SINGLE)(i)?; - Ok((i, String::from(v).replace(SINGLE_ESC, SINGLE))) -} - -fn script_double(i: &str) -> IResult<&str, String> { - let (i, _) = tag(DOUBLE)(i)?; - let (i, v) = alt((escaped(is_not(DOUBLE_ESC), '\\', one_of(DOUBLE)), tag("")))(i)?; - let (i, _) = tag(DOUBLE)(i)?; - Ok((i, String::from(v).replace(DOUBLE_ESC, DOUBLE))) -} - -#[cfg(test)] -mod tests { - - use super::*; - - #[test] - fn script_empty() { - let sql = r#""""#; - let res = script(sql); - assert!(res.is_ok()); - let out = res.unwrap().1; - assert_eq!(r#""""#, format!("{}", out)); - assert_eq!(out, Script::from("")); - } - - #[test] - fn script_single() { - let sql = r#"'test'"#; - let res = script(sql); - assert!(res.is_ok()); - let out = res.unwrap().1; - assert_eq!(r#""test""#, format!("{}", out)); - assert_eq!(out, Script::from("test")); - } - - #[test] - fn script_double() { - let sql = r#""test""#; - let res = script(sql); - assert!(res.is_ok()); - let out = res.unwrap().1; - assert_eq!(r#""test""#, format!("{}", out)); - assert_eq!(out, Script::from("test")); - } - - #[test] - fn script_quoted_single() { - let sql = r#"'te\'st'"#; - let res = script(sql); - assert!(res.is_ok()); - let out = res.unwrap().1; - assert_eq!(r#""te'st""#, format!("{}", out)); - assert_eq!(out, Script::from(r#"te'st"#)); - } - - #[test] - fn script_quoted_double() { - let sql = r#""te\"st""#; - let res = script(sql); - assert!(res.is_ok()); - let out = res.unwrap().1; - assert_eq!(r#""te"st""#, format!("{}", out)); - assert_eq!(out, Script::from(r#"te"st"#)); - } +pub fn script(_: &str) -> IResult<&str, Script> { + unimplemented!() } diff --git a/lib/src/sql/test.rs b/lib/src/sql/test.rs index 3d8d0e4c..6249ff11 100644 --- a/lib/src/sql/test.rs +++ b/lib/src/sql/test.rs @@ -2,6 +2,7 @@ use crate::sql::array::{array, Array}; use crate::sql::expression::{expression, Expression}; use crate::sql::idiom::{idiom, Idiom}; use crate::sql::param::{param, Param}; +use crate::sql::script::{script, Script}; use crate::sql::value::{value, Value}; pub trait Parse { @@ -32,6 +33,12 @@ impl Parse for Idiom { } } +impl Parse