use std::marker::PhantomData;
use objc2_app_kit::NSWorkspace;
use objc2_foundation::{NSString, NSURL};
use crate::{Error, Result};
pub(crate) struct Uri<'a, 'b> {
inner: &'a str,
phantom: PhantomData<&'b ()>,
}
impl<'a, 'b> Uri<'a, 'b> {
pub(crate) fn new(inner: &'a str) -> Self {
Self {
inner,
phantom: PhantomData,
}
}
pub(crate) fn is_empty(&self) -> bool {
self.inner.is_empty()
}
pub fn action(self, _: &'b str) -> Self {
self
}
pub fn open<F>(self, on_completion: F) -> Result<()>
where
F: Fn(bool) + 'static,
{
let string = NSString::from_str(self.inner);
let url = NSURL::URLWithString(&string).ok_or(Error::MalformedUri)?;
let workspace = NSWorkspace::sharedWorkspace();
if workspace.openURL(&url) {
on_completion(true);
Ok(())
} else {
Err(Error::Unknown)
}
}
}