diff options
author | Jesse Luehrs <doy@tozt.net> | 2022-02-26 18:08:40 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2022-02-26 18:08:40 -0500 |
commit | dfd5ebfa25d799399de0d15cb38ba47f5069647b (patch) | |
tree | 262c8b27621507bde8ab292bcd1b1fa04b44ca2f /src/runner | |
parent | 07ed5629fff12ccb95216f7b07c91bb6e8bbfbc7 (diff) | |
download | nbsh-dfd5ebfa25d799399de0d15cb38ba47f5069647b.tar.gz nbsh-dfd5ebfa25d799399de0d15cb38ba47f5069647b.zip |
reduce typing
Diffstat (limited to 'src/runner')
-rw-r--r-- | src/runner/builtins/command.rs | 17 | ||||
-rw-r--r-- | src/runner/builtins/mod.rs | 20 | ||||
-rw-r--r-- | src/runner/command.rs | 9 | ||||
-rw-r--r-- | src/runner/mod.rs | 12 | ||||
-rw-r--r-- | src/runner/sys.rs | 6 |
5 files changed, 30 insertions, 34 deletions
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<Child> { + pub fn spawn(self, env: &Env) -> Result<Child> { 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<std::process::ExitStatus>, - > + Send + dyn std::future::Future<Output = Result<std::process::ExitStatus>> + + 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<command::Child> +) -> Result<command::Child> + Sync + Send); @@ -54,7 +54,7 @@ fn cd( exe: crate::parse::Exe, env: &Env, cfg: command::Cfg, -) -> anyhow::Result<command::Child> { +) -> Result<command::Child> { 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<command::Child> { +) -> Result<command::Child> { 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<command::Child> { +) -> Result<command::Child> { 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<command::Child> { +) -> Result<command::Child> { 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<command::Child> { +) -> Result<command::Child> { 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<command::Child> { +) -> Result<command::Child> { 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<command::Child> { +) -> Result<command::Child> { 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<command::Child> { +) -> Result<command::Child> { 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<command::Child> { +) -> Result<command::Child> { 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<Child> { + pub fn spawn(self, env: &Env) -> Result<Child> { 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<std::process::ExitStatus>, - > + Send + dyn std::future::Future<Output = Result<std::process::ExitStatus>> + + 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<tokio::fs::File>, -) -> anyhow::Result<i32> { +) -> Result<i32> { 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<tokio::fs::File>, -) -> 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<tokio::fs::File>, -) -> 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<tokio::fs::File>, 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<Child>, Option<nix::unistd::Pid>)> { +) -> Result<(Vec<Child>, Option<nix::unistd::Pid>)> { 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<nix::sys::wait::WaitStatus>), - Builtin((anyhow::Result<std::process::ExitStatus>, bool)), + Builtin((Result<std::process::ExitStatus>, 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<nix::unistd::Pid>) -> std::io::Result<()> { pub fn setpgid_parent( pid: nix::unistd::Pid, pg: Option<nix::unistd::Pid>, -) -> anyhow::Result<()> { +) -> Result<()> { nix::unistd::setpgid(pid, pg.unwrap_or(PID0)) // the child already called exec, so it must have already called // setpgid itself |