From 035c598646d54ac0dc55fb9161e2f42e1863529e Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 9 Jun 2019 10:52:47 -0400 Subject: refactor --- src/main.rs | 1 + src/parser.rs | 14 ++++++++++++++ src/process.rs | 7 ++++--- src/repl.rs | 5 +++++ 4 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 src/parser.rs (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 1d78e10..d283021 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +mod parser; mod process; mod readline; mod repl; diff --git a/src/parser.rs b/src/parser.rs new file mode 100644 index 0000000..cc86020 --- /dev/null +++ b/src/parser.rs @@ -0,0 +1,14 @@ +#[derive(Debug)] +pub enum Error { + CommandRequired, +} + +pub fn parse(line: &str) -> Result<(String, Vec), Error> { + // TODO + let mut tokens = line.split_whitespace().map(|s| s.to_string()); + if let Some(cmd) = tokens.next() { + Ok((cmd, tokens.collect())) + } else { + Err(Error::CommandRequired) + } +} diff --git a/src/process.rs b/src/process.rs index 0c7b61f..5a82e5c 100644 --- a/src/process.rs +++ b/src/process.rs @@ -6,6 +6,7 @@ use tokio_pty_process::CommandExt; #[derive(Debug)] pub enum Error { IOError(std::io::Error), + ParserError(crate::parser::Error), } pub fn spawn(line: &str) -> Result { @@ -34,10 +35,10 @@ impl RunningProcess { let pty = tokio_pty_process::AsyncPtyMaster::open() .map_err(|e| Error::IOError(e))?; - let mut argv: Vec<_> = line.split(' ').collect(); - let cmd = argv.remove(0); + let (cmd, args) = + crate::parser::parse(line).map_err(|e| Error::ParserError(e))?; let process = std::process::Command::new(cmd) - .args(&argv) + .args(&args) .spawn_pty_async(&pty) .map_err(|e| Error::IOError(e))?; diff --git a/src/repl.rs b/src/repl.rs index e05624e..e54eb22 100644 --- a/src/repl.rs +++ b/src/repl.rs @@ -42,6 +42,11 @@ pub fn repl() { Err(Error::ReadError(crate::readline::Error::EOF)) => { return Ok((done, true)); } + Err(Error::EvalError(crate::process::Error::ParserError( + crate::parser::Error::CommandRequired, + ))) => { + return Ok((done, false)); + } Err(e) => { let stderr = std::io::stderr(); let mut stderr = stderr.lock(); -- cgit v1.2.3-54-g00ecf