Skip to content

Commit 3835a18

Browse files
authored
Merge pull request #85 from elichai/refactor
DRAFT: Simplify the API and inner implementation
2 parents 9a40594 + 1f775f4 commit 3835a18

File tree

10 files changed

+221
-291
lines changed

10 files changed

+221
-291
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn main() -> anyhow::Result<()> {
1515
let user = "matklad";
1616
let repo = "xshell";
1717
cmd!(sh, "git clone https://github.com/{user}/{repo}.git").run()?;
18-
sh.change_dir(repo);
18+
sh.set_current_dir(repo);
1919

2020
let test_args = ["-Zunstable-options", "--report-time"];
2121
cmd!(sh, "cargo test -- {test_args...}").run()?;
@@ -29,7 +29,7 @@ fn main() -> anyhow::Result<()> {
2929

3030
cmd!(sh, "git tag {version}").run()?;
3131

32-
let dry_run = if sh.var("CI").is_ok() { None } else { Some("--dry-run") };
32+
let dry_run = if sh.env_var("CI").is_ok() { None } else { Some("--dry-run") };
3333
cmd!(sh, "cargo publish {dry_run...}").run()?;
3434

3535
Ok(())

examples/ci.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn publish(sh: &Shell) -> Result<()> {
5757

5858
if current_branch == "master" && !tag_exists {
5959
// Could also just use `CARGO_REGISTRY_TOKEN` environmental variable.
60-
let token = sh.var("CRATES_IO_TOKEN").unwrap_or("DUMMY_TOKEN".to_string());
60+
let token = sh.env_var("CRATES_IO_TOKEN").unwrap_or("DUMMY_TOKEN".to_string());
6161
cmd!(sh, "git tag v{version}").run()?;
6262
cmd!(sh, "cargo publish --token {token} --package xshell-macros").run()?;
6363
cmd!(sh, "cargo publish --token {token} --package xshell").run()?;

examples/clone_and_publish.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
use xshell::{cmd, Shell};
33

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

77
let user = "matklad";
88
let repo = "xshell";
99
cmd!(sh, "git clone https://github.com/{user}/{repo}.git").run()?;
10-
sh.change_dir(repo);
10+
sh.set_current_dir(repo);
1111

1212
let test_args = ["-Zunstable-options", "--report-time"];
1313
cmd!(sh, "cargo test -- {test_args...}").run()?;
@@ -21,7 +21,7 @@ fn main() -> anyhow::Result<()> {
2121

2222
cmd!(sh, "git tag {version}").run()?;
2323

24-
let dry_run = if sh.var("CI").is_ok() { None } else { Some("--dry-run") };
24+
let dry_run = if sh.env_var("CI").is_ok() { None } else { Some("--dry-run") };
2525
cmd!(sh, "cargo publish {dry_run...}").run()?;
2626

2727
Ok(())

src/error.rs

+26-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
use std::{env, ffi::OsString, fmt, io, path::PathBuf, process::ExitStatus, string::FromUtf8Error};
2-
3-
use crate::{Cmd, CmdData};
1+
use std::{
2+
env,
3+
ffi::OsString,
4+
fmt, io,
5+
path::{Path, PathBuf},
6+
process::ExitStatus,
7+
string::FromUtf8Error,
8+
sync::Arc,
9+
};
10+
11+
use crate::Cmd;
412

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

1321
/// Note: this is intentionally not public.
1422
enum ErrorKind {
15-
CurrentDir { err: io::Error, path: Option<PathBuf> },
23+
CurrentDir { err: io::Error, path: Option<Arc<Path>> },
1624
Var { err: env::VarError, var: OsString },
1725
ReadFile { err: io::Error, path: PathBuf },
1826
ReadDir { err: io::Error, path: PathBuf },
@@ -21,10 +29,10 @@ enum ErrorKind {
2129
HardLink { err: io::Error, src: PathBuf, dst: PathBuf },
2230
CreateDir { err: io::Error, path: PathBuf },
2331
RemovePath { err: io::Error, path: PathBuf },
24-
CmdStatus { cmd: CmdData, status: ExitStatus },
25-
CmdIo { err: io::Error, cmd: CmdData },
26-
CmdUtf8 { err: FromUtf8Error, cmd: CmdData },
27-
CmdStdin { err: io::Error, cmd: CmdData },
32+
CmdStatus { cmd: Cmd, status: ExitStatus },
33+
CmdIo { err: io::Error, cmd: Cmd },
34+
CmdUtf8 { err: FromUtf8Error, cmd: Cmd },
35+
CmdStdin { err: io::Error, cmd: Cmd },
2836
}
2937

3038
impl From<ErrorKind> for Error {
@@ -91,7 +99,7 @@ impl fmt::Display for Error {
9199
},
92100
ErrorKind::CmdIo { err, cmd } => {
93101
if err.kind() == io::ErrorKind::NotFound {
94-
let prog = cmd.prog.display();
102+
let prog = cmd.prog.as_path().display();
95103
write!(f, "command not found: `{prog}`")
96104
} else {
97105
write!(f, "io error when running command `{cmd}`: {err}")
@@ -117,7 +125,7 @@ impl std::error::Error for Error {}
117125

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

@@ -153,23 +161,23 @@ impl Error {
153161
ErrorKind::RemovePath { err, path }.into()
154162
}
155163

156-
pub(crate) fn new_cmd_status(cmd: &Cmd<'_>, status: ExitStatus) -> Error {
157-
let cmd = cmd.data.clone();
164+
pub(crate) fn new_cmd_status(cmd: &Cmd, status: ExitStatus) -> Error {
165+
let cmd = cmd.clone();
158166
ErrorKind::CmdStatus { cmd, status }.into()
159167
}
160168

161-
pub(crate) fn new_cmd_io(cmd: &Cmd<'_>, err: io::Error) -> Error {
162-
let cmd = cmd.data.clone();
169+
pub(crate) fn new_cmd_io(cmd: &Cmd, err: io::Error) -> Error {
170+
let cmd = cmd.clone();
163171
ErrorKind::CmdIo { err, cmd }.into()
164172
}
165173

166-
pub(crate) fn new_cmd_utf8(cmd: &Cmd<'_>, err: FromUtf8Error) -> Error {
167-
let cmd = cmd.data.clone();
174+
pub(crate) fn new_cmd_utf8(cmd: &Cmd, err: FromUtf8Error) -> Error {
175+
let cmd = cmd.clone();
168176
ErrorKind::CmdUtf8 { err, cmd }.into()
169177
}
170178

171-
pub(crate) fn new_cmd_stdin(cmd: &Cmd<'_>, err: io::Error) -> Error {
172-
let cmd = cmd.data.clone();
179+
pub(crate) fn new_cmd_stdin(cmd: &Cmd, err: io::Error) -> Error {
180+
let cmd = cmd.clone();
173181
ErrorKind::CmdStdin { err, cmd }.into()
174182
}
175183
}

0 commit comments

Comments
 (0)