diff --git a/src/lib.rs b/src/lib.rs index 2e95e4f..79c22c2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,35 +37,41 @@ //! } //! # } //! ``` +use std::ffi::OsStr; use std::io; use std::process::{Command, ExitStatus}; -use std::ffi::OsStr; #[cfg(not(any(target_os = "windows", target_os = "macos")))] pub fn that + Sized>(path: T) -> io::Result { - let mut last_err: io::Result = Err(io::Error::from_raw_os_error(0)); + let path_ref = path.as_ref(); + let mut last_err: io::Error = io::Error::from_raw_os_error(0); for program in &["xdg-open", "gnome-open", "kde-open"] { - match Command::new(program).arg(path.as_ref()).spawn() { + match Command::new(program).arg(path_ref).spawn() { Ok(mut child) => return child.wait(), Err(err) => { - last_err = Err(err); + last_err = err; continue; } } } - last_err + Err(last_err) } #[cfg(target_os = "windows")] pub fn that + Sized>(path: T) -> io::Result { - let mut cmd = Command::new("cmd"); - cmd.arg("/C").arg("start").arg(""); - if let Some(s) = path.as_ref().to_str() { - cmd.arg(s.replace("&", "^&")); - } else { - cmd.arg(path.as_ref()); - } - cmd.spawn()?.wait() + Command::new("cmd") + .arg("/C") + .arg("start") + .arg("") + .arg({ + let path_ref = path.as_ref(); + match path_ref.to_str() { + Some(s) => s.replace("&", "^&"), + None => path_ref, + } + }) + .spawn()? + .wait() } #[cfg(target_os = "macos")] diff --git a/src/main.rs b/src/main.rs index 391b776..d1eae9d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ extern crate open; -use std::io::{stderr, Write}; use std::env; +use std::io::{stderr, Write}; use std::process; fn main() {