Browse Source

Reduce to single error type, remove main

main
Dylan Baker 3 years ago
parent
commit
ec30423ebc
6 changed files with 30 additions and 91 deletions
  1. 3
    3
      src/compiler.rs
  2. 7
    61
      src/error.rs
  3. 3
    3
      src/lexer.rs
  4. 9
    0
      src/lib.rs
  5. 0
    16
      src/main.rs
  6. 8
    8
      src/parser.rs

+ 3
- 3
src/compiler.rs View File

@@ -2,7 +2,7 @@ use serde_json::{json, Value};
2 2
 
3 3
 use std::fmt::{Display, Formatter};
4 4
 
5
-use crate::error::CompilerError;
5
+use crate::error::ElasticError;
6 6
 use crate::parser::{Field, Identifier, Select};
7 7
 
8 8
 #[derive(Debug, PartialEq)]
@@ -28,7 +28,7 @@ impl Display for Query {
28 28
     }
29 29
 }
30 30
 
31
-pub fn compile(expr: Select) -> Result<Query, CompilerError> {
31
+pub fn compile(expr: Select) -> Result<Query, ElasticError> {
32 32
     let field_names: Vec<Value> = expr
33 33
         .fields
34 34
         .iter()
@@ -52,7 +52,7 @@ mod tests {
52 52
     use crate::lexer::scan;
53 53
     use crate::parser::parse;
54 54
 
55
-    fn _compile(input: &str) -> Result<Query, CompilerError> {
55
+    fn _compile(input: &str) -> Result<Query, ElasticError> {
56 56
         let tokens = scan(input).unwrap();
57 57
         let select = parse(tokens).unwrap();
58 58
         compile(select)

+ 7
- 61
src/error.rs View File

@@ -1,11 +1,11 @@
1 1
 use std::{error::Error, fmt, fmt::Display};
2 2
 
3 3
 #[derive(Debug, PartialEq)]
4
-pub struct LexerError {
5
-    message: String,
4
+pub struct ElasticError {
5
+    pub message: String,
6 6
 }
7 7
 
8
-impl LexerError {
8
+impl ElasticError {
9 9
     pub fn new(message: &str) -> Self {
10 10
         Self {
11 11
             message: message.to_string(),
@@ -13,70 +13,16 @@ impl LexerError {
13 13
     }
14 14
 }
15 15
 
16
-impl Display for LexerError {
16
+impl Display for ElasticError {
17 17
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
18 18
         write!(f, "{}", &self.message)
19 19
     }
20 20
 }
21 21
 
22
-impl Error for LexerError {}
22
+impl Error for ElasticError {}
23 23
 
24
-impl From<LexerError> for std::io::Error {
25
-    fn from(e: LexerError) -> Self {
26
-        Self::new(std::io::ErrorKind::InvalidInput, e.message)
27
-    }
28
-}
29
-
30
-#[derive(Debug, PartialEq)]
31
-pub struct ParserError {
32
-    message: String,
33
-}
34
-
35
-impl ParserError {
36
-    pub fn new(message: &str) -> Self {
37
-        Self {
38
-            message: message.to_string(),
39
-        }
40
-    }
41
-}
42
-
43
-impl Display for ParserError {
44
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45
-        write!(f, "{}", &self.message)
46
-    }
47
-}
48
-
49
-impl Error for ParserError {}
50
-
51
-impl From<ParserError> for std::io::Error {
52
-    fn from(e: ParserError) -> Self {
53
-        Self::new(std::io::ErrorKind::InvalidInput, e.message)
54
-    }
55
-}
56
-
57
-#[derive(Debug, PartialEq)]
58
-pub struct CompilerError {
59
-    message: String,
60
-}
61
-
62
-impl CompilerError {
63
-    pub fn new(message: &str) -> Self {
64
-        Self {
65
-            message: message.to_string(),
66
-        }
67
-    }
68
-}
69
-
70
-impl Display for CompilerError {
71
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
72
-        write!(f, "{}", &self.message)
73
-    }
74
-}
75
-
76
-impl Error for CompilerError {}
77
-
78
-impl From<CompilerError> for std::io::Error {
79
-    fn from(e: CompilerError) -> Self {
24
+impl From<ElasticError> for std::io::Error {
25
+    fn from(e: ElasticError) -> Self {
80 26
         Self::new(std::io::ErrorKind::InvalidInput, e.message)
81 27
     }
82 28
 }

+ 3
- 3
src/lexer.rs View File

@@ -1,7 +1,7 @@
1 1
 use lazy_static::lazy_static;
2 2
 use regex::Regex;
3 3
 
4
-use crate::error::LexerError;
4
+use crate::error::ElasticError;
5 5
 use crate::token::{Token, TokenType};
6 6
 
7 7
 #[derive(Debug)]
@@ -32,7 +32,7 @@ lazy_static! {
32 32
     ];
33 33
 }
34 34
 
35
-pub fn scan(input: &str) -> Result<Vec<Token>, LexerError> {
35
+pub fn scan(input: &str) -> Result<Vec<Token>, ElasticError> {
36 36
     let mut tokens: Vec<Token> = vec![];
37 37
     let mut position = 0;
38 38
 
@@ -62,7 +62,7 @@ pub fn scan(input: &str) -> Result<Vec<Token>, LexerError> {
62 62
         }
63 63
 
64 64
         if !matched {
65
-            return Err(LexerError::new("Unrecognized sequence"));
65
+            return Err(ElasticError::new("Unrecognized sequence"));
66 66
         }
67 67
     }
68 68
 

+ 9
- 0
src/lib.rs View File

@@ -3,3 +3,12 @@ pub mod error;
3 3
 pub mod lexer;
4 4
 pub mod parser;
5 5
 pub mod token;
6
+
7
+use compiler::Query;
8
+use error::ElasticError;
9
+
10
+pub fn compile(input: &str) -> Result<Query, ElasticError> {
11
+    let tokens = lexer::scan(&input)?;
12
+    let select = parser::parse(tokens)?;
13
+    compiler::compile(select)
14
+}

+ 0
- 16
src/main.rs View File

@@ -1,16 +0,0 @@
1
-use elastic::parser;
2
-use elastic::{compiler::compile, lexer};
3
-
4
-use std::env;
5
-
6
-fn main() -> std::io::Result<()> {
7
-    let args: Vec<String> = env::args().into_iter().skip(1).collect();
8
-    let input = args.join(" ");
9
-    let tokens = lexer::scan(&input)?;
10
-    let select = parser::parse(tokens)?;
11
-    let result = compile(select)?;
12
-
13
-    println!("{}", result);
14
-
15
-    Ok(())
16
-}

+ 8
- 8
src/parser.rs View File

@@ -1,6 +1,6 @@
1 1
 use std::{iter::Peekable, slice::Iter};
2 2
 
3
-use crate::error::ParserError;
3
+use crate::error::ElasticError;
4 4
 use crate::token::{Token, TokenType};
5 5
 
6 6
 #[derive(Debug, PartialEq)]
@@ -28,11 +28,11 @@ impl Identifier {
28 28
     }
29 29
 }
30 30
 
31
-pub fn parse(tokens: Vec<Token>) -> Result<Select, ParserError> {
31
+pub fn parse(tokens: Vec<Token>) -> Result<Select, ElasticError> {
32 32
     parse_select_expression(&mut tokens.iter().peekable())
33 33
 }
34 34
 
35
-fn parse_select_expression(tokens: &mut Peekable<Iter<Token>>) -> Result<Select, ParserError> {
35
+fn parse_select_expression(tokens: &mut Peekable<Iter<Token>>) -> Result<Select, ElasticError> {
36 36
     eat(tokens, TokenType::Select)?;
37 37
     let fields = parse_fields(tokens)?;
38 38
     eat(tokens, TokenType::From)?;
@@ -41,7 +41,7 @@ fn parse_select_expression(tokens: &mut Peekable<Iter<Token>>) -> Result<Select,
41 41
     Ok(Select { fields, source })
42 42
 }
43 43
 
44
-fn parse_fields(tokens: &mut Peekable<Iter<Token>>) -> Result<Vec<Field>, ParserError> {
44
+fn parse_fields(tokens: &mut Peekable<Iter<Token>>) -> Result<Vec<Field>, ElasticError> {
45 45
     if current_token_is(tokens, TokenType::Star) {
46 46
         eat(tokens, TokenType::Star).and_then(|_| Ok(vec![Field::Star]))
47 47
     } else {
@@ -72,19 +72,19 @@ fn current_token_is(tokens: &mut Peekable<Iter<Token>>, token_type: TokenType) -
72 72
 fn eat<'a>(
73 73
     tokens: &'a mut Peekable<Iter<Token>>,
74 74
     token_type: TokenType,
75
-) -> Result<&'a Token, ParserError> {
75
+) -> Result<&'a Token, ElasticError> {
76 76
     match tokens.next() {
77 77
         Some(token) => {
78 78
             if token.token_type == token_type {
79 79
                 Ok(token)
80 80
             } else {
81
-                Err(ParserError::new(&format!(
81
+                Err(ElasticError::new(&format!(
82 82
                     "Expected {} but got {}",
83 83
                     token_type, token.token_type
84 84
                 )))
85 85
             }
86 86
         }
87
-        None => Err(ParserError::new("Unexpected end of tokens")),
87
+        None => Err(ElasticError::new("Unexpected end of tokens")),
88 88
     }
89 89
 }
90 90
 
@@ -93,7 +93,7 @@ mod tests {
93 93
     use super::*;
94 94
     use crate::lexer::scan;
95 95
 
96
-    fn _parse(input: &str) -> Result<Select, ParserError> {
96
+    fn _parse(input: &str) -> Result<Select, ElasticError> {
97 97
         let tokens = scan(input).unwrap();
98 98
         parse(tokens)
99 99
     }

Loading…
Cancel
Save