Remaniement intensif de la structure du projet

This commit is contained in:
Hippolyte Chauvin 2023-11-01 14:57:23 +01:00
commit 2911811010
3 changed files with 60 additions and 21 deletions

37
src/input.rs Normal file
View file

@ -0,0 +1,37 @@
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) -> usize {
self.buffer.clear();
let result: stdio::Result<usize> = stdin().read_line(&mut self.buffer);
if result.is_err() {
eprintln!("error: invalid UTF-8 characters were read");
self.buffer.clear();
1
} else {
self.buffer = self.buffer.trim().to_string();
result.unwrap()
}
}
pub fn is_empty(&self) -> bool {
self.buffer.is_empty()
}
pub fn get_buffer(&self) -> &String {
&self.buffer
}
}

View file

@ -1,34 +1,27 @@
use std::io; use std::process::exit;
use std::io::Write;
mod input;
mod output;
mod parser; mod parser;
fn main() { fn main() {
let mut buffer = String::new(); let mut buffer = input::Buffer::new();
let mut result_bytes_read: io::Result<usize>;
let mut bytes_read: usize = 1; let mut bytes_read: usize = 1;
let mut command_vec: Vec<String>; let mut command_line: String;
let mut argv: Vec<String>;
while bytes_read != 0 { while bytes_read != 0 {
buffer.clear(); output::print_ps1();
bytes_read = buffer.read_line();
print!("$ "); if ! buffer.is_empty() {
if io::stdout().flush().is_err() { command_line = buffer.get_buffer().to_string();
eprintln!("error: can't fully flush stdout or reached EOF"); argv = parser::parse(&command_line);
} println!("{:?}", argv);
result_bytes_read = io::stdin().read_line(&mut buffer);
if result_bytes_read.is_err() {
eprintln!("error: invalid UTF-8 characters were read");
bytes_read = 1;
} else {
bytes_read = result_bytes_read.unwrap();
if bytes_read != 0 {
command_vec = parser::parse(&buffer);
println!("{:?}", command_vec);
}
} }
} }
println!(); println!();
exit(0);
} }

9
src/output.rs Normal file
View file

@ -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");
}
}