summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorIrene Knapp <ireneista@gmail.com>2020-09-04 15:34:35 -0700
committerIrene Knapp <ireneista@gmail.com>2020-09-04 15:34:35 -0700
commit4df874267c325dc4040f900699fa8bf2f8eb7d78 (patch)
tree8732321d4b27b3af8a05b133e7037b291f23fa26 /src
parent0c8e06d588038ae6e24d1898bd33b3ee5a938640 (diff)
Added the basic loop structure, including a trivial prompt and reading a line of input.
Diffstat (limited to 'src')
-rw-r--r--src/error.rs5
-rw-r--r--src/main.rs45
2 files changed, 50 insertions, 0 deletions
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<std::io::Error> 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<T> = std::result::Result<T, Error>;
 
 
+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<Input> {
+    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(())
 }