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::meta_commands::{MetaCommand, MetaCommandExecuteResult, MetaCommandParseError};
|
||||||
use crate::statements::{Statement, StatementParseError};
|
use crate::statements::{Statement, StatementExecuteResult, StatementParseError};
|
||||||
|
|
||||||
pub enum Command {
|
pub enum Command {
|
||||||
MetaCommand(MetaCommand),
|
MetaCommand(MetaCommand),
|
||||||
Statement(Statement),
|
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 {
|
pub enum CommandParseError {
|
||||||
MetaCommand(MetaCommandParseError),
|
MetaCommand(MetaCommandParseError),
|
||||||
Statement(StatementParseError),
|
Statement(StatementParseError),
|
||||||
|
|
|
||||||
15
src/main.rs
15
src/main.rs
|
|
@ -5,16 +5,13 @@ use osdb::command::Command;
|
||||||
fn main() {
|
fn main() {
|
||||||
startup_msg();
|
startup_msg();
|
||||||
while let Some(input) = read_input() {
|
while let Some(input) = read_input() {
|
||||||
match input.parse() {
|
match input.parse::<Command>() {
|
||||||
Ok(cmd) => match cmd {
|
Ok(cmd) => {
|
||||||
Command::MetaCommand(cmd) => {
|
if cmd.execute().should_exit {
|
||||||
if cmd.execute().should_exit {
|
println!("Good-bye");
|
||||||
println!("Good-bye");
|
break;
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Command::Statement(stmt) => stmt.execute(),
|
}
|
||||||
},
|
|
||||||
Err(err) => eprintln!("{err}"),
|
Err(err) => eprintln!("{err}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,11 +35,14 @@ impl std::str::FromStr for Statement {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct StatementExecuteResult {}
|
||||||
|
|
||||||
impl Statement {
|
impl Statement {
|
||||||
pub fn execute(&self) {
|
pub fn execute(&self) -> StatementExecuteResult {
|
||||||
match self {
|
match self {
|
||||||
Statement::Insert => println!("insert"),
|
Statement::Insert => println!("insert"),
|
||||||
Statement::Select => println!("select"),
|
Statement::Select => println!("select"),
|
||||||
}
|
}
|
||||||
|
StatementExecuteResult {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue