diff --git a/src/command.rs b/src/command.rs index 8f291c6..337e4c7 100644 --- a/src/command.rs +++ b/src/command.rs @@ -1,11 +1,38 @@ -use crate::meta_commands::{MetaCommand, MetaCommandParseError}; -use crate::statements::{Statement, StatementParseError}; +use crate::meta_commands::{MetaCommand, MetaCommandExecuteResult, MetaCommandParseError}; +use crate::statements::{Statement, StatementExecuteResult, StatementParseError}; pub enum Command { MetaCommand(MetaCommand), Statement(Statement), } +pub struct CommandExecuteResult { + pub should_exit: bool, +} + +impl From for CommandExecuteResult { + fn from(value: MetaCommandExecuteResult) -> Self { + Self { + should_exit: value.should_exit, + } + } +} + +impl From for CommandExecuteResult { + fn from(_value: StatementExecuteResult) -> Self { + Self { should_exit: false } + } +} + +impl Command { + pub fn execute(&self) -> CommandExecuteResult { + match self { + Command::MetaCommand(cmd) => cmd.execute().into(), + Command::Statement(stmt) => stmt.execute().into(), + } + } +} + pub enum CommandParseError { MetaCommand(MetaCommandParseError), Statement(StatementParseError), diff --git a/src/main.rs b/src/main.rs index 46092c7..9a221f9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,16 +5,13 @@ use osdb::command::Command; fn main() { startup_msg(); while let Some(input) = read_input() { - match input.parse() { - Ok(cmd) => match cmd { - Command::MetaCommand(cmd) => { - if cmd.execute().should_exit { - println!("Good-bye"); - break; - } + match input.parse::() { + Ok(cmd) => { + if cmd.execute().should_exit { + println!("Good-bye"); + break; } - Command::Statement(stmt) => stmt.execute(), - }, + } Err(err) => eprintln!("{err}"), } } diff --git a/src/statements.rs b/src/statements.rs index a108fb6..01c99b4 100644 --- a/src/statements.rs +++ b/src/statements.rs @@ -35,11 +35,14 @@ impl std::str::FromStr for Statement { } } +pub struct StatementExecuteResult {} + impl Statement { - pub fn execute(&self) { + pub fn execute(&self) -> StatementExecuteResult { match self { Statement::Insert => println!("insert"), Statement::Select => println!("select"), } + StatementExecuteResult {} } }