summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2022-02-26 18:08:40 -0500
committerJesse Luehrs <doy@tozt.net>2022-02-26 18:08:40 -0500
commitdfd5ebfa25d799399de0d15cb38ba47f5069647b (patch)
tree262c8b27621507bde8ab292bcd1b1fa04b44ca2f /src
parent07ed5629fff12ccb95216f7b07c91bb6e8bbfbc7 (diff)
downloadnbsh-dfd5ebfa25d799399de0d15cb38ba47f5069647b.tar.gz
nbsh-dfd5ebfa25d799399de0d15cb38ba47f5069647b.zip
reduce typing
Diffstat (limited to 'src')
-rw-r--r--src/env.rs6
-rw-r--r--src/info.rs12
-rw-r--r--src/main.rs2
-rw-r--r--src/parse/ast.rs10
-rw-r--r--src/prelude.rs1
-rw-r--r--src/runner/builtins/command.rs17
-rw-r--r--src/runner/builtins/mod.rs20
-rw-r--r--src/runner/command.rs9
-rw-r--r--src/runner/mod.rs12
-rw-r--r--src/runner/sys.rs6
-rw-r--r--src/shell/history/mod.rs6
-rw-r--r--src/shell/history/pty.rs4
-rw-r--r--src/shell/mod.rs6
-rw-r--r--src/shell/readline.rs2
14 files changed, 56 insertions, 57 deletions
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<Self> {
+ pub fn new() -> Result<Self> {
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<Self> {
+ pub fn new_from_env() -> Result<Self> {
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<String> {
+use crate::prelude::*;
+
+pub fn user() -> Result<String> {
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<String> {
+pub fn prompt_char() -> Result<String> {
if users::get_current_uid() == 0 {
Ok("#".into())
} else {
@@ -14,7 +16,7 @@ pub fn prompt_char() -> anyhow::Result<String> {
}
}
-pub fn hostname() -> anyhow::Result<String> {
+pub fn hostname() -> Result<String> {
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<String> {
}
#[allow(clippy::unnecessary_wraps)]
-pub fn time(offset: time::UtcOffset) -> anyhow::Result<String> {
+pub fn time(offset: time::UtcOffset) -> Result<String> {
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<i32> {
+async fn async_main(opt: Opt) -> Result<i32> {
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<super::Pipeline> {
+ pub async fn eval(self, env: &Env) -> Result<super::Pipeline> {
Ok(super::Pipeline {
exes: self
.exes
@@ -124,7 +124,7 @@ struct Exe {
}
impl Exe {
- async fn eval(self, env: &Env) -> anyhow::Result<super::Exe> {
+ async fn eval(self, env: &Env) -> Result<super::Exe> {
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<Vec<String>> {
+ pub async fn eval(self, env: &Env) -> Result<Vec<String>> {
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<super::Redirect> {
+ async fn eval(self, env: &Env) -> Result<super::Redirect> {
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<String> {
+fn expand_home(dir: &str) -> Result<String> {
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<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
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<usize>,
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<usize> {
+ ) -> Result<usize> {
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<std::process::ExitStatus> {
+) -> Result<std::process::ExitStatus> {
enum Res {
Read(crate::runner::Event),
Exit(std::io::Result<std::process::ExitStatus>),
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<Vec<u8>>,
resize_r: tokio::sync::mpsc::UnboundedReceiver<(u16, u16)>,
event_w: crate::shell::event::Writer,
- ) -> anyhow::Result<Self> {
+ ) -> Result<Self> {
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<tokio::process::Child> {
+ ) -> Result<tokio::process::Child> {
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<i32> {
+pub async fn main() -> Result<i32> {
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<Self> {
+ pub fn new(offset: time::UtcOffset) -> Result<Self> {
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()?;