add simple parser
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Kasper Juul Hermansen 2022-03-27 21:47:59 +02:00
parent 8e9efaca63
commit 1e819b3a24

View File

@ -6,6 +6,44 @@ struct InputBuffer {
buffer: Option<String>,
}
enum MetaCommandResult {
Success,
Unrecognized,
}
enum PrepareResult {
Success,
Unrecognized,
}
enum StatementType {
Insert,
Select,
}
struct Statement {
statement_type: StatementType,
}
impl Statement {
fn new(statement_type: StatementType) -> Self {
Self {
statement_type
}
}
pub(crate) fn execute(&self) {
match self.statement_type {
StatementType::Insert => {
println!("This is where you do an insert")
}
StatementType::Select => {
println!("This is where you do an select")
}
}
}
}
impl InputBuffer {
pub fn new() -> Self {
return Self {
@ -38,18 +76,40 @@ impl InputBuffer {
pub(crate) fn parse(&self) {
match &self.buffer {
Some(command) => {
match command.as_str().replace("\n", "").trim() {
".exit" => {
exit(0);
}
cmd => {
println!("Could not handle command: {cmd}")
if command.starts_with(".") {
Self::handle_meta_statement(command);
} else {
if let Ok(statement) = Self::prepare_statement(command) {
statement.execute()
} else {
println!("could not recognize statement");
}
}
}
None => {}
}
}
fn prepare_statement(command: &String) -> Result<Statement, String> {
if command.starts_with("insert") {
Ok(Statement::new(StatementType::Insert))
} else if command.starts_with("select") {
Ok(Statement::new(StatementType::Select))
} else {
Err(String::from("Unrecognized statement"))
}
}
fn handle_meta_statement(command: &String) {
match command.replace("\n", "").trim() {
".exit" => {
exit(0);
}
cmd => {
println!("Could not handle command: {cmd}")
}
}
}
}