Skip to content

Commit

Permalink
feat: Improve error messages when use options with dumpfile
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrDeveloper committed Aug 13, 2024
1 parent e7fd097 commit dcc3528
Showing 1 changed file with 35 additions and 16 deletions.
51 changes: 35 additions & 16 deletions crates/gitql-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1175,32 +1175,24 @@ fn parse_into_statement(

// Make sure user defined a file path as string literal
if *position >= tokens.len() || tokens[*position].kind != TokenKind::String {
return Err(
Diagnostic::error("Expect String literal as file path after OUTFILE keyword")
.with_location(get_safe_location(tokens, *position))
.as_boxed(),
);
return Err(Diagnostic::error(
"Expect String literal as file path after OUTFILE or DUMPFILE keyword",
)
.with_location(get_safe_location(tokens, *position))
.as_boxed());
}

let file_path = &tokens[*position].literal;

// Consume File path token
*position += 1;

// DUMPFILE take no option and should return the node here
if *file_format_kind == TokenKind::Dumpfile {
return Ok(Box::new(IntoStatement {
file_path: file_path.to_string(),
lines_terminated: String::new(),
fields_terminated: String::new(),
enclosed: String::new(),
}));
}
let is_dump_file = *file_format_kind == TokenKind::Dumpfile;

let mut lines_terminated = "\n";
let mut lines_terminated = if is_dump_file { "" } else { "\n" };
let mut lines_terminated_used = false;

let mut fields_termianted = ",";
let mut fields_termianted = if is_dump_file { "" } else { "," };
let mut fields_termianted_used = false;

let mut enclosed = "";
Expand All @@ -1210,6 +1202,15 @@ fn parse_into_statement(
let token = &tokens[*position];

if token.kind == TokenKind::Lines {
if is_dump_file {
return Err(Diagnostic::error(
"`LINES TERMINATED` option can't be used with INTO DUMPFILE",
)
.add_help("To customize the format replace `DUMPFILE` with `OUTFILE` option")
.with_location(tokens[*position].location)
.as_boxed());
}

if lines_terminated_used {
return Err(
Diagnostic::error("You already used `LINES TERMINATED` option")
Expand Down Expand Up @@ -1257,6 +1258,15 @@ fn parse_into_statement(
}

if token.kind == TokenKind::Fields {
if is_dump_file {
return Err(Diagnostic::error(
"`FIELDS TERMINATED` option can't be used with INTO DUMPFILE",
)
.add_help("To customize the format replace `DUMPFILE` with `OUTFILE` option")
.with_location(tokens[*position].location)
.as_boxed());
}

if fields_termianted_used {
return Err(
Diagnostic::error("You already used `FIELDS TERMINATED` option")
Expand Down Expand Up @@ -1304,6 +1314,15 @@ fn parse_into_statement(
}

if token.kind == TokenKind::Enclosed {
if is_dump_file {
return Err(Diagnostic::error(
"`ENCLOSED` option can't be used with INTO DUMPFILE",
)
.add_help("To customize the format replace `DUMPFILE` with `OUTFILE` option")
.with_location(tokens[*position].location)
.as_boxed());
}

if enclosed_used {
return Err(Diagnostic::error("You already used ENCLOSED option")
.with_location(tokens[*position].location)
Expand Down

0 comments on commit dcc3528

Please sign in to comment.