2025-05-10 10:41:30 +02:00
|
|
|
use crate::meta_commands::{MetaCommand, MetaCommandExecuteResult};
|
|
|
|
|
use crate::statements::{Statement, StatementExecuteResult};
|
2025-05-10 10:55:44 +02:00
|
|
|
use crate::tokens::{ScanError, Token};
|
2025-05-02 20:35:45 +02:00
|
|
|
|
2025-05-03 21:20:54 +02:00
|
|
|
#[derive(Debug)]
|
2025-05-02 20:35:45 +02:00
|
|
|
pub enum Command {
|
|
|
|
|
MetaCommand(MetaCommand),
|
|
|
|
|
Statement(Statement),
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-03 21:18:20 +02:00
|
|
|
#[derive(Debug)]
|
2025-05-02 20:53:12 +02:00
|
|
|
pub struct CommandExecuteResult {
|
|
|
|
|
pub should_exit: bool,
|
2025-05-03 19:21:05 +02:00
|
|
|
msg: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl CommandExecuteResult {
|
|
|
|
|
pub fn display(&self) -> String {
|
|
|
|
|
self.msg.to_string()
|
|
|
|
|
}
|
2025-05-02 20:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<MetaCommandExecuteResult> for CommandExecuteResult {
|
|
|
|
|
fn from(value: MetaCommandExecuteResult) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
should_exit: value.should_exit,
|
2025-05-03 19:21:05 +02:00
|
|
|
msg: String::new(),
|
2025-05-02 20:53:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<StatementExecuteResult> for CommandExecuteResult {
|
2025-05-03 19:21:05 +02:00
|
|
|
fn from(value: StatementExecuteResult) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
should_exit: false,
|
|
|
|
|
msg: value.msg,
|
|
|
|
|
}
|
2025-05-02 20:53:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Command {
|
|
|
|
|
pub fn execute(&self) -> CommandExecuteResult {
|
|
|
|
|
match self {
|
|
|
|
|
Command::MetaCommand(cmd) => cmd.execute().into(),
|
|
|
|
|
Command::Statement(stmt) => stmt.execute().into(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-03 21:20:54 +02:00
|
|
|
#[derive(Debug)]
|
2025-05-02 20:35:45 +02:00
|
|
|
pub enum CommandParseError {
|
2025-05-04 13:36:39 +02:00
|
|
|
Scan(ScanError),
|
2025-05-10 10:55:44 +02:00
|
|
|
UnexpectedToken(Token, &'static [&'static str]),
|
2025-05-02 20:35:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<MetaCommand> for Command {
|
|
|
|
|
fn from(value: MetaCommand) -> Self {
|
|
|
|
|
Command::MetaCommand(value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<Statement> for Command {
|
|
|
|
|
fn from(value: Statement) -> Self {
|
|
|
|
|
Command::Statement(value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-04 13:36:39 +02:00
|
|
|
impl From<ScanError> for CommandParseError {
|
|
|
|
|
fn from(value: ScanError) -> Self {
|
|
|
|
|
CommandParseError::Scan(value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-03 21:13:46 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2025-05-04 18:17:36 +02:00
|
|
|
use crate::{
|
|
|
|
|
command::Command, meta_commands::MetaCommand, parser::parse, statements::Statement,
|
|
|
|
|
};
|
2025-05-03 21:18:20 +02:00
|
|
|
use insta::{assert_debug_snapshot, assert_snapshot};
|
2025-05-03 21:13:46 +02:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_execute_insert_statement() {
|
|
|
|
|
let statement: Command = Statement::Insert.into();
|
|
|
|
|
let result = statement.execute().display();
|
|
|
|
|
assert_snapshot!(result);
|
|
|
|
|
}
|
2025-05-03 21:15:33 +02:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_execute_select_statement() {
|
|
|
|
|
let statement: Command = Statement::Select.into();
|
|
|
|
|
let result = statement.execute().display();
|
|
|
|
|
assert_snapshot!(result);
|
|
|
|
|
}
|
2025-05-03 21:18:20 +02:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_execute_exit_metacommand() {
|
|
|
|
|
assert_debug_snapshot!(Into::<Command>::into(MetaCommand::Exit).execute());
|
|
|
|
|
}
|
2025-05-03 21:20:54 +02:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_parse_wrong_statement() {
|
2025-05-04 18:17:36 +02:00
|
|
|
assert_debug_snapshot!(parse("<stdin>".to_string(), "salact".to_string()));
|
2025-05-03 21:20:54 +02:00
|
|
|
}
|
2025-05-03 21:21:59 +02:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_parse_wrong_meta_command() {
|
2025-05-04 18:17:36 +02:00
|
|
|
assert_debug_snapshot!(parse("<stdin>".to_string(), ".halp".to_string()));
|
2025-05-03 21:21:59 +02:00
|
|
|
}
|
2025-05-03 21:13:46 +02:00
|
|
|
}
|