From 03d44852c373f0078e8faf45f49ca4fca8a1a191 Mon Sep 17 00:00:00 2001 From: Hippolyte Chauvin Date: Wed, 1 Nov 2023 13:48:25 +0100 Subject: [PATCH] Grand remaniement du code en modules partie 1 --- src/io/mod.rs | 38 +++++++++++++++++++++++++++++++++++++- src/{ => io}/parser/mod.rs | 0 src/main.rs | 34 ---------------------------------- 3 files changed, 37 insertions(+), 35 deletions(-) rename src/{ => io}/parser/mod.rs (100%) diff --git a/src/io/mod.rs b/src/io/mod.rs index 4c95642..dbbb0a4 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -1,3 +1,39 @@ +use std::io; +use io::{ + stdin, + stdout, + Write, +}; + +mod parser; + pub fn run() -> i32 { - todo!(); + let mut buffer = String::new(); + let mut bytes_read_result: io::Result; + let mut bytes_read: usize = 1; + + while bytes_read != 0 { + buffer.clear(); + + print!("$ "); + if stdout().flush().is_err() { + eprintln!("error: can't fully flush stdout or reached EOF"); + } + bytes_read_result = stdin().read_line(&mut buffer); + + if bytes_read_result.is_err() { + eprintln!("error: invalid UTF-8 characters were read"); + bytes_read = 1; + } else { + bytes_read = bytes_read_result.unwrap(); + if bytes_read != 0 { + let command_vec: Vec = parser::parse(&buffer); + println!("{:?}", command_vec); + } + } + } + + println!(); + + 0 } diff --git a/src/parser/mod.rs b/src/io/parser/mod.rs similarity index 100% rename from src/parser/mod.rs rename to src/io/parser/mod.rs diff --git a/src/main.rs b/src/main.rs index 14c038f..c05e11f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,43 +1,9 @@ -use std::io as stdio; -use stdio::{ - stdin, - stdout, - Write, -}; use std::process::exit; -mod parser; mod io; fn main() { - let mut buffer = String::new(); - let mut bytes_read_result: stdio::Result; - let mut bytes_read: usize = 1; - let last_exit_code: i32 = io::run(); exit(last_exit_code); - - while bytes_read != 0 { - buffer.clear(); - - print!("$ "); - if stdout().flush().is_err() { - eprintln!("error: can't fully flush stdout or reached EOF"); - } - bytes_read_result = stdin().read_line(&mut buffer); - - if bytes_read_result.is_err() { - eprintln!("error: invalid UTF-8 characters were read"); - bytes_read = 1; - } else { - bytes_read = bytes_read_result.unwrap(); - if bytes_read != 0 { - let command_vec: Vec = parser::parse(&buffer); - println!("{:?}", command_vec); - } - } - } - - println!(); }