diff --git a/src/command.rs b/src/command.rs index 10b1a97..d029889 100644 --- a/src/command.rs +++ b/src/command.rs @@ -112,16 +112,21 @@ impl From for CommandParseError { #[cfg(test)] mod tests { use crate::{ - command::Command, meta_commands::MetaCommand, parser::parse, statements::Statement, + command::Command, + meta_commands::MetaCommand, + parser::parse, + statements::{Row, Statement}, }; use insta::{assert_debug_snapshot, assert_snapshot}; #[test] fn test_execute_insert_statement() { let statement: Command = Statement::Insert { - id: 45, - username: String::from("user"), - email: String::from("user@example.org"), + row: Row { + id: 45, + username: String::from("user"), + email: String::from("user@example.org"), + }, } .into(); let result = statement.execute().display(); diff --git a/src/parser.rs b/src/parser.rs index 181f3e3..3803fb6 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -2,7 +2,7 @@ use std::collections::VecDeque; use crate::{ command::{Command, CommandParseError, ExpectedToken}, - statements::Statement, + statements::{Row, Statement}, tokens::{Location, Token, TokenData, tokenize}, }; @@ -230,9 +230,11 @@ fn parse_insert_command( expect_semicolon(tokens)?; Ok(Command::Statement(Statement::Insert { - id, - username, - email, + row: Row { + id, + username, + email, + }, })) } diff --git a/src/snapshots/osdb__parser__tests__parse_insert_command.snap b/src/snapshots/osdb__parser__tests__parse_insert_command.snap index 954c83e..6a3808c 100644 --- a/src/snapshots/osdb__parser__tests__parse_insert_command.snap +++ b/src/snapshots/osdb__parser__tests__parse_insert_command.snap @@ -1,14 +1,16 @@ --- source: src/parser.rs -expression: "parse(file.clone(),\nString::from(r#\"insert 1 \"username\" \"email@example.com\"\"#))" +expression: "parse(file.clone(),\nString::from(r#\"insert 1 \"username\" \"email@example.com\";\"#))" --- Ok( [ Statement( Insert { - id: 1, - username: "username", - email: "email@example.com", + row: Row { + id: 1, + username: "username", + email: "email@example.com", + }, }, ), ], diff --git a/src/snapshots/osdb__parser__tests__parse_multiple_statements_with_insert.snap b/src/snapshots/osdb__parser__tests__parse_multiple_statements_with_insert.snap index 0305714..a42a445 100644 --- a/src/snapshots/osdb__parser__tests__parse_multiple_statements_with_insert.snap +++ b/src/snapshots/osdb__parser__tests__parse_multiple_statements_with_insert.snap @@ -9,9 +9,11 @@ Ok( ), Statement( Insert { - id: 1, - username: "user", - email: "email@test.com", + row: Row { + id: 1, + username: "user", + email: "email@test.com", + }, }, ), Statement( diff --git a/src/statements.rs b/src/statements.rs index cf5b3fa..68e77a8 100644 --- a/src/statements.rs +++ b/src/statements.rs @@ -1,10 +1,13 @@ +#[derive(Debug)] +pub struct Row { + pub id: i64, + pub username: String, + pub email: String, +} + #[derive(Debug)] pub enum Statement { - Insert { - id: i64, - username: String, - email: String, - }, + Insert { row: Row }, Select, } @@ -15,13 +18,16 @@ pub struct StatementExecuteResult { impl Statement { pub fn execute(&self) -> StatementExecuteResult { match self { - Statement::Insert { - id, - username, - email, - } => StatementExecuteResult { - msg: format!("insert {id:?} {username:?} {email:?}"), - }, + Statement::Insert { row } => { + let Row { + id, + username, + email, + } = row; + StatementExecuteResult { + msg: format!("insert {id:?} {username:?} {email:?}"), + } + } Statement::Select => StatementExecuteResult { msg: String::from("select"), },