|
1 |
| -use annotate_snippets::{Level, Renderer, Snippet}; |
2 | 1 | use rayql::{
|
3 | 2 | schema::error::{ParseError, TokenizationError},
|
4 | 3 | sql::error::{FunctionError, ToSQLError},
|
5 | 4 | };
|
6 | 5 |
|
7 |
| -struct ErrorMessageBuilder<'a> { |
8 |
| - code: &'a str, |
9 |
| - title: String, |
10 |
| - label: String, |
11 |
| - line: usize, |
12 |
| - span: std::ops::Range<usize>, |
13 |
| -} |
14 |
| - |
15 |
| -impl<'a> ErrorMessageBuilder<'a> { |
16 |
| - fn new(code: &'a str) -> Self { |
17 |
| - ErrorMessageBuilder { |
18 |
| - code, |
19 |
| - title: String::new(), |
20 |
| - label: String::new(), |
21 |
| - line: 0, |
22 |
| - span: 0..0, |
23 |
| - } |
24 |
| - } |
25 |
| - |
26 |
| - fn with_title(mut self, title: String) -> Self { |
27 |
| - self.title = title; |
28 |
| - self |
29 |
| - } |
30 |
| - |
31 |
| - fn with_label(mut self, label: String) -> Self { |
32 |
| - self.label = label; |
33 |
| - self |
34 |
| - } |
35 |
| - |
36 |
| - fn with_line(mut self, line: usize) -> Self { |
37 |
| - self.line = line; |
38 |
| - self |
39 |
| - } |
40 |
| - |
41 |
| - fn with_span(mut self, span: std::ops::Range<usize>) -> Self { |
42 |
| - self.span = span; |
43 |
| - self |
44 |
| - } |
45 |
| - |
46 |
| - fn build(self) -> String { |
47 |
| - let message = Level::Error.title(&self.title).snippet( |
48 |
| - Snippet::source(self.code.lines().nth(self.line - 1).unwrap()) |
49 |
| - .line_start(self.line) |
50 |
| - .origin("schema.rayql") |
51 |
| - .fold(true) |
52 |
| - .annotation(Level::Error.span(self.span).label(&self.label)), |
53 |
| - ); |
54 |
| - let renderer = Renderer::styled(); |
55 |
| - let rendered_message = renderer.render(message); |
56 |
| - rendered_message.to_string() |
57 |
| - } |
58 |
| -} |
59 |
| - |
60 | 6 | pub fn pretty_error_message(error: &ParseError, code: &str) -> String {
|
61 | 7 | match error {
|
62 | 8 | ParseError::TokenizationError(tokenization_error) => {
|
@@ -99,16 +45,17 @@ pub fn pretty_error_message(error: &ParseError, code: &str) -> String {
|
99 | 45 | }
|
100 | 46 | }
|
101 | 47 |
|
102 |
| -fn pretty_tokenization_error_message(tokenization_error: &TokenizationError, code: &str) -> String { |
| 48 | +fn pretty_tokenization_error_message( |
| 49 | + tokenization_error: &TokenizationError, |
| 50 | + _code: &str, |
| 51 | +) -> String { |
103 | 52 | match tokenization_error {
|
104 | 53 | TokenizationError::UnexpectedCharacter { char, line, column }
|
105 | 54 | | TokenizationError::UnknownEscapeSequence { char, line, column } => {
|
106 |
| - ErrorMessageBuilder::new(code) |
107 |
| - .with_title("Unexpected character".to_string()) |
108 |
| - .with_label(format!("Unexpected character '{}'", char)) |
109 |
| - .with_line(*line) |
110 |
| - .with_span(*column - 1..*column) |
111 |
| - .build() |
| 55 | + format!( |
| 56 | + "Unexpected character '{}' at line {}, column {}", |
| 57 | + char, line, column |
| 58 | + ) |
112 | 59 | }
|
113 | 60 | TokenizationError::StringLiteralOpened { line, column } => {
|
114 | 61 | format!("String literal opened at line {}, column {}", line, column)
|
|
0 commit comments