From 5cbbb16888321736a1f1a554af4966010ab58e1e Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 25 Jun 2019 23:13:10 -0400 Subject: simplify --- src/builtins.rs | 8 +++++--- src/eval.rs | 6 ++++-- src/parser.rs | 4 +++- src/process.rs | 6 ++++-- src/readline.rs | 20 ++++++++------------ src/repl.rs | 6 ++++-- 6 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/builtins.rs b/src/builtins.rs index 5c1f288..2c239ec 100644 --- a/src/builtins.rs +++ b/src/builtins.rs @@ -32,7 +32,9 @@ pub enum Error { ChdirUnknownHome, } -pub fn exec(cmd: &str, args: &[String]) -> Result { +pub type Result = std::result::Result; + +pub fn exec(cmd: &str, args: &[String]) -> Result { Builtin::new(cmd, args) } @@ -43,7 +45,7 @@ pub struct Builtin { } impl Builtin { - fn new(cmd: &str, args: &[String]) -> Result { + fn new(cmd: &str, args: &[String]) -> Result { match cmd { "cd" => Ok(Builtin { cmd: cmd.to_string(), @@ -82,7 +84,7 @@ impl futures::stream::Stream for Builtin { } } -fn cd(args: &[String]) -> Result<(), Error> { +fn cd(args: &[String]) -> Result<()> { ensure!( args.len() <= 1, TooManyParams { diff --git a/src/eval.rs b/src/eval.rs index a46045a..be14463 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -28,7 +28,9 @@ pub enum Error { }, } -pub fn eval(line: &str) -> Result { +pub type Result = std::result::Result; + +pub fn eval(line: &str) -> Result { Eval::new(line) } @@ -46,7 +48,7 @@ pub struct Eval { } impl Eval { - fn new(line: &str) -> Result { + fn new(line: &str) -> Result { let (cmd, args) = crate::parser::parse(line).context(ParserError { line })?; let builtin_stream = crate::builtins::exec(&cmd, &args); diff --git a/src/parser.rs b/src/parser.rs index 3a03d43..9d4125f 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -6,7 +6,9 @@ pub enum Error { CommandRequired, } -pub fn parse(line: &str) -> Result<(String, Vec), Error> { +pub type Result = std::result::Result; + +pub fn parse(line: &str) -> Result<(String, Vec)> { // TODO let mut tokens = line.split_whitespace().map(|s| s.to_string()); let cmd = tokens.next().context(CommandRequired)?; diff --git a/src/process.rs b/src/process.rs index d059803..80d5ced 100644 --- a/src/process.rs +++ b/src/process.rs @@ -34,7 +34,9 @@ pub enum Error { IntoRawMode { source: std::io::Error }, } -pub fn spawn(cmd: &str, args: &[String]) -> Result { +pub type Result = std::result::Result; + +pub fn spawn(cmd: &str, args: &[String]) -> Result { RunningProcess::new(cmd, args) } @@ -51,7 +53,7 @@ pub struct RunningProcess { } impl RunningProcess { - fn new(cmd: &str, args: &[String]) -> Result { + fn new(cmd: &str, args: &[String]) -> Result { let pty = tokio_pty_process::AsyncPtyMaster::open().context(OpenPty)?; diff --git a/src/readline.rs b/src/readline.rs index aee69c4..c167e56 100644 --- a/src/readline.rs +++ b/src/readline.rs @@ -22,7 +22,9 @@ pub enum Error { TerminalInputReadingThread { source: std::io::Error }, } -pub fn readline(prompt: &str, echo: bool) -> Result { +pub type Result = std::result::Result; + +pub fn readline(prompt: &str, echo: bool) -> Result { Readline::new(prompt, echo) } @@ -42,7 +44,7 @@ struct ReadlineState { } impl Readline { - fn new(prompt: &str, echo: bool) -> Result { + fn new(prompt: &str, echo: bool) -> Result { let screen = crossterm::RawScreen::into_raw_mode().context(IntoRawMode)?; @@ -59,9 +61,9 @@ impl Readline { }) } - fn with_reader(&mut self, f: F) -> Result + fn with_reader(&mut self, f: F) -> Result where - F: FnOnce(&KeyReader, &mut ReadlineState) -> Result, + F: FnOnce(&KeyReader, &mut ReadlineState) -> Result, { let mut reader_opt = self.reader.take(); if reader_opt.is_none() { @@ -240,7 +242,7 @@ impl futures::future::Future for Readline { self.with_reader(|reader, state| { loop { - match reader.try_recv() { + match reader.events.try_recv() { Ok(event) => { let a = state.process_event(event)?; if a.is_ready() { @@ -266,7 +268,7 @@ struct KeyReader { } impl KeyReader { - fn new(task: futures::task::Task) -> Result { + fn new(task: futures::task::Task) -> Result { let reader = crossterm::input().read_sync(); let (events_tx, events_rx) = std::sync::mpsc::channel(); let (quit_tx, quit_rx) = std::sync::mpsc::channel(); @@ -298,12 +300,6 @@ impl KeyReader { quit: quit_tx, }) } - - fn try_recv( - &self, - ) -> Result { - self.events.try_recv() - } } impl Drop for KeyReader { diff --git a/src/repl.rs b/src/repl.rs index 53e897c..3901e4c 100644 --- a/src/repl.rs +++ b/src/repl.rs @@ -4,7 +4,7 @@ use snafu::{ResultExt, Snafu}; use std::io::Write; #[derive(Debug, Snafu)] -enum Error { +pub enum Error { #[snafu(display("error during read: {}", source))] ReadError { source: crate::readline::Error }, @@ -15,6 +15,8 @@ enum Error { PrintError { source: std::io::Error }, } +pub type Result = std::result::Result; + pub fn repl() { let loop_stream = futures::stream::unfold(false, |done| { if done { @@ -90,7 +92,7 @@ fn eval( .map_err(|e| Error::EvalError { source: e }) } -fn print(out: &[u8]) -> Result<(), Error> { +fn print(out: &[u8]) -> Result<()> { let stdout = std::io::stdout(); let mut stdout = stdout.lock(); stdout.write(out).context(PrintError)?; -- cgit v1.2.3-54-g00ecf