From 1f90d0acc376c258bd3e4def947e9732915cca09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Sat, 3 May 2025 19:21:05 +0200 Subject: [PATCH] refactor(command): display command results from special function --- notes.org | 10 ++++++++++ src/command.rs | 15 +++++++++++++-- src/main.rs | 4 +++- src/statements.rs | 13 +++++++++---- 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/notes.org b/notes.org index 75dac93..7d047c4 100644 --- a/notes.org +++ b/notes.org @@ -42,6 +42,16 @@ CLOCK: [2025-05-03 sam. 19:00]--[2025-05-03 sam. 19:05] => 0:05 :PROPERTIES: :EFFORT: 10min :END: +:LOGBOOK: +CLOCK: [2025-05-03 sam. 19:08]--[2025-05-03 sam. 19:21] => 0:13 +:END: +*** DONE all display of command results must be made via a display method on CommandExecutionResult +:PROPERTIES: +:EFFORT: 10min +:END: +:LOGBOOK: +CLOCK: [2025-05-03 sam. 21:02]--[2025-05-03 sam. 21:09] => 0:07 +:END: ** TODO insta test select :PROPERTIES: :EFFORT: 10min diff --git a/src/command.rs b/src/command.rs index 337e4c7..89e21cc 100644 --- a/src/command.rs +++ b/src/command.rs @@ -8,19 +8,30 @@ pub enum Command { pub struct CommandExecuteResult { pub should_exit: bool, + msg: String, +} + +impl CommandExecuteResult { + pub fn display(&self) -> String { + self.msg.to_string() + } } impl From for CommandExecuteResult { fn from(value: MetaCommandExecuteResult) -> Self { Self { should_exit: value.should_exit, + msg: String::new(), } } } impl From for CommandExecuteResult { - fn from(_value: StatementExecuteResult) -> Self { - Self { should_exit: false } + fn from(value: StatementExecuteResult) -> Self { + Self { + should_exit: false, + msg: value.msg, + } } } diff --git a/src/main.rs b/src/main.rs index c5db917..a0fe1d0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,9 @@ fn main() { while let Some(input) = read_input() { match input.parse::() { Ok(cmd) => { - if cmd.execute().should_exit { + let result = cmd.execute(); + println!("{}", result.display()); + if result.should_exit { break; } } diff --git a/src/statements.rs b/src/statements.rs index 01c99b4..b755f75 100644 --- a/src/statements.rs +++ b/src/statements.rs @@ -35,14 +35,19 @@ impl std::str::FromStr for Statement { } } -pub struct StatementExecuteResult {} +pub struct StatementExecuteResult { + pub msg: String, +} impl Statement { pub fn execute(&self) -> StatementExecuteResult { match self { - Statement::Insert => println!("insert"), - Statement::Select => println!("select"), + Statement::Insert => StatementExecuteResult { + msg: String::from("insert"), + }, + Statement::Select => StatementExecuteResult { + msg: String::from("select"), + }, } - StatementExecuteResult {} } }