From dfd5ebfa25d799399de0d15cb38ba47f5069647b Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 26 Feb 2022 18:08:40 -0500 Subject: reduce typing --- src/env.rs | 6 +++--- src/info.rs | 12 +++++++----- src/main.rs | 2 +- src/parse/ast.rs | 10 +++++----- src/prelude.rs | 1 + src/runner/builtins/command.rs | 17 +++++++---------- src/runner/builtins/mod.rs | 20 ++++++++++---------- src/runner/command.rs | 9 ++++----- src/runner/mod.rs | 12 ++++++------ src/runner/sys.rs | 6 +++--- src/shell/history/mod.rs | 6 +++--- src/shell/history/pty.rs | 4 ++-- src/shell/mod.rs | 6 +++--- src/shell/readline.rs | 2 +- 14 files changed, 56 insertions(+), 57 deletions(-) (limited to 'src') diff --git a/src/env.rs b/src/env.rs index 3a6d8b5..72a69d1 100644 --- a/src/env.rs +++ b/src/env.rs @@ -16,7 +16,7 @@ const __NBSH_LATEST_STATUS: &str = "__NBSH_LATEST_STATUS"; const __NBSH_PREV_PWD: &str = "__NBSH_PREV_PWD"; impl Env { - pub fn new() -> anyhow::Result { + pub fn new() -> Result { let pwd = std::env::current_dir()?; Ok(Self::V0(V0 { pwd: pwd.clone(), @@ -26,7 +26,7 @@ impl Env { })) } - pub fn new_from_env() -> anyhow::Result { + pub fn new_from_env() -> Result { let pwd = std::env::current_dir()?; Ok(Self::V0(V0 { pwd: pwd.clone(), @@ -111,7 +111,7 @@ impl Env { } } - pub fn update(&mut self) -> anyhow::Result<()> { + pub fn update(&mut self) -> Result<()> { let idx = self.idx(); let status = self.latest_status(); let prev_pwd = self.prev_pwd(); diff --git a/src/info.rs b/src/info.rs index bd94205..dc62125 100644 --- a/src/info.rs +++ b/src/info.rs @@ -1,12 +1,14 @@ -pub fn user() -> anyhow::Result { +use crate::prelude::*; + +pub fn user() -> Result { Ok(users::get_current_username() - .ok_or_else(|| anyhow::anyhow!("couldn't get username"))? + .ok_or_else(|| anyhow!("couldn't get username"))? .to_string_lossy() .into_owned()) } #[allow(clippy::unnecessary_wraps)] -pub fn prompt_char() -> anyhow::Result { +pub fn prompt_char() -> Result { if users::get_current_uid() == 0 { Ok("#".into()) } else { @@ -14,7 +16,7 @@ pub fn prompt_char() -> anyhow::Result { } } -pub fn hostname() -> anyhow::Result { +pub fn hostname() -> Result { let mut hostname = hostname::get()?.to_string_lossy().into_owned(); if let Some(idx) = hostname.find('.') { hostname.truncate(idx); @@ -23,7 +25,7 @@ pub fn hostname() -> anyhow::Result { } #[allow(clippy::unnecessary_wraps)] -pub fn time(offset: time::UtcOffset) -> anyhow::Result { +pub fn time(offset: time::UtcOffset) -> Result { Ok(crate::format::time( time::OffsetDateTime::now_utc().to_offset(offset), )) diff --git a/src/main.rs b/src/main.rs index e9c420d..65f1e52 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,7 +37,7 @@ struct Opt { } #[tokio::main] -async fn async_main(opt: Opt) -> anyhow::Result { +async fn async_main(opt: Opt) -> Result { if let Some(command) = opt.command { let mut shell_write = opt.status_fd.and_then(|fd| { nix::sys::stat::fstat(fd).ok().map(|_| { diff --git a/src/parse/ast.rs b/src/parse/ast.rs index 46aa63a..1f0cdc2 100644 --- a/src/parse/ast.rs +++ b/src/parse/ast.rs @@ -90,7 +90,7 @@ pub struct Pipeline { } impl Pipeline { - pub async fn eval(self, env: &Env) -> anyhow::Result { + pub async fn eval(self, env: &Env) -> Result { Ok(super::Pipeline { exes: self .exes @@ -124,7 +124,7 @@ struct Exe { } impl Exe { - async fn eval(self, env: &Env) -> anyhow::Result { + async fn eval(self, env: &Env) -> Result { let exe = self.exe.eval(env).await?; assert_eq!(exe.len(), 1); // TODO let exe = &exe[0]; @@ -212,7 +212,7 @@ pub struct Word { } impl Word { - pub async fn eval(self, env: &Env) -> anyhow::Result> { + pub async fn eval(self, env: &Env) -> Result> { let mut opts = glob::MatchOptions::new(); opts.require_literal_separator = true; opts.require_literal_leading_dot = true; @@ -436,7 +436,7 @@ impl Redirect { Self { from, to, dir } } - async fn eval(self, env: &Env) -> anyhow::Result { + async fn eval(self, env: &Env) -> Result { let to = if self.to.parts.len() == 1 { if let WordPart::Bareword(s) = &self.to.parts[0] { if let Some(fd) = s.strip_prefix('&') { @@ -514,7 +514,7 @@ fn parse_fd(s: &str) -> std::os::unix::io::RawFd { } } -fn expand_home(dir: &str) -> anyhow::Result { +fn expand_home(dir: &str) -> Result { if dir.starts_with('~') { let path: std::path::PathBuf = dir.into(); if let std::path::Component::Normal(prefix) = diff --git a/src/prelude.rs b/src/prelude.rs index 9fe5992..bc48955 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -1,4 +1,5 @@ pub use crate::env::Env; +pub use anyhow::{anyhow, Result}; pub use std::io::{Read as _, Write as _}; diff --git a/src/runner/builtins/command.rs b/src/runner/builtins/command.rs index 85f6594..16d8b40 100644 --- a/src/runner/builtins/command.rs +++ b/src/runner/builtins/command.rs @@ -51,7 +51,7 @@ impl Command { self.cfg.io.apply_redirects(redirects); } - pub fn spawn(self, env: &Env) -> anyhow::Result { + pub fn spawn(self, env: &Env) -> Result { let Self { f, exe, cfg } = self; (f)(exe, env, cfg) } @@ -187,7 +187,7 @@ impl Io { } } - pub fn read_line_stdin(&self) -> anyhow::Result<(String, bool)> { + pub fn read_line_stdin(&self) -> Result<(String, bool)> { let mut line = vec![]; if let Some(file) = self.stdin() { if let File::In(fh) = &*file { @@ -216,7 +216,7 @@ impl Io { Ok((line, done)) } - pub fn write_stdout(&self, buf: &[u8]) -> anyhow::Result<()> { + pub fn write_stdout(&self, buf: &[u8]) -> Result<()> { if let Some(file) = self.stdout() { if let File::Out(fh) = &*file { Ok((&*fh).write_all(buf)?) @@ -228,7 +228,7 @@ impl Io { } } - pub fn write_stderr(&self, buf: &[u8]) -> anyhow::Result<()> { + pub fn write_stderr(&self, buf: &[u8]) -> Result<()> { if let Some(file) = self.stderr() { if let File::Out(fh) = &*file { Ok((&*fh).write_all(buf)?) @@ -358,17 +358,14 @@ impl Child { self, ) -> std::pin::Pin< Box< - dyn std::future::Future< - Output = anyhow::Result, - > + Send + dyn std::future::Future> + + Send + Sync, >, > { Box::pin(async move { match self { - Self::Task(task) => { - task.await.map_err(|e| anyhow::anyhow!(e)) - } + Self::Task(task) => task.await.map_err(|e| anyhow!(e)), Self::Wrapped(child) => child.status().await, } }) diff --git a/src/runner/builtins/mod.rs b/src/runner/builtins/mod.rs index f212496..862f5b0 100644 --- a/src/runner/builtins/mod.rs +++ b/src/runner/builtins/mod.rs @@ -7,7 +7,7 @@ type Builtin = &'static (dyn for<'a> Fn( crate::parse::Exe, &'a Env, command::Cfg, -) -> anyhow::Result +) -> Result + Sync + Send); @@ -54,7 +54,7 @@ fn cd( exe: crate::parse::Exe, env: &Env, cfg: command::Cfg, -) -> anyhow::Result { +) -> Result { let prev_pwd = env.prev_pwd(); let home = env.var("HOME"); Ok(command::Child::new_task(move || { @@ -92,7 +92,7 @@ fn set( exe: crate::parse::Exe, _env: &Env, cfg: command::Cfg, -) -> anyhow::Result { +) -> Result { Ok(command::Child::new_task(move || { let k = if let Some(k) = exe.args().get(0).map(String::as_str) { k @@ -115,7 +115,7 @@ fn unset( exe: crate::parse::Exe, _env: &Env, cfg: command::Cfg, -) -> anyhow::Result { +) -> Result { Ok(command::Child::new_task(move || { let k = if let Some(k) = exe.args().get(0).map(String::as_str) { k @@ -136,7 +136,7 @@ fn echo( exe: crate::parse::Exe, _env: &Env, cfg: command::Cfg, -) -> anyhow::Result { +) -> Result { Ok(command::Child::new_task(move || { macro_rules! write_stdout { ($bytes:expr) => { @@ -167,7 +167,7 @@ fn read( exe: crate::parse::Exe, _env: &Env, cfg: command::Cfg, -) -> anyhow::Result { +) -> Result { Ok(command::Child::new_task(move || { let var = if let Some(var) = exe.args().get(0).map(String::as_str) { var @@ -191,7 +191,7 @@ fn and( mut exe: crate::parse::Exe, env: &Env, cfg: command::Cfg, -) -> anyhow::Result { +) -> Result { exe.shift(); if env.latest_status().success() { let mut cmd = crate::runner::Command::new(exe, cfg.io().clone()); @@ -207,7 +207,7 @@ fn or( mut exe: crate::parse::Exe, env: &Env, cfg: command::Cfg, -) -> anyhow::Result { +) -> Result { exe.shift(); if env.latest_status().success() { let status = env.latest_status(); @@ -223,7 +223,7 @@ fn command( mut exe: crate::parse::Exe, env: &Env, cfg: command::Cfg, -) -> anyhow::Result { +) -> Result { exe.shift(); let mut cmd = crate::runner::Command::new_binary(exe); cfg.setup_command(&mut cmd); @@ -234,7 +234,7 @@ fn builtin( mut exe: crate::parse::Exe, env: &Env, cfg: command::Cfg, -) -> anyhow::Result { +) -> Result { exe.shift(); let mut cmd = crate::runner::Command::new_builtin(exe, cfg.io().clone()); cfg.setup_command(&mut cmd); diff --git a/src/runner/command.rs b/src/runner/command.rs index efbf166..b0120fc 100644 --- a/src/runner/command.rs +++ b/src/runner/command.rs @@ -94,7 +94,7 @@ impl Command { self.pre_exec = Some(Box::new(f)); } - pub fn spawn(self, env: &Env) -> anyhow::Result { + pub fn spawn(self, env: &Env) -> Result { let Self { inner, exe, @@ -127,7 +127,7 @@ impl Command { // functions unsafe { cmd.pre_exec(pre_exec) }; Ok(Child::Binary(cmd.spawn().map_err(|e| { - anyhow::anyhow!( + anyhow!( "{}: {}", crate::format::io_error(&e), exe.display() @@ -167,9 +167,8 @@ impl Child { self, ) -> std::pin::Pin< Box< - dyn std::future::Future< - Output = anyhow::Result, - > + Send + dyn std::future::Future> + + Send + Sync, >, > { diff --git a/src/runner/mod.rs b/src/runner/mod.rs index acdb127..622bdd7 100644 --- a/src/runner/mod.rs +++ b/src/runner/mod.rs @@ -71,7 +71,7 @@ enum Frame { pub async fn run( commands: &str, shell_write: &mut Option, -) -> anyhow::Result { +) -> Result { let mut env = Env::new_from_env()?; run_commands(commands, &mut env, shell_write).await?; let status = env.latest_status(); @@ -87,7 +87,7 @@ async fn run_commands( commands: &str, env: &mut Env, shell_write: &mut Option, -) -> anyhow::Result<()> { +) -> Result<()> { let commands = crate::parse::ast::Commands::parse(commands)?; let commands = commands.commands(); let mut pc = 0; @@ -232,7 +232,7 @@ async fn run_pipeline( pipeline: crate::parse::ast::Pipeline, env: &mut Env, shell_write: &mut Option, -) -> anyhow::Result<()> { +) -> Result<()> { write_event(shell_write, Event::RunPipeline(env.idx(), pipeline.span())) .await?; // Safety: pipelines are run serially, so only one copy of these will ever @@ -267,7 +267,7 @@ async fn run_pipeline( async fn write_event( fh: &mut Option, event: Event, -) -> anyhow::Result<()> { +) -> Result<()> { if let Some(fh) = fh { fh.write_all(&bincode::serialize(&event)?).await?; fh.flush().await?; @@ -280,7 +280,7 @@ fn spawn_children( env: &Env, io: &builtins::Io, interactive: bool, -) -> anyhow::Result<(Vec, Option)> { +) -> Result<(Vec, Option)> { let mut cmds: Vec<_> = pipeline .into_exes() .map(|exe| Command::new(exe, io.clone())) @@ -325,7 +325,7 @@ async fn wait_children( ) -> std::process::ExitStatus { enum Res { Child(nix::Result), - Builtin((anyhow::Result, bool)), + Builtin((Result, bool)), } macro_rules! bail { diff --git a/src/runner/sys.rs b/src/runner/sys.rs index 413882c..b6a9428 100644 --- a/src/runner/sys.rs +++ b/src/runner/sys.rs @@ -2,7 +2,7 @@ use crate::runner::prelude::*; const PID0: nix::unistd::Pid = nix::unistd::Pid::from_raw(0); -pub fn pipe() -> anyhow::Result<(std::fs::File, std::fs::File)> { +pub fn pipe() -> Result<(std::fs::File, std::fs::File)> { let (r, w) = nix::unistd::pipe2(nix::fcntl::OFlag::O_CLOEXEC)?; // Safety: these file descriptors were just returned by pipe2 above, and // are only available in this function, so nothing else can be accessing @@ -12,7 +12,7 @@ pub fn pipe() -> anyhow::Result<(std::fs::File, std::fs::File)> { })) } -pub fn set_foreground_pg(pg: nix::unistd::Pid) -> anyhow::Result<()> { +pub fn set_foreground_pg(pg: nix::unistd::Pid) -> Result<()> { let pty = nix::fcntl::open( "/dev/tty", nix::fcntl::OFlag::empty(), @@ -60,7 +60,7 @@ pub fn setpgid_child(pg: Option) -> std::io::Result<()> { pub fn setpgid_parent( pid: nix::unistd::Pid, pg: Option, -) -> anyhow::Result<()> { +) -> Result<()> { nix::unistd::setpgid(pid, pg.unwrap_or(PID0)) // the child already called exec, so it must have already called // setpgid itself diff --git a/src/shell/history/mod.rs b/src/shell/history/mod.rs index 6d38891..e7202be 100644 --- a/src/shell/history/mod.rs +++ b/src/shell/history/mod.rs @@ -26,7 +26,7 @@ impl History { focus: Option, scrolling: bool, offset: time::UtcOffset, - ) -> anyhow::Result<()> { + ) -> Result<()> { let mut used_lines = repl_lines; let mut cursor = None; for (idx, mut entry) in @@ -87,7 +87,7 @@ impl History { cmdline: &str, env: &Env, event_w: crate::shell::event::Writer, - ) -> anyhow::Result { + ) -> Result { let (input_w, input_r) = tokio::sync::mpsc::unbounded_channel(); let (resize_w, resize_r) = tokio::sync::mpsc::unbounded_channel(); @@ -279,7 +279,7 @@ async fn spawn_commands( pty: &pty::Pty, env: &mut Env, event_w: crate::shell::event::Writer, -) -> anyhow::Result { +) -> Result { enum Res { Read(crate::runner::Event), Exit(std::io::Result), diff --git a/src/shell/history/pty.rs b/src/shell/history/pty.rs index 58f872c..e96da41 100644 --- a/src/shell/history/pty.rs +++ b/src/shell/history/pty.rs @@ -12,7 +12,7 @@ impl Pty { input_r: tokio::sync::mpsc::UnboundedReceiver>, resize_r: tokio::sync::mpsc::UnboundedReceiver<(u16, u16)>, event_w: crate::shell::event::Writer, - ) -> anyhow::Result { + ) -> Result { let (close_w, close_r) = tokio::sync::mpsc::unbounded_channel(); let pty = pty_process::Pty::new()?; @@ -35,7 +35,7 @@ impl Pty { pub fn spawn( &self, mut cmd: pty_process::Command, - ) -> anyhow::Result { + ) -> Result { Ok(cmd.spawn(&*self.pts)?) } diff --git a/src/shell/mod.rs b/src/shell/mod.rs index c187272..88eb63e 100644 --- a/src/shell/mod.rs +++ b/src/shell/mod.rs @@ -9,7 +9,7 @@ mod history; mod prelude; mod readline; -pub async fn main() -> anyhow::Result { +pub async fn main() -> Result { let mut input = textmode::blocking::Input::new()?; let mut output = textmode::Output::new().await?; @@ -205,7 +205,7 @@ pub struct Shell { } impl Shell { - pub fn new(offset: time::UtcOffset) -> anyhow::Result { + pub fn new(offset: time::UtcOffset) -> Result { let mut env = Env::new()?; env.set_var("SHELL", std::env::current_exe()?); env.set_var("TERM", "screen"); @@ -225,7 +225,7 @@ impl Shell { pub async fn render( &self, out: &mut impl textmode::Textmode, - ) -> anyhow::Result<()> { + ) -> Result<()> { out.clear(); out.write(&vt100::Parser::default().screen().input_mode_formatted()); match self.scene { diff --git a/src/shell/readline.rs b/src/shell/readline.rs index f0fb950..463a7f0 100644 --- a/src/shell/readline.rs +++ b/src/shell/readline.rs @@ -26,7 +26,7 @@ impl Readline { git: Option<&super::git::Info>, focus: bool, offset: time::UtcOffset, - ) -> anyhow::Result<()> { + ) -> Result<()> { let pwd = env.pwd(); let user = crate::info::user()?; let hostname = crate::info::hostname()?; -- cgit v1.2.3-54-g00ecf