use async_trait::async_trait;
use origin_domain::{AppError, Result};
use origin_platform::Opener;
use tauri::{AppHandle, Runtime};
use tauri_plugin_opener::OpenerExt;
#[derive(Debug, Clone)]
pub struct TauriOpener<R: Runtime> {
app: AppHandle<R>,
}
impl<R: Runtime> TauriOpener<R> {
pub fn new(app: AppHandle<R>) -> Self {
Self { app }
}
}
#[async_trait]
impl<R: Runtime> Opener for TauriOpener<R> {
async fn open_url(&self, url: &str) -> Result<()> {
if !(url.starts_with("https://") || url.starts_with("http://")) {
return Err(AppError::validation(format!(
"refusing to open {url:?}: only http and https URLs are allowed"
)));
}
self.app
.opener()
.open_url(url, None::<&str>)
.map_err(|error| AppError::internal(format!("cannot open url: {error}")))
}
}