use std::{cell::RefCell, fmt, sync::Arc};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum OpenUrlError {
NotAnAddress,
Unsupported,
Failed(String),
}
impl fmt::Display for OpenUrlError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotAnAddress => f.write_str("it is not an address the system opens"),
Self::Unsupported => f.write_str("this platform opens no address"),
Self::Failed(reason) => f.write_str(reason),
}
}
}
impl std::error::Error for OpenUrlError {}
type Opener = Arc<dyn Fn(&str) + Send + Sync>;
thread_local! {
static STAND_IN: RefCell<Option<Opener>> = const { RefCell::new(None) };
}
pub fn stand_in(opener: impl Fn(&str) + Send + Sync + 'static) -> StandIn {
let before = STAND_IN.with(|held| held.borrow_mut().replace(Arc::new(opener)));
StandIn { before }
}
#[must_use = "the stand-in is taken away when this is dropped"]
pub struct StandIn {
before: Option<Opener>,
}
impl Drop for StandIn {
fn drop(&mut self) {
let before = self.before.take();
STAND_IN.with(|held| *held.borrow_mut() = before);
}
}
pub fn open_url(url: &str) -> Result<(), OpenUrlError> {
let url = url.trim();
if !has_scheme(url) {
return Err(OpenUrlError::NotAnAddress);
}
if let Some(opener) = STAND_IN.with(|held| held.borrow().clone()) {
opener(url);
return Ok(());
}
system::open(url)
}
pub(super) fn has_scheme(url: &str) -> bool {
let Some((scheme, rest)) = url.split_once(':') else {
return false;
};
let mut chars = scheme.chars();
let well_formed = chars.next().is_some_and(|first| first.is_ascii_alphabetic())
&& chars.all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.'));
well_formed && scheme.len() > 1 && !scheme.eq_ignore_ascii_case("file") && !rest.is_empty()
}
#[cfg(windows)]
mod system {
use windows::{
Win32::UI::{Shell::ShellExecuteW, WindowsAndMessaging::SW_SHOWNORMAL},
core::{PCWSTR, w},
};
use super::OpenUrlError;
pub(super) fn open(url: &str) -> Result<(), OpenUrlError> {
let wide: Vec<u16> = url.encode_utf16().chain(std::iter::once(0)).collect();
let instance = unsafe {
ShellExecuteW(
None,
w!("open"),
PCWSTR(wide.as_ptr()),
PCWSTR::null(),
PCWSTR::null(),
SW_SHOWNORMAL,
)
};
let code = instance.0 as isize;
if code > 32 {
Ok(())
} else {
Err(OpenUrlError::Failed(format!("ShellExecuteW answered {code}")))
}
}
}
#[cfg(target_os = "macos")]
mod system {
use objc2::{class, msg_send, rc::Retained, runtime::AnyObject};
use objc2_foundation::{NSString, NSURL};
use super::OpenUrlError;
pub(super) fn open(url: &str) -> Result<(), OpenUrlError> {
let Some(address) = NSURL::URLWithString(&NSString::from_str(url)) else {
return Err(OpenUrlError::NotAnAddress);
};
let opened: bool = unsafe {
let workspace: Retained<AnyObject> = msg_send![class!(NSWorkspace), sharedWorkspace];
msg_send![&*workspace, openURL: &*address]
};
if opened {
Ok(())
} else {
Err(OpenUrlError::Failed("NSWorkspace did not open it".to_owned()))
}
}
}
#[cfg(target_os = "ios")]
mod system {
use objc2::{
class, msg_send,
rc::Retained,
runtime::{AnyObject, Bool},
};
use objc2_foundation::{NSString, NSURL};
use super::OpenUrlError;
pub(super) fn open(url: &str) -> Result<(), OpenUrlError> {
let Some(address) = NSURL::URLWithString(&NSString::from_str(url)) else {
return Err(OpenUrlError::NotAnAddress);
};
unsafe {
let application: Retained<AnyObject> = msg_send![class!(UIApplication), sharedApplication];
let options: Retained<AnyObject> = msg_send![class!(NSDictionary), dictionary];
let _: () = msg_send![
&*application,
openURL: &*address,
options: &*options,
completionHandler: None::<&block2::Block<dyn Fn(Bool)>>
];
}
Ok(())
}
}
#[cfg(all(unix, not(any(target_os = "macos", target_os = "ios", target_os = "android"))))]
mod system {
use super::OpenUrlError;
pub(super) fn open(url: &str) -> Result<(), OpenUrlError> {
let mut child = std::process::Command::new("xdg-open")
.arg(url)
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.spawn()
.map_err(|error| OpenUrlError::Failed(error.to_string()))?;
std::thread::spawn(move || {
let _ = child.wait();
});
Ok(())
}
}
#[cfg(target_arch = "wasm32")]
mod system {
use super::OpenUrlError;
pub(super) fn open(url: &str) -> Result<(), OpenUrlError> {
let window = web_sys::window().ok_or(OpenUrlError::Unsupported)?;
match window.open_with_url_and_target(url, "_blank") {
Ok(_) => Ok(()),
Err(error) => Err(OpenUrlError::Failed(format!("{error:?}"))),
}
}
}
#[cfg(not(any(
windows,
target_os = "macos",
target_os = "ios",
all(unix, not(target_os = "android")),
target_arch = "wasm32"
)))]
mod system {
use super::OpenUrlError;
pub(super) fn open(_url: &str) -> Result<(), OpenUrlError> {
Err(OpenUrlError::Unsupported)
}
}
#[cfg(test)]
mod tests {
use std::sync::Mutex;
use super::*;
#[test]
fn only_an_address_with_a_scheme_is_opened() {
for url in [
"https://lurq.dev",
"http://a.b/c?d",
"mailto:ivan@lurq.dev",
"tel:+380",
"x-app:open",
] {
assert!(has_scheme(url), "{url}");
}
for url in [
"lurq.dev",
"C:\\Windows\\notepad.exe",
"file:///etc/passwd",
"FILE:x",
"/usr/bin/x",
":x",
"https:",
"1a:b",
] {
assert!(!has_scheme(url), "{url}");
}
}
#[test]
fn a_stand_in_is_given_the_address_and_the_one_before_it_is_back_once_it_is_dropped() {
let opened = Arc::new(Mutex::new(Vec::<String>::new()));
let (outer_log, inner_log) = (opened.clone(), opened.clone());
let outer = stand_in(move |url| outer_log.lock().unwrap().push(format!("outer {url}")));
{
let _inner = stand_in(move |url| inner_log.lock().unwrap().push(format!("inner {url}")));
open_url(" https://lurq.dev/a ").unwrap();
}
open_url("https://lurq.dev/b").unwrap();
assert_eq!(open_url("notes.txt"), Err(OpenUrlError::NotAnAddress));
drop(outer);
assert_eq!(
*opened.lock().unwrap(),
["inner https://lurq.dev/a", "outer https://lurq.dev/b"]
);
}
}