dw_query/
lib.rs

1#[macro_use]
2extern crate log;
3extern crate serde;
4extern crate serde_json;
5
6use std::fmt;
7
8use dw_models::TimeInterval;
9
10use dw_datastore::Datastore;
11
12pub mod datatype;
13
14mod ast;
15mod functions;
16mod interpret;
17mod lexer;
18#[allow(
19    clippy::match_single_binding,
20    clippy::redundant_closure_call,
21    unused_braces
22)]
23mod parser;
24
25pub use crate::datatype::DataType;
26pub use crate::interpret::VarEnv;
27
28// TODO: add line numbers to errors
29// (works during lexing, but not during parsing I believe)
30
31#[derive(Debug)]
32pub enum QueryError {
33    // Parser
34    ParsingError(String),
35
36    // Execution
37    EmptyQuery(),
38    VariableNotDefined(String),
39    MathError(String),
40    InvalidType(String),
41    InvalidFunctionParameters(String),
42    TimeIntervalError(String),
43    BucketQueryError(String),
44    RegexCompileError(String),
45}
46
47impl fmt::Display for QueryError {
48    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
49        write!(f, "{:?}", self)
50    }
51}
52
53pub fn query(code: &str, ti: &TimeInterval, ds: &Datastore) -> Result<DataType, QueryError> {
54    let lexer = lexer::Lexer::new(code);
55    let program = match parser::parse(lexer) {
56        Ok(p) => p,
57        Err(e) => {
58            // TODO: Improve parsing error message
59            warn!("ParsingError: {:?}", e);
60            return Err(QueryError::ParsingError(format!("{:?}", e)));
61        }
62    };
63    interpret::interpret_prog(program, ti, ds)
64}