Remove unimplemented script parsing functionality

This commit is contained in:
Tobie Morgan Hitchcock 2022-02-23 13:20:00 +00:00
parent 02d7154189
commit 7f47fad02b
3 changed files with 11 additions and 130 deletions

View file

@ -83,7 +83,7 @@ impl fmt::Display for Function {
} }
pub fn function(i: &str) -> IResult<&str, 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> { fn future(i: &str) -> IResult<&str, Function> {
@ -99,7 +99,7 @@ fn future(i: &str) -> IResult<&str, Function> {
Ok((i, Function::Future(v))) 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, _) = tag("fn::script")(i)?;
let (i, _) = mightbespace(i)?; let (i, _) = mightbespace(i)?;
let (i, _) = tag("->")(i)?; let (i, _) = tag("->")(i)?;
@ -418,22 +418,4 @@ mod tests {
assert_eq!("fn::future -> { 1.2345 + 5.4321 }", format!("{}", out)); assert_eq!("fn::future -> { 1.2345 + 5.4321 }", format!("{}", out));
assert_eq!(out, Function::Future(Value::from(Expression::parse("1.2345 + 5.4321")))); 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; });"
))
);
}
} }

View file

@ -1,127 +1,19 @@
use crate::sql::error::IResult; 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 serde::{Deserialize, Serialize};
use std::fmt; use std::fmt;
use std::str; 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)] #[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Script { pub struct Script {
pub value: String, pub value: String,
} }
impl From<String> 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 { impl fmt::Display for Script {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "\"{}\"", self.value) write!(f, "\"{}\"", self.value)
} }
} }
pub fn script(i: &str) -> IResult<&str, Script> { pub fn script(_: &str) -> IResult<&str, Script> {
let (i, v) = script_raw(i)?; unimplemented!()
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"#));
}
} }

View file

@ -2,6 +2,7 @@ use crate::sql::array::{array, Array};
use crate::sql::expression::{expression, Expression}; use crate::sql::expression::{expression, Expression};
use crate::sql::idiom::{idiom, Idiom}; use crate::sql::idiom::{idiom, Idiom};
use crate::sql::param::{param, Param}; use crate::sql::param::{param, Param};
use crate::sql::script::{script, Script};
use crate::sql::value::{value, Value}; use crate::sql::value::{value, Value};
pub trait Parse<T> { pub trait Parse<T> {
@ -32,6 +33,12 @@ impl Parse<Idiom> for Idiom {
} }
} }
impl Parse<Script> for Script {
fn parse(val: &str) -> Script {
script(val).unwrap().1
}
}
impl Parse<Expression> for Expression { impl Parse<Expression> for Expression {
fn parse(val: &str) -> Expression { fn parse(val: &str) -> Expression {
expression(val).unwrap().1 expression(val).unwrap().1