Skip to content

Commit

Permalink
ExecuteResult changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kakapio committed Apr 9, 2024
1 parent 216d2f5 commit b21bacd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::process::exit;

#[derive(PartialEq, Debug, Default)]
pub enum ExecuteResult {
Success,
Success(Option<Vec<Row>>),
#[default]
TableFull,
}
Expand Down Expand Up @@ -61,7 +61,7 @@ pub fn entrypoint() {
}

match execute_statement(statement, &mut table) {
ExecuteResult::Success => {
ExecuteResult::Success(data) => {
println!("Successfully executed...")
}
ExecuteResult::TableFull => {
Expand Down Expand Up @@ -96,7 +96,7 @@ fn execute_statement(statement: Statement, tb: &mut Table) -> ExecuteResult {
fn execute_insert(statement: Statement, table: &mut Table) -> ExecuteResult {
table.data.push(statement.row_instance.expect("Insert is missing row data."));

ExecuteResult::Success
ExecuteResult::Success(None)
}

fn execute_select(statement: Statement, table: &mut Table) -> ExecuteResult {
Expand All @@ -106,10 +106,12 @@ fn execute_select(statement: Statement, table: &mut Table) -> ExecuteResult {
for row in table.data.iter() {
println!("Found data: {:?}", row);
}
return ExecuteResult::Success;
return ExecuteResult::Success(Some(table.data.iter().cloned().collect()));
}



// Select specified an instance of data.
// Select cmd specified an instance of data.
for row in table.data.iter() {
/* I use 'as_ref()' here because otherwise the ownership of row_instance would go to unwrap().
* unwrap() consumes the given option which means iteration gets interrupted.
Expand All @@ -119,5 +121,5 @@ fn execute_select(statement: Statement, table: &mut Table) -> ExecuteResult {
}
}

ExecuteResult::Success
ExecuteResult::Success(Some(table.data.iter().filter(|row| row.id == statement.row_instance.as_ref().unwrap().id).cloned().collect()))
}
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct Statement {
pub row_instance: Option<Row>,
}

#[derive(PartialEq, Debug, Default)]
#[derive(PartialEq, Debug, Default, Clone)]
pub struct Row {
pub id: u32,
pub username: String,
Expand Down

0 comments on commit b21bacd

Please sign in to comment.