From b21bacd04a90988ab1d032e5bb26e54ef0d3c6c2 Mon Sep 17 00:00:00 2001 From: Roshan Patel Date: Tue, 9 Apr 2024 09:45:38 -0400 Subject: [PATCH] ExecuteResult changes --- src/backend.rs | 14 ++++++++------ src/parser.rs | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/backend.rs b/src/backend.rs index 4c24dd1..6634d70 100644 --- a/src/backend.rs +++ b/src/backend.rs @@ -4,7 +4,7 @@ use std::process::exit; #[derive(PartialEq, Debug, Default)] pub enum ExecuteResult { - Success, + Success(Option>), #[default] TableFull, } @@ -61,7 +61,7 @@ pub fn entrypoint() { } match execute_statement(statement, &mut table) { - ExecuteResult::Success => { + ExecuteResult::Success(data) => { println!("Successfully executed...") } ExecuteResult::TableFull => { @@ -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 { @@ -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. @@ -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())) } diff --git a/src/parser.rs b/src/parser.rs index 7987607..345df73 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -32,7 +32,7 @@ pub struct Statement { pub row_instance: Option, } -#[derive(PartialEq, Debug, Default)] +#[derive(PartialEq, Debug, Default, Clone)] pub struct Row { pub id: u32, pub username: String,