summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
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(())
 }