Skip to content

Commit d813036

Browse files
Merge pull request #49 from crzysdrs/master
Support iterators that can produce OsStr for `$[value]`
2 parents 6f16a1b + 339e789 commit d813036

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

macros/src/parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ impl<I: Iterator<Item = ParseArg>> Parser<I> {
8080
ret.extend(quote!(.add_redirect(#redirect)));
8181
}
8282
ParseArg::ArgStr(opt) => {
83-
ret.extend(quote!(.add_arg(#opt.into_os_string())));
83+
ret.extend(quote!(.add_arg(#opt)));
8484
}
8585
ParseArg::ArgVec(opts) => {
86-
ret.extend(quote! (.add_args(#opts.iter().map(|s| ::std::ffi::OsString::from(s)).collect())));
86+
ret.extend(quote! (.add_args(#opts)));
8787
}
8888
ParseArg::Pipe | ParseArg::Semicolon => break,
8989
}

src/process.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -291,21 +291,28 @@ impl Default for Cmd {
291291
}
292292

293293
impl Cmd {
294-
pub fn add_arg(mut self, arg: OsString) -> Self {
295-
let arg_str = arg.to_string_lossy().to_string();
294+
pub fn add_arg<O>(mut self, arg: O) -> Self
295+
where
296+
O: AsRef<OsStr>,
297+
{
298+
let arg_str = arg.as_ref().to_string_lossy().to_string();
296299
if arg_str != IGNORE_CMD && !self.args.iter().any(|cmd| *cmd != IGNORE_CMD) {
297300
let v: Vec<&str> = arg_str.split('=').collect();
298301
if v.len() == 2 && v[0].chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
299302
self.vars.insert(v[0].into(), v[1].into());
300303
return self;
301304
}
302-
self.in_cmd_map = CMD_MAP.lock().unwrap().contains_key(&arg);
305+
self.in_cmd_map = CMD_MAP.lock().unwrap().contains_key(arg.as_ref());
303306
}
304-
self.args.push(arg);
307+
self.args.push(arg.as_ref().to_os_string());
305308
self
306309
}
307310

308-
pub fn add_args(mut self, args: Vec<OsString>) -> Self {
311+
pub fn add_args<I, O>(mut self, args: I) -> Self
312+
where
313+
I: IntoIterator<Item = O>,
314+
O: AsRef<OsStr>,
315+
{
309316
for arg in args {
310317
self = self.add_arg(arg);
311318
}
@@ -583,6 +590,12 @@ impl CmdString {
583590
}
584591
}
585592

593+
impl AsRef<OsStr> for CmdString {
594+
fn as_ref(&self) -> &OsStr {
595+
self.0.as_ref()
596+
}
597+
}
598+
586599
impl<T: ?Sized + AsRef<OsStr>> From<&T> for CmdString {
587600
fn from(s: &T) -> Self {
588601
Self(s.as_ref().into())
@@ -603,8 +616,8 @@ mod tests {
603616
fn test_run_piped_cmds() {
604617
let mut current_dir = PathBuf::new();
605618
assert!(Cmds::default()
606-
.pipe(Cmd::default().add_args(vec!["echo".into(), "rust".into()]))
607-
.pipe(Cmd::default().add_args(vec!["wc".into()]))
619+
.pipe(Cmd::default().add_args(vec!["echo", "rust"]))
620+
.pipe(Cmd::default().add_args(vec!["wc"]))
608621
.run_cmd(&mut current_dir)
609622
.is_ok());
610623
}
@@ -614,16 +627,16 @@ mod tests {
614627
let mut current_dir = PathBuf::new();
615628
assert_eq!(
616629
Cmds::default()
617-
.pipe(Cmd::default().add_args(vec!["echo".into(), "rust".into()]))
630+
.pipe(Cmd::default().add_args(vec!["echo", "rust"]))
618631
.run_fun(&mut current_dir)
619632
.unwrap(),
620633
"rust"
621634
);
622635

623636
assert_eq!(
624637
Cmds::default()
625-
.pipe(Cmd::default().add_args(vec!["echo".into(), "rust".into()]))
626-
.pipe(Cmd::default().add_args(vec!["wc".into(), "-c".into()]))
638+
.pipe(Cmd::default().add_args(vec!["echo", "rust"]))
639+
.pipe(Cmd::default().add_args(vec!["wc", "-c"]))
627640
.run_fun(&mut current_dir)
628641
.unwrap()
629642
.trim(),
@@ -635,14 +648,14 @@ mod tests {
635648
fn test_stdout_redirect() {
636649
let mut current_dir = PathBuf::new();
637650
let tmp_file = "/tmp/file_echo_rust";
638-
let mut write_cmd = Cmd::default().add_args(vec!["echo".into(), "rust".into()]);
651+
let mut write_cmd = Cmd::default().add_args(vec!["echo", "rust"]);
639652
write_cmd = write_cmd.add_redirect(Redirect::StdoutToFile(PathBuf::from(tmp_file), false));
640653
assert!(Cmds::default()
641654
.pipe(write_cmd)
642655
.run_cmd(&mut current_dir)
643656
.is_ok());
644657

645-
let read_cmd = Cmd::default().add_args(vec!["cat".into(), tmp_file.into()]);
658+
let read_cmd = Cmd::default().add_args(vec!["cat", tmp_file]);
646659
assert_eq!(
647660
Cmds::default()
648661
.pipe(read_cmd)
@@ -651,7 +664,7 @@ mod tests {
651664
"rust"
652665
);
653666

654-
let cleanup_cmd = Cmd::default().add_args(vec!["rm".into(), tmp_file.into()]);
667+
let cleanup_cmd = Cmd::default().add_args(vec!["rm", tmp_file]);
655668
assert!(Cmds::default()
656669
.pipe(cleanup_cmd)
657670
.run_cmd(&mut current_dir)

0 commit comments

Comments
 (0)