refactor(command): display command results from special function

This commit is contained in:
Khaïs COLIN 2025-05-03 19:21:05 +02:00
parent 4435b375ef
commit 1f90d0acc3
4 changed files with 35 additions and 7 deletions

View file

@ -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

View file

@ -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<MetaCommandExecuteResult> for CommandExecuteResult {
fn from(value: MetaCommandExecuteResult) -> Self {
Self {
should_exit: value.should_exit,
msg: String::new(),
}
}
}
impl From<StatementExecuteResult> for CommandExecuteResult {
fn from(_value: StatementExecuteResult) -> Self {
Self { should_exit: false }
fn from(value: StatementExecuteResult) -> Self {
Self {
should_exit: false,
msg: value.msg,
}
}
}

View file

@ -8,7 +8,9 @@ fn main() {
while let Some(input) = read_input() {
match input.parse::<Command>() {
Ok(cmd) => {
if cmd.execute().should_exit {
let result = cmd.execute();
println!("{}", result.display());
if result.should_exit {
break;
}
}

View file

@ -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"),
}
StatementExecuteResult {}
Statement::Insert => StatementExecuteResult {
msg: String::from("insert"),
},
Statement::Select => StatementExecuteResult {
msg: String::from("select"),
},
}
}
}