Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: Simplify the API and inner implementation #85

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/clone_and_publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use xshell::{cmd, Shell};

fn main() -> anyhow::Result<()> {
let sh = Shell::new()?;
let mut sh = Shell::new()?;

let user = "matklad";
let repo = "xshell";
Expand Down
44 changes: 26 additions & 18 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
use std::{env, ffi::OsString, fmt, io, path::PathBuf, process::ExitStatus, string::FromUtf8Error};

use crate::{Cmd, CmdData};
use std::{
env,
ffi::OsString,
fmt, io,
path::{Path, PathBuf},
process::ExitStatus,
string::FromUtf8Error,
sync::Arc,
};

use crate::Cmd;

/// `Result` from std, with the error type defaulting to xshell's [`Error`].
pub type Result<T, E = Error> = std::result::Result<T, E>;
Expand All @@ -12,7 +20,7 @@ pub struct Error {

/// Note: this is intentionally not public.
enum ErrorKind {
CurrentDir { err: io::Error, path: Option<PathBuf> },
CurrentDir { err: io::Error, path: Option<Arc<Path>> },
Var { err: env::VarError, var: OsString },
ReadFile { err: io::Error, path: PathBuf },
ReadDir { err: io::Error, path: PathBuf },
Expand All @@ -21,10 +29,10 @@ enum ErrorKind {
HardLink { err: io::Error, src: PathBuf, dst: PathBuf },
CreateDir { err: io::Error, path: PathBuf },
RemovePath { err: io::Error, path: PathBuf },
CmdStatus { cmd: CmdData, status: ExitStatus },
CmdIo { err: io::Error, cmd: CmdData },
CmdUtf8 { err: FromUtf8Error, cmd: CmdData },
CmdStdin { err: io::Error, cmd: CmdData },
CmdStatus { cmd: Cmd, status: ExitStatus },
CmdIo { err: io::Error, cmd: Cmd },
CmdUtf8 { err: FromUtf8Error, cmd: Cmd },
CmdStdin { err: io::Error, cmd: Cmd },
}

impl From<ErrorKind> for Error {
Expand Down Expand Up @@ -91,7 +99,7 @@ impl fmt::Display for Error {
},
ErrorKind::CmdIo { err, cmd } => {
if err.kind() == io::ErrorKind::NotFound {
let prog = cmd.prog.display();
let prog = cmd.prog.as_path().display();
write!(f, "command not found: `{prog}`")
} else {
write!(f, "io error when running command `{cmd}`: {err}")
Expand All @@ -117,7 +125,7 @@ impl std::error::Error for Error {}

/// `pub(crate)` constructors, visible only in this crate.
impl Error {
pub(crate) fn new_current_dir(err: io::Error, path: Option<PathBuf>) -> Error {
pub(crate) fn new_current_dir(err: io::Error, path: Option<Arc<Path>>) -> Error {
ErrorKind::CurrentDir { err, path }.into()
}

Expand Down Expand Up @@ -153,23 +161,23 @@ impl Error {
ErrorKind::RemovePath { err, path }.into()
}

pub(crate) fn new_cmd_status(cmd: &Cmd<'_>, status: ExitStatus) -> Error {
let cmd = cmd.data.clone();
pub(crate) fn new_cmd_status(cmd: &Cmd, status: ExitStatus) -> Error {
let cmd = cmd.clone();
ErrorKind::CmdStatus { cmd, status }.into()
}

pub(crate) fn new_cmd_io(cmd: &Cmd<'_>, err: io::Error) -> Error {
let cmd = cmd.data.clone();
pub(crate) fn new_cmd_io(cmd: &Cmd, err: io::Error) -> Error {
let cmd = cmd.clone();
ErrorKind::CmdIo { err, cmd }.into()
}

pub(crate) fn new_cmd_utf8(cmd: &Cmd<'_>, err: FromUtf8Error) -> Error {
let cmd = cmd.data.clone();
pub(crate) fn new_cmd_utf8(cmd: &Cmd, err: FromUtf8Error) -> Error {
let cmd = cmd.clone();
ErrorKind::CmdUtf8 { err, cmd }.into()
}

pub(crate) fn new_cmd_stdin(cmd: &Cmd<'_>, err: io::Error) -> Error {
let cmd = cmd.data.clone();
pub(crate) fn new_cmd_stdin(cmd: &Cmd, err: io::Error) -> Error {
let cmd = cmd.clone();
ErrorKind::CmdStdin { err, cmd }.into()
}
}
Expand Down
Loading
Loading