use std::{
ffi::OsString,
path::{Path, PathBuf},
};
use compio::{
BufResult,
buf::{IoBuf, IoBufMut},
io::{AsyncReadAt, AsyncWriteAt},
};
use crate::Result;
cfg_if::cfg_if! {
if #[cfg(any(target_os = "android", target_os = "ios"))] {
use crate::sys as internal;
} else {
#[path = "desktop.rs"]
mod internal;
}
}
#[derive(Debug)]
pub struct UriFile {
inner: internal::UriFile,
}
impl UriFile {
pub async fn open(uri: impl AsRef<Path>) -> Result<Self> {
Ok(Self {
inner: internal::open_uri(uri.as_ref()).await?,
})
}
pub async fn create(uri: impl AsRef<Path>) -> Result<Self> {
Ok(Self {
inner: internal::create_uri(uri.as_ref()).await?,
})
}
pub async fn update(uri: impl AsRef<Path>) -> Result<Self> {
Ok(Self {
inner: internal::update_uri(uri.as_ref()).await?,
})
}
}
impl AsyncReadAt for UriFile {
async fn read_at<T: IoBufMut>(&self, buf: T, pos: u64) -> BufResult<usize, T> {
self.inner.read_at(buf, pos).await
}
}
impl AsyncWriteAt for UriFile {
async fn write_at<T: IoBuf>(&mut self, buf: T, pos: u64) -> BufResult<usize, T> {
self.inner.write_at(buf, pos).await
}
}
impl AsyncWriteAt for &UriFile {
async fn write_at<T: IoBuf>(&mut self, buf: T, pos: u64) -> BufResult<usize, T> {
(&self.inner).write_at(buf, pos).await
}
}
#[derive(Debug)]
pub struct UriReadDir(internal::UriReadDir);
impl Iterator for UriReadDir {
type Item = Result<UriDirEntry>;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|res| Ok(UriDirEntry(res?)))
}
}
#[derive(Debug)]
pub struct UriDirEntry(internal::UriDirEntry);
impl UriDirEntry {
pub fn path(&self) -> PathBuf {
self.0.path()
}
pub fn file_name(&self) -> OsString {
self.0.file_name()
}
pub fn file_type(&self) -> Result<UriFileType> {
Ok(UriFileType(self.0.file_type()?))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct UriFileType(internal::UriFileType);
impl UriFileType {
pub fn is_dir(&self) -> bool {
self.0.is_dir()
}
pub fn is_file(&self) -> bool {
self.0.is_file()
}
pub fn is_symlink(&self) -> bool {
self.0.is_symlink()
}
}
pub fn read_uri_dir(uri: impl AsRef<Path>) -> Result<UriReadDir> {
Ok(UriReadDir(internal::read_dir(uri.as_ref())?))
}