aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2019-06-25 23:13:10 -0400
committerJesse Luehrs <doy@tozt.net>2019-06-25 23:13:10 -0400
commit5cbbb16888321736a1f1a554af4966010ab58e1e (patch)
tree74547691c271bed54816aeaad41702575756aaf5
parent1c84d7ae8b7c2d7189e4d604a396506dd2608d4f (diff)
downloadnbsh-old-5cbbb16888321736a1f1a554af4966010ab58e1e.tar.gz
nbsh-old-5cbbb16888321736a1f1a554af4966010ab58e1e.zip
simplify
-rw-r--r--src/builtins.rs8
-rw-r--r--src/eval.rs6
-rw-r--r--src/parser.rs4
-rw-r--r--src/process.rs6
-rw-r--r--src/readline.rs20
-rw-r--r--src/repl.rs6
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<Builtin, Error> {
+pub type Result<T> = std::result::Result<T, Error>;
+
+pub fn exec(cmd: &str, args: &[String]) -> Result<Builtin> {
Builtin::new(cmd, args)
}
@@ -43,7 +45,7 @@ pub struct Builtin {
}
impl Builtin {
- fn new(cmd: &str, args: &[String]) -> Result<Self, Error> {
+ fn new(cmd: &str, args: &[String]) -> Result<Self> {
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<Eval, Error> {
+pub type Result<T> = std::result::Result<T, Error>;
+
+pub fn eval(line: &str) -> Result<Eval> {
Eval::new(line)
}
@@ -46,7 +48,7 @@ pub struct Eval {
}
impl Eval {
- fn new(line: &str) -> Result<Self, Error> {
+ fn new(line: &str) -> Result<Self> {
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<String>), Error> {
+pub type Result<T> = std::result::Result<T, Error>;
+
+pub fn parse(line: &str) -> Result<(String, Vec<String>)> {
// 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<RunningProcess, Error> {
+pub type Result<T> = std::result::Result<T, Error>;
+
+pub fn spawn(cmd: &str, args: &[String]) -> Result<RunningProcess> {
RunningProcess::new(cmd, args)
}
@@ -51,7 +53,7 @@ pub struct RunningProcess {
}
impl RunningProcess {
- fn new(cmd: &str, args: &[String]) -> Result<Self, Error> {
+ fn new(cmd: &str, args: &[String]) -> Result<Self> {
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<Readline, Error> {
+pub type Result<T> = std::result::Result<T, Error>;
+
+pub fn readline(prompt: &str, echo: bool) -> Result<Readline> {
Readline::new(prompt, echo)
}
@@ -42,7 +44,7 @@ struct ReadlineState {
}
impl Readline {
- fn new(prompt: &str, echo: bool) -> Result<Self, Error> {
+ fn new(prompt: &str, echo: bool) -> Result<Self> {
let screen =
crossterm::RawScreen::into_raw_mode().context(IntoRawMode)?;
@@ -59,9 +61,9 @@ impl Readline {
})
}
- fn with_reader<F, T>(&mut self, f: F) -> Result<T, Error>
+ fn with_reader<F, T>(&mut self, f: F) -> Result<T>
where
- F: FnOnce(&KeyReader, &mut ReadlineState) -> Result<T, Error>,
+ F: FnOnce(&KeyReader, &mut ReadlineState) -> Result<T>,
{
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<Self, Error> {
+ fn new(task: futures::task::Task) -> Result<Self> {
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<crossterm::InputEvent, std::sync::mpsc::TryRecvError> {
- 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<T> = std::result::Result<T, Error>;
+
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)?;