tudor-sql 0.2.0

Does sql stuff to todo.txt files
Documentation
use pest_consume::Parser;

use crate::sql::expression::parser::{ExpressionParser, Result, Rule};
use crate::sql::expression::value::Value;
use crate::TODO_DATE_FORMAT;

#[derive(Eq, PartialEq, Debug)]
pub struct Function {
    pub name: String,
    // TODO: Function args are Expressions
    // f.args : Vec<Expressions>
    // then, in Expression::eval:
    //   let arg_vals = f.args.iter().map(|a| a.eval(t))
    //   f.apply(arg_vals)
    pub args: Vec<Value>,
}

// FIXME: unnastify this, Value also can be an ERR? ERR <> ERR? evals return Results?
#[allow(deprecated)]
fn date(args: &[Value]) -> Value {
    match args {
        [Value::Text(date_string)] => {
            match date_string.as_str() {
                // TODO: free this from side effects .. Today is part of a global context that gets passed into apply?
                // TODO: this is deprecated. Use locale to determine date
                "today" => Value::Date(time::Date::today()),
                s => Value::Date(time::Date::parse(s, TODO_DATE_FORMAT).unwrap()),
            }
        }
        _ => unimplemented!(),
    }
}

impl Function {
    // TODO: date('today')
    // TODO: date('today', '+ 2 weeks')
    // TODO: count(field)
    // TODO: size(field)
    pub fn apply(&self) -> Value {
        match self.name.as_str() {
            "date" => date(&self.args[..]),
            _ => unimplemented!(),
        }
    }

    pub fn parse(input: &str) -> Result<Function> {
        let nodes = ExpressionParser::parse(Rule::function, input)?;
        let node = nodes.single()?;
        let f = ExpressionParser::function(node)?;
        Ok(f)
    }
}