use std::process::Command;
pub fn open_browser(url: &str) -> Result<(), String> {
let (cmd, args): (&str, Vec<&str>) = if cfg!(target_os = "macos") {
("open", vec![url])
} else if cfg!(target_os = "windows") {
("rundll32", vec!["url.dll,FileProtocolHandler", url])
} else if cfg!(target_os = "linux") || cfg!(target_os = "freebsd") || cfg!(target_os = "openbsd") || cfg!(target_os = "netbsd") {
("xdg-open", vec![url])
} else {
return Err(format!("Unsupported platform for opening browser"));
};
match Command::new(cmd).args(&args).spawn() {
Ok(_child) => {
Ok(())
}
Err(e) => {
Err(format!("Failed to open browser: {}", e))
}
}
}
pub fn open_file(path: &str) -> Result<(), String> {
let (cmd, args): (&str, Vec<&str>) = if cfg!(target_os = "macos") {
("open", vec![path])
} else if cfg!(target_os = "windows") {
("rundll32", vec!["url.dll,FileProtocolHandler", path])
} else if cfg!(target_os = "linux") || cfg!(target_os = "freebsd") || cfg!(target_os = "openbsd") || cfg!(target_os = "netbsd") {
("xdg-open", vec![path])
} else {
return Err(format!("Unsupported platform for opening file"));
};
match Command::new(cmd).args(&args).spawn() {
Ok(_) => Ok(()),
Err(e) => Err(format!("Failed to open file: {}", e)),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore] fn test_open_browser() {
let result = open_browser("https://example.com");
assert!(result.is_ok());
}
#[test]
fn test_platform_detection() {
let _ = open_browser("https://example.com");
}
}