| 1 | use std::path::Path; |
| 2 | use std::process::Command; |
| 3 | |
| 4 | use anyhow::Result; |
| 5 | |
| 6 | /// Open a URL in the system's default browser. |
| 7 | /// |
| 8 | /// Dispatches to the platform-appropriate opener: |
| 9 | /// - macOS: `open` |
| 10 | /// - Linux / BSD: `xdg-open` |
| 11 | /// - Windows: `rundll32 url.dll,FileProtocolHandler` |
| 12 | /// - Other: returns an error. |
| 13 | /// |
| 14 | /// This is the single entry point for URL opening — every call site in |
| 15 | /// the codebase should use this instead of hardcoding `Command::new("open")`, |
| 16 | /// `Command::new("xdg-open")`, or `Command::new("cmd")`. |
| 17 | pub fn open_url(url: &str) -> Result<()> { |
| 18 | let mut command = browser_open_command(url)?; |
| 19 | command |
| 20 | .stdout(std::process::Stdio::null()) |
| 21 | .stderr(std::process::Stdio::null()) |
| 22 | .spawn() |
| 23 | .map(|_| ()) |
| 24 | .map_err(|e| anyhow::anyhow!("failed to launch browser command: {e}")) |
| 25 | } |
| 26 | |
| 27 | pub fn open_path(path: &Path) -> Result<()> { |
| 28 | let mut command = path_open_command(path)?; |
| 29 | command |
| 30 | .stdout(std::process::Stdio::null()) |
| 31 | .stderr(std::process::Stdio::null()) |
| 32 | .spawn() |
| 33 | .map(|_| ()) |
| 34 | .map_err(|e| anyhow::anyhow!("failed to launch path opener command: {e}")) |
| 35 | } |
| 36 | |
| 37 | pub fn reveal_path(path: &Path) -> Result<()> { |
| 38 | let mut command = path_reveal_command(path)?; |
| 39 | command |
| 40 | .stdout(std::process::Stdio::null()) |
| 41 | .stderr(std::process::Stdio::null()) |
| 42 | .spawn() |
| 43 | .map(|_| ()) |
| 44 | .map_err(|e| anyhow::anyhow!("failed to launch path reveal command: {e}")) |
| 45 | } |
| 46 | |
| 47 | fn path_open_command(path: &Path) -> Result<Command> { |
| 48 | if path.as_os_str().is_empty() { |
| 49 | return Err(anyhow::anyhow!("path cannot be empty")); |
| 50 | } |
| 51 | |
| 52 | #[cfg(target_os = "macos")] |
| 53 | { |
| 54 | let mut command = Command::new("open"); |
| 55 | command.arg(path); |
| 56 | Ok(command) |
| 57 | } |
| 58 | |
| 59 | #[cfg(any( |
| 60 | all(target_os = "linux", not(target_env = "ohos")), |
| 61 | target_os = "netbsd", |
| 62 | target_os = "freebsd", |
| 63 | target_os = "openbsd", |
| 64 | target_os = "dragonfly" |
| 65 | ))] |
| 66 | { |
| 67 | let mut command = Command::new("xdg-open"); |
| 68 | command.arg(path); |
| 69 | Ok(command) |
| 70 | } |
| 71 | |
| 72 | #[cfg(target_os = "windows")] |
| 73 | { |
| 74 | let mut command = Command::new("explorer"); |
| 75 | command.arg(path); |
| 76 | Ok(command) |
| 77 | } |
| 78 | |
| 79 | #[cfg(not(any( |
| 80 | target_os = "macos", |
| 81 | all(target_os = "linux", not(target_env = "ohos")), |
| 82 | target_os = "windows", |
| 83 | target_os = "netbsd", |
| 84 | target_os = "freebsd", |
| 85 | target_os = "openbsd", |
| 86 | target_os = "dragonfly" |
| 87 | )))] |
| 88 | Err(anyhow::anyhow!( |
| 89 | "path opening is unsupported on this platform" |
| 90 | )) |
| 91 | } |
| 92 | |
| 93 | fn path_reveal_command(path: &Path) -> Result<Command> { |
| 94 | if path.as_os_str().is_empty() { |
| 95 | return Err(anyhow::anyhow!("path cannot be empty")); |
| 96 | } |
| 97 | |
| 98 | #[cfg(target_os = "macos")] |
| 99 | { |
| 100 | let mut command = Command::new("open"); |
| 101 | command.arg("-R").arg(path); |
| 102 | Ok(command) |
| 103 | } |
| 104 | |
| 105 | #[cfg(any( |
| 106 | all(target_os = "linux", not(target_env = "ohos")), |
| 107 | target_os = "netbsd", |
| 108 | target_os = "freebsd", |
| 109 | target_os = "openbsd", |
| 110 | target_os = "dragonfly" |
| 111 | ))] |
| 112 | { |
| 113 | let target = if path.is_dir() { |
| 114 | path |
| 115 | } else { |
| 116 | path.parent() |
| 117 | .filter(|parent| !parent.as_os_str().is_empty()) |
| 118 | .unwrap_or(path) |
| 119 | }; |
| 120 | let mut command = Command::new("xdg-open"); |
| 121 | command.arg(target); |
| 122 | Ok(command) |
| 123 | } |
| 124 | |
| 125 | #[cfg(target_os = "windows")] |
| 126 | { |
| 127 | let mut arg = std::ffi::OsString::from("/select,"); |
| 128 | arg.push(path.as_os_str()); |
| 129 | let mut command = Command::new("explorer"); |
| 130 | command.arg(arg); |
| 131 | Ok(command) |
| 132 | } |
| 133 | |
| 134 | #[cfg(not(any( |
| 135 | target_os = "macos", |
| 136 | all(target_os = "linux", not(target_env = "ohos")), |
| 137 | target_os = "windows", |
| 138 | target_os = "netbsd", |
| 139 | target_os = "freebsd", |
| 140 | target_os = "openbsd", |
| 141 | target_os = "dragonfly" |
| 142 | )))] |
| 143 | Err(anyhow::anyhow!( |
| 144 | "path reveal is unsupported on this platform" |
| 145 | )) |
| 146 | } |
| 147 | |
| 148 | fn browser_open_command(url: &str) -> Result<Command> { |
| 149 | if url.trim().is_empty() { |
| 150 | return Err(anyhow::anyhow!("browser URL cannot be empty")); |
| 151 | } |
| 152 | |
| 153 | #[cfg(target_os = "macos")] |
| 154 | { |
| 155 | let mut command = Command::new("open"); |
| 156 | command.arg(url); |
| 157 | Ok(command) |
| 158 | } |
| 159 | |
| 160 | #[cfg(any( |
| 161 | all(target_os = "linux", not(target_env = "ohos")), |
| 162 | target_os = "netbsd", |
| 163 | target_os = "freebsd", |
| 164 | target_os = "openbsd", |
| 165 | target_os = "dragonfly" |
| 166 | ))] |
| 167 | { |
| 168 | let mut command = Command::new("xdg-open"); |
| 169 | command.arg(url); |
| 170 | Ok(command) |
| 171 | } |
| 172 | |
| 173 | // Not `cmd /C start`: cmd.exe would parse `&`, `|`, `^` and `%` inside |
| 174 | // the URL. The protocol handler receives it as data. |
| 175 | #[cfg(target_os = "windows")] |
| 176 | { |
| 177 | let mut cmd = Command::new("rundll32"); |
| 178 | cmd.args(["url.dll,FileProtocolHandler", url]); |
| 179 | Ok(cmd) |
| 180 | } |
| 181 | |
| 182 | #[cfg(not(any( |
| 183 | target_os = "macos", |
| 184 | all(target_os = "linux", not(target_env = "ohos")), |
| 185 | target_os = "windows", |
| 186 | target_os = "netbsd", |
| 187 | target_os = "freebsd", |
| 188 | target_os = "openbsd", |
| 189 | target_os = "dragonfly" |
| 190 | )))] |
| 191 | Err(anyhow::anyhow!( |
| 192 | "browser opening is unsupported on this platform" |
| 193 | )) |
| 194 | } |
| 195 | |
| 196 | #[cfg(test)] |
| 197 | mod tests { |
| 198 | // =================================================================== |
| 199 | // open_url tests |
| 200 | // =================================================================== |
| 201 | |
| 202 | #[test] |
| 203 | fn open_url_builds_platform_command_without_spawning() { |
| 204 | let command = super::browser_open_command("https://example.com").expect("command"); |
| 205 | |
| 206 | #[cfg(target_os = "macos")] |
| 207 | { |
| 208 | assert_eq!(command.get_program(), "open"); |
| 209 | assert_eq!( |
| 210 | command |
| 211 | .get_args() |
| 212 | .map(|arg| arg.to_string_lossy().into_owned()) |
| 213 | .collect::<Vec<_>>(), |
| 214 | vec!["https://example.com"] |
| 215 | ); |
| 216 | } |
| 217 | |
| 218 | #[cfg(any( |
| 219 | target_os = "netbsd", |
| 220 | target_os = "freebsd", |
| 221 | target_os = "openbsd", |
| 222 | target_os = "dragonfly" |
| 223 | ))] |
| 224 | { |
| 225 | assert_eq!(command.get_program(), "xdg-open"); |
| 226 | } |
| 227 | |
| 228 | #[cfg(all(target_os = "linux", not(target_env = "ohos")))] |
| 229 | { |
| 230 | assert_eq!(command.get_program(), "xdg-open"); |
| 231 | assert_eq!( |
| 232 | command |
| 233 | .get_args() |
| 234 | .map(|arg| arg.to_string_lossy().into_owned()) |
| 235 | .collect::<Vec<_>>(), |
| 236 | vec!["https://example.com"] |
| 237 | ); |
| 238 | } |
| 239 | |
| 240 | #[cfg(target_os = "windows")] |
| 241 | { |
| 242 | assert_eq!(command.get_program(), "rundll32"); |
| 243 | assert_eq!( |
| 244 | command |
| 245 | .get_args() |
| 246 | .map(|arg| arg.to_string_lossy().into_owned()) |
| 247 | .collect::<Vec<_>>(), |
| 248 | vec!["url.dll,FileProtocolHandler", "https://example.com"] |
| 249 | ); |
| 250 | // Shell metacharacters stay inside the single URL argument. |
| 251 | let url = "https://example.com/?a=1&b=2|x^y%PATH%"; |
| 252 | let command = super::browser_open_command(url).expect("command"); |
| 253 | assert_eq!(command.get_program(), "rundll32"); |
| 254 | assert_eq!( |
| 255 | command |
| 256 | .get_args() |
| 257 | .last() |
| 258 | .map(|arg| arg.to_string_lossy().into_owned()), |
| 259 | Some(url.to_string()) |
| 260 | ); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | #[test] |
| 265 | fn open_url_rejects_empty_url_gracefully() { |
| 266 | // An empty URL should fail with a clear error, not panic. |
| 267 | let result = super::browser_open_command(""); |
| 268 | match result { |
| 269 | Ok(_) => panic!("empty URL should not build an opener command"), |
| 270 | Err(e) => { |
| 271 | let msg = e.to_string(); |
| 272 | assert!(!msg.is_empty(), "error message must not be empty"); |
| 273 | assert!(msg.contains("empty"), "unexpected error message: {msg}"); |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | #[test] |
| 279 | fn open_path_builds_platform_command_without_spawning() { |
| 280 | let path = std::path::Path::new("/tmp/workspace/src"); |
| 281 | let command = super::path_open_command(path).expect("command"); |
| 282 | |
| 283 | #[cfg(target_os = "macos")] |
| 284 | { |
| 285 | assert_eq!(command.get_program(), "open"); |
| 286 | assert_eq!( |
| 287 | command |
| 288 | .get_args() |
| 289 | .map(|arg| arg.to_string_lossy().into_owned()) |
| 290 | .collect::<Vec<_>>(), |
| 291 | vec!["/tmp/workspace/src"] |
| 292 | ); |
| 293 | } |
| 294 | |
| 295 | #[cfg(any( |
| 296 | all(target_os = "linux", not(target_env = "ohos")), |
| 297 | target_os = "netbsd", |
| 298 | target_os = "freebsd", |
| 299 | target_os = "openbsd", |
| 300 | target_os = "dragonfly" |
| 301 | ))] |
| 302 | { |
| 303 | assert_eq!(command.get_program(), "xdg-open"); |
| 304 | assert_eq!( |
| 305 | command |
| 306 | .get_args() |
| 307 | .map(|arg| arg.to_string_lossy().into_owned()) |
| 308 | .collect::<Vec<_>>(), |
| 309 | vec!["/tmp/workspace/src"] |
| 310 | ); |
| 311 | } |
| 312 | |
| 313 | #[cfg(target_os = "windows")] |
| 314 | { |
| 315 | assert_eq!(command.get_program(), "explorer"); |
| 316 | assert_eq!( |
| 317 | command |
| 318 | .get_args() |
| 319 | .map(|arg| arg.to_string_lossy().into_owned()) |
| 320 | .collect::<Vec<_>>(), |
| 321 | vec!["/tmp/workspace/src"] |
| 322 | ); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | #[test] |
| 327 | fn reveal_path_builds_platform_command_without_spawning() { |
| 328 | let path = std::path::Path::new("/tmp/workspace/src/main.rs"); |
| 329 | let command = super::path_reveal_command(path).expect("command"); |
| 330 | |
| 331 | #[cfg(target_os = "macos")] |
| 332 | { |
| 333 | assert_eq!(command.get_program(), "open"); |
| 334 | assert_eq!( |
| 335 | command |
| 336 | .get_args() |
| 337 | .map(|arg| arg.to_string_lossy().into_owned()) |
| 338 | .collect::<Vec<_>>(), |
| 339 | vec!["-R", "/tmp/workspace/src/main.rs"] |
| 340 | ); |
| 341 | } |
| 342 | |
| 343 | #[cfg(any( |
| 344 | all(target_os = "linux", not(target_env = "ohos")), |
| 345 | target_os = "netbsd", |
| 346 | target_os = "freebsd", |
| 347 | target_os = "openbsd", |
| 348 | target_os = "dragonfly" |
| 349 | ))] |
| 350 | { |
| 351 | assert_eq!(command.get_program(), "xdg-open"); |
| 352 | assert_eq!( |
| 353 | command |
| 354 | .get_args() |
| 355 | .map(|arg| arg.to_string_lossy().into_owned()) |
| 356 | .collect::<Vec<_>>(), |
| 357 | vec!["/tmp/workspace/src"] |
| 358 | ); |
| 359 | } |
| 360 | |
| 361 | #[cfg(target_os = "windows")] |
| 362 | { |
| 363 | assert_eq!(command.get_program(), "explorer"); |
| 364 | assert_eq!( |
| 365 | command |
| 366 | .get_args() |
| 367 | .map(|arg| arg.to_string_lossy().into_owned()) |
| 368 | .collect::<Vec<_>>(), |
| 369 | vec!["/select,/tmp/workspace/src/main.rs"] |
| 370 | ); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | #[test] |
| 375 | fn open_and_reveal_path_reject_empty_path() { |
| 376 | let empty = std::path::Path::new(""); |
| 377 | assert!(super::path_open_command(empty).is_err()); |
| 378 | assert!(super::path_reveal_command(empty).is_err()); |
| 379 | } |
| 380 | } |
| 381 |