Skip to content

Commit

Permalink
Remove one dependency on anyhow (#49)
Browse files Browse the repository at this point in the history
prepare
  • Loading branch information
reubeno authored Jun 8, 2024
1 parent 82182f1 commit 37d4c39
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 29 deletions.
4 changes: 2 additions & 2 deletions brush-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ bench = false
workspace = true

[dependencies]
anyhow = "1.0.86"
async-recursion = "1.1.0"
async-trait = "0.1.80"
brush-parser = { path = "../brush-parser" }
brush-parser = { version = "0.1.0", path = "../brush-parser" }
cached = "0.51.3"
clap = { version = "4.5.4", features = ["derive"] }
fancy-regex = "0.13.0"
Expand Down Expand Up @@ -51,6 +50,7 @@ procfs = "0.16.0"
uzers = "0.12.0"

[dev-dependencies]
anyhow = "1.0.86"
criterion = { version = "0.5.1", features = ["async_tokio", "html_reports"] }

[target.'cfg(unix)'.dev-dependencies]
Expand Down
12 changes: 12 additions & 0 deletions brush-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ pub enum Error {
#[error("not a directory: {0}")]
NotADirectory(PathBuf),

/// The given path is a directory.
#[error("path is a directory")]
IsADirectory,

/// The given variable is not an array.
#[error("variable is not an array")]
NotArray,
Expand Down Expand Up @@ -150,6 +154,14 @@ pub enum Error {
/// An error occurred reading from procfs.
#[error("procfs error: {0}")]
ProcfsError(#[from] procfs::ProcError),

/// The given open file cannot be read from.
#[error("cannot read from {0}")]
OpenFileNotReadable(&'static str),

/// The given open file cannot be written to.
#[error("cannot write to {0}")]
OpenFileNotWritable(&'static str),
}

/// Convenience function for returning an error for unimplemented functionality.
Expand Down
36 changes: 18 additions & 18 deletions brush-core/src/openfiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,42 +97,42 @@ impl std::io::Read for OpenFile {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
match self {
OpenFile::Stdin => std::io::stdin().read(buf),
OpenFile::Stdout => Err(std::io::Error::other(anyhow::anyhow!(
"cannot read from stdout"
OpenFile::Stdout => Err(std::io::Error::other(error::Error::OpenFileNotReadable(
"stdout",
))),
OpenFile::Stderr => Err(std::io::Error::other(anyhow::anyhow!(
"cannot read from stderr"
OpenFile::Stderr => Err(std::io::Error::other(error::Error::OpenFileNotReadable(
"stderr",
))),
OpenFile::Null => Ok(0),
OpenFile::File(f) => f.read(buf),
OpenFile::PipeReader(reader) => reader.read(buf),
OpenFile::PipeWriter(_) => Err(std::io::Error::other(anyhow::anyhow!(
"cannot read from pipe writer"
))),
OpenFile::HereDocument(_) => Err(std::io::Error::other(anyhow::anyhow!(
"cannot read from here document"
))),
OpenFile::PipeWriter(_) => Err(std::io::Error::other(
error::Error::OpenFileNotReadable("pipe writer"),
)),
OpenFile::HereDocument(_) => Err(std::io::Error::other(
error::Error::OpenFileNotReadable("here document"),
)),
}
}
}

impl std::io::Write for OpenFile {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
match self {
OpenFile::Stdin => Err(std::io::Error::other(anyhow::anyhow!(
"cannot write to stdin"
OpenFile::Stdin => Err(std::io::Error::other(error::Error::OpenFileNotWritable(
"stdin",
))),
OpenFile::Stdout => std::io::stdout().write(buf),
OpenFile::Stderr => std::io::stderr().write(buf),
OpenFile::Null => Ok(buf.len()),
OpenFile::File(f) => f.write(buf),
OpenFile::PipeReader(_) => Err(std::io::Error::other(anyhow::anyhow!(
"cannot write to pipe reader"
))),
OpenFile::PipeReader(_) => Err(std::io::Error::other(
error::Error::OpenFileNotWritable("pipe reader"),
)),
OpenFile::PipeWriter(writer) => writer.write(buf),
OpenFile::HereDocument(_) => Err(std::io::Error::other(anyhow::anyhow!(
"cannot write to here document"
))),
OpenFile::HereDocument(_) => Err(std::io::Error::other(
error::Error::OpenFileNotWritable("here document"),
)),
}
}

Expand Down
5 changes: 1 addition & 4 deletions brush-core/src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,7 @@ impl Shell {
if file_metadata.is_dir() {
return Err(error::Error::FailedSourcingFile(
path.to_owned(),
std::io::Error::new(
std::io::ErrorKind::Other,
anyhow::anyhow!("path is a directory"),
),
std::io::Error::new(std::io::ErrorKind::Other, error::Error::IsADirectory),
));
}

Expand Down
4 changes: 2 additions & 2 deletions brush-interactive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ bench = false
workspace = true

[dependencies]
brush-parser = { path = "../brush-parser" }
brush-core = { path = "../brush-core" }
brush-parser = { version = "0.1.0", path = "../brush-parser" }
brush-core = { version = "0.1.0", path = "../brush-core" }
rustyline = { git = "https://github.com/reubeno/rustyline", rev = "fffa79df0751f95af9f8ec39191e92f3d4d7e21e", features = [
"derive",
] }
Expand Down
6 changes: 3 additions & 3 deletions brush-shell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ harness = false
workspace = true

[dependencies]
brush-interactive = { path = "../brush-interactive" }
brush-parser = { path = "../brush-parser" }
brush-core = { path = "../brush-core" }
brush-interactive = { version = "0.1.0", path = "../brush-interactive" }
brush-parser = { version = "0.1.0", path = "../brush-parser" }
brush-core = { version = "0.1.0", path = "../brush-core" }
clap = { version = "4.5.4", features = ["derive"] }
tokio = { version = "1.37.0", features = ["rt", "rt-multi-thread"] }
tracing = "0.1.40"
Expand Down

0 comments on commit 37d4c39

Please sign in to comment.