refactor(command): generic execute and result type
This commit is contained in:
parent
711d51090e
commit
5b6d878208
3 changed files with 39 additions and 12 deletions
|
|
@ -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<MetaCommandExecuteResult> for CommandExecuteResult {
|
||||
fn from(value: MetaCommandExecuteResult) -> Self {
|
||||
Self {
|
||||
should_exit: value.should_exit,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<StatementExecuteResult> 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),
|
||||
|
|
|
|||
|
|
@ -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) => {
|
||||
match input.parse::<Command>() {
|
||||
Ok(cmd) => {
|
||||
if cmd.execute().should_exit {
|
||||
println!("Good-bye");
|
||||
break;
|
||||
}
|
||||
}
|
||||
Command::Statement(stmt) => stmt.execute(),
|
||||
},
|
||||
Err(err) => eprintln!("{err}"),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue