Skip to content

Commit

Permalink
taking T:AsRef<OsStr> instead of &str
Browse files Browse the repository at this point in the history
this allows passing Paths and PathBufs and alls other sorts of string
  • Loading branch information
hoodie committed Apr 10, 2016
1 parent 31605e0 commit 2540a0a
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@
//!
//! ```test_harness,no_run
//! extern crate open;
//!
//!
//! # #[test]
//! # fn doit() {
//! open::that("/path/to/file/with.fancy-extension");
//! if open::that("https://google.de").is_ok() {
//! if open::that("https://rust-lang.org").is_ok() {
//! println!("Look at your browser !");
//! }
//! # }
//! ```
//!
//! # Notes
//! As an operating system program is used, chances are that the open operation fails.
//! Therfore, you are advised to at least check the result with `.is_err()` and
//! Therfore, you are advised to at least check the result with `.is_err()` and
//! behave accordingly, e.g. by letting the user know what you tried to open, and failed.
use std::io;
use std::process::{Command, ExitStatus};
use std::ffi::OsStr;

#[cfg(not(any(target_os = "windows", target_os = "macos")))]
pub fn that(path: &str) -> io::Result<ExitStatus> {
pub fn that<T:AsRef<OsStr>+Sized>(path: T) -> io::Result<ExitStatus> {
let mut last_err: io::Result<ExitStatus> = Err(io::Error::from_raw_os_error(0));
for program in &["xdg-open", "gnome-open", "kde-open"] {
match Command::new(program).arg(path).spawn() {
match Command::new(program).arg(path.as_ref()).spawn() {
Ok(mut child) => return child.wait(),
Err(err) => {
last_err = Err(err);
Expand All @@ -37,11 +38,11 @@ pub fn that(path: &str) -> io::Result<ExitStatus> {
}

#[cfg(target_os = "windows")]
pub fn that(path: &str) -> io::Result<ExitStatus> {
try!(Command::new("cmd").arg("/C").arg("start").arg(path).spawn()).wait()
pub fn that<T:AsRef<OsStr>+Sized>(path: T) -> io::Result<ExitStatus> {
try!(Command::new("cmd").arg("/C").arg("start").arg(path.as_ref()).spawn()).wait()
}

#[cfg(target_os = "macos")]
pub fn that(path: &str) -> io::Result<ExitStatus> {
try!(Command::new("open").arg(path).spawn()).wait()
pub fn that<T:AsRef<OsStr>+Sized>(path: T) -> io::Result<ExitStatus> {
try!(Command::new("open").arg(path.as_ref()).spawn()).wait()
}

0 comments on commit 2540a0a

Please sign in to comment.