use std::pin::Pin;
use async_trait::async_trait;
use crate::types::MaybeSync;
use crate::types::Metadata;
use crate::types::PathType;
use crate::types::ReadDir;
use crate::ReadableFileSystem;
pub type Path = relative_path::RelativePath;
pub type PathBuf = relative_path::RelativePathBuf;
mod private {
pub trait Sealed {}
impl Sealed for super::Path {}
}
#[cfg(target_family = "wasm")]
pub type BoxFuture<'a, T> = Pin<Box<dyn std::future::Future<Output = T> + 'a>>;
#[cfg(not(target_family = "wasm"))]
pub type BoxFuture<'a, T> = Pin<Box<dyn std::future::Future<Output = T> + Send + 'a>>;
#[cfg_attr(target_family = "wasm", async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait)]
pub trait LunchboxPathUtils: private::Sealed
where
Self: PathType + MaybeSync,
{
fn metadata<'a, 'b, 'c>(
&'a self,
fs: &'b impl ReadableFileSystem,
) -> BoxFuture<'c, std::io::Result<Metadata>>
where
'a: 'c,
'b: 'c,
{
fs.metadata(self)
}
fn symlink_metadata<'a, 'b, 'c>(
&'a self,
fs: &'b impl ReadableFileSystem,
) -> BoxFuture<'c, std::io::Result<Metadata>>
where
'a: 'c,
'b: 'c,
{
fs.symlink_metadata(self)
}
fn canonicalize<'a, 'b, 'c>(
&'a self,
fs: &'b impl ReadableFileSystem,
) -> BoxFuture<'c, std::io::Result<PathBuf>>
where
'a: 'c,
'b: 'c,
{
fs.canonicalize(self)
}
fn read_link<'a, 'b, 'c>(
&'a self,
fs: &'b impl ReadableFileSystem,
) -> BoxFuture<'c, std::io::Result<PathBuf>>
where
'a: 'c,
'b: 'c,
{
fs.read_link(self)
}
fn read_dir<'a, 'b, 'c, T: ReadableFileSystem>(
&'a self,
fs: &'b T,
) -> BoxFuture<'c, std::io::Result<ReadDir<'b, T::ReadDirPollerType, T>>>
where
'a: 'c,
'b: 'c,
{
fs.read_dir(self)
}
#[must_use]
async fn exists(&self, fs: &(impl ReadableFileSystem + MaybeSync)) -> bool {
fs.metadata(self).await.is_ok()
}
#[must_use]
async fn is_file(&self, fs: &(impl ReadableFileSystem + MaybeSync)) -> bool {
fs.metadata(self)
.await
.map(|m| m.is_file())
.unwrap_or(false)
}
#[must_use]
async fn is_dir(&self, fs: &(impl ReadableFileSystem + MaybeSync)) -> bool {
fs.metadata(self).await.map(|m| m.is_dir()).unwrap_or(false)
}
#[must_use]
async fn is_symlink(&self, fs: &(impl ReadableFileSystem + MaybeSync)) -> bool {
fs.symlink_metadata(self)
.await
.map(|m| m.is_symlink())
.unwrap_or(false)
}
}
impl LunchboxPathUtils for Path {}