From 4df874267c325dc4040f900699fa8bf2f8eb7d78 Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Fri, 4 Sep 2020 15:34:35 -0700 Subject: Added the basic loop structure, including a trivial prompt and reading a line of input. --- src/error.rs | 5 +++++ src/main.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/error.rs b/src/error.rs index 7054551..aa9e9d6 100644 --- a/src/error.rs +++ b/src/error.rs @@ -22,3 +22,8 @@ impl std::cmp::PartialEq for Error { } } +impl From for Error { + fn from(e: std::io::Error) -> Error { + Error::IO(e) + } +} diff --git a/src/main.rs b/src/main.rs index 5ca6d46..e176dc0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,19 @@ pub mod error; use error::Error; +use std::io; +use std::io::prelude::*; pub type Result = std::result::Result; +pub enum Input { + String(String), + End, +} + + fn main() -> Result<()> { std::process::exit(match repl() { Ok(()) => 0, @@ -20,6 +28,43 @@ fn main() -> Result<()> { fn repl() -> Result<()> { println!("Hello, terminal!"); + loop { + prompt()?; + + let input = read()?; + match input { + Input::String(string) => execute(string)?, + Input::End => break, + } + } + + Ok(()) +} + + +fn prompt() -> Result<()> { + print!("$ "); + io::stdout().flush()?; + + Ok(()) +} + + +fn read() -> Result { + let mut input = String::new(); + let n_bytes = io::stdin().read_line(&mut input)?; + + if n_bytes == 0 { + Ok(Input::End) + } else { + Ok(Input::String(input)) + } +} + + +fn execute(input: String) -> Result<()> { + println!("{}", input); + Ok(()) } -- cgit 1.4.1