use super::Error;
use crate::ThreadBound;
use objc2::rc::Retained;
use objc2_foundation::{NSString, NSURL};
use std::ffi::CStr;
use std::path::{Path, PathBuf};
#[allow(clippy::upper_case_acronyms)]
pub struct URL {
inner: Retained<NSURL>,
_thread_bound: ThreadBound,
}
impl URL {
pub fn from_file_path(path: &Path) -> Result<Self, Error> {
let path = path
.to_str()
.ok_or_else(|| Error::invalid_argument("file URL path is not valid UTF-8"))?;
Ok(Self {
inner: NSURL::fileURLWithPath(&NSString::from_str(path)),
_thread_bound: ThreadBound::new(),
})
}
#[must_use]
pub fn file_path(&self) -> PathBuf {
let pointer = self.inner.fileSystemRepresentation();
let bytes = unsafe { CStr::from_ptr(pointer.as_ptr()) }.to_bytes();
PathBuf::from(String::from_utf8_lossy(bytes).into_owned())
}
}