From 6ca8606081cd61a57dc310722dcbf006e4fa5f19 Mon Sep 17 00:00:00 2001 From: Hippolyte Chauvin Date: Wed, 1 Nov 2023 14:10:45 +0100 Subject: [PATCH] =?UTF-8?q?Impl=C3=A9mentation=20de=20Buffer=20et=20utilis?= =?UTF-8?q?ation=20dans=20io/mod.rs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/io/in.rs | 24 ++++++++++++++++++++++++ src/io/mod.rs | 21 +++++---------------- src/io/out.rs | 9 +++++++++ 3 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/io/in.rs b/src/io/in.rs index e69de29..ba176e5 100644 --- a/src/io/in.rs +++ b/src/io/in.rs @@ -0,0 +1,24 @@ +use std::io as stdio; +use stdio::stdin; + +pub struct Buffer { + buffer: String, +} + +impl Buffer { + pub fn new() -> Self { + Self { + buffer: String::new(), + } + } + + pub fn read_line(&mut self) -> stdio::Result { + self.buffer.clear(); + let result: stdio::Result = stdin().read_line(&mut self.buffer); + if result.is_err() { + eprintln!("error: invalid UTF-8 characters were read"); + } + + result + } +} diff --git a/src/io/mod.rs b/src/io/mod.rs index 7b8987e..e706f45 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -1,9 +1,4 @@ use std::io; -use io::{ - stdin, - stdout, - Write, -}; mod r#in; mod r#out; @@ -11,27 +6,21 @@ mod r#out; mod parser; pub fn run() -> i32 { - let mut buffer = String::new(); + let mut buffer = r#in::Buffer::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); + out::print_ps1(); + bytes_read_result = buffer.read_line(); 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); + //let command_vec: Vec = parser::parse(&buffer); + //println!("{:?}", command_vec); } } } diff --git a/src/io/out.rs b/src/io/out.rs index e69de29..3888585 100644 --- a/src/io/out.rs +++ b/src/io/out.rs @@ -0,0 +1,9 @@ +use std::io::stdout; +use std::io::Write; + +pub fn print_ps1() { + print!("$ "); + if stdout().flush().is_err() { + eprintln!("error: can't fully flush stdout or reached EOF"); + } +}