A high-performance inference engine for AI models
use std::{
io,
ops::Range,
path::{Path, PathBuf},
};
use crate::time::SystemTime;
pub async fn canonicalize(path: impl AsRef<Path>) -> Result<PathBuf, io::Error> {
#[cfg(target_family = "wasm")]
return crate::fs::asyn_opfs::canonicalize(path.as_ref().to_str().unwrap()).await;
#[cfg(not(target_family = "wasm"))]
tokio::fs::canonicalize(path).await
}
pub async fn is_dir(path: impl AsRef<Path>) -> bool {
#[cfg(target_family = "wasm")]
return crate::fs::asyn_opfs::is_dir(path.as_ref().to_str().unwrap()).await;
#[cfg(not(target_family = "wasm"))]
tokio::fs::metadata(path).await.is_ok_and(|metadata| metadata.is_dir())
}
pub async fn is_dir_no_follow(path: impl AsRef<Path>) -> bool {
#[cfg(target_family = "wasm")]
return is_dir(path).await;
#[cfg(not(target_family = "wasm"))]
tokio::fs::symlink_metadata(path).await.is_ok_and(|metadata| metadata.is_dir())
}
pub async fn create_dir_all(path: impl AsRef<Path>) -> Result<(), io::Error> {
#[cfg(target_family = "wasm")]
return super::asyn_opfs::dir_create_all(path.as_ref().to_str().unwrap()).await;
#[cfg(not(target_family = "wasm"))]
tokio::fs::create_dir_all(path).await
}
pub async fn file_length(path: impl AsRef<Path>) -> Result<u64, io::Error> {
#[cfg(target_family = "wasm")]
return super::asyn_opfs::file_length(path.as_ref().to_str().unwrap()).await;
#[cfg(not(target_family = "wasm"))]
Ok(tokio::fs::metadata(path.as_ref()).await?.len())
}
pub async fn file_modified(path: impl AsRef<Path>) -> Result<SystemTime, io::Error> {
#[cfg(target_family = "wasm")]
return super::asyn_opfs::file_modified(path.as_ref().to_str().unwrap()).await;
#[cfg(not(target_family = "wasm"))]
tokio::fs::metadata(path.as_ref()).await?.modified()
}
pub async fn hard_link(
original: impl AsRef<Path>,
link: impl AsRef<Path>,
) -> Result<(), io::Error> {
// OPFS doesn't support linking, that's why just copy
#[cfg(target_family = "wasm")]
{
let original = original.as_ref().to_str().unwrap();
let link = link.as_ref().to_str().unwrap();
super::asyn_opfs::file_copy_if_absent(original, link).await
}
#[cfg(not(target_family = "wasm"))]
tokio::fs::hard_link(original.as_ref(), link.as_ref()).await
}
pub async fn is_file(path: impl AsRef<Path>) -> bool {
#[cfg(target_family = "wasm")]
return super::asyn_opfs::is_file(path.as_ref().to_str().unwrap()).await;
#[cfg(not(target_family = "wasm"))]
tokio::fs::metadata(path).await.map(|m| m.is_file()).unwrap_or(false)
}
pub async fn read(path: impl AsRef<Path>) -> Result<Vec<u8>, io::Error> {
#[cfg(target_family = "wasm")]
return super::asyn_opfs::file_read(path.as_ref().to_str().unwrap()).await;
#[cfg(not(target_family = "wasm"))]
tokio::fs::read(path.as_ref()).await
}
pub async fn read_range(
path: impl AsRef<Path>,
range: Range<u64>,
) -> Result<Vec<u8>, io::Error> {
#[cfg(target_family = "wasm")]
return super::asyn_opfs::file_read_range(path.as_ref().to_str().unwrap(), range).await;
#[cfg(not(target_family = "wasm"))]
{
use tokio::io::{AsyncReadExt, AsyncSeekExt, SeekFrom};
let mut file = tokio::fs::File::open(path).await?;
file.seek(SeekFrom::Start(range.start)).await?;
let buffer_len = (range.end - range.start) as usize;
let mut buffer = vec![0; buffer_len];
file.read_exact(&mut buffer).await?;
Ok(buffer)
}
}
pub async fn read_dir(path: impl AsRef<Path>) -> Result<Vec<PathBuf>, io::Error> {
#[cfg(target_family = "wasm")]
{
let path = path.as_ref();
let names = super::asyn_opfs::dir_entries(path.to_str().unwrap()).await?;
Ok(names.into_iter().map(|name| path.join(name)).collect())
}
#[cfg(not(target_family = "wasm"))]
{
let mut entries = tokio::fs::read_dir(path.as_ref()).await?;
let mut paths = Vec::new();
while let Some(entry) = entries.next_entry().await? {
paths.push(entry.path());
}
Ok(paths)
}
}
pub async fn read_to_string(path: impl AsRef<Path>) -> Result<String, io::Error> {
#[cfg(target_family = "wasm")]
{
let content = read(path).await?;
let string = String::from_utf8(content).map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?;
Ok(string)
}
#[cfg(not(target_family = "wasm"))]
tokio::fs::read_to_string(path.as_ref()).await
}
pub async fn remove_dir_all(path: impl AsRef<Path>) -> Result<(), io::Error> {
#[cfg(target_family = "wasm")]
return super::asyn_opfs::dir_remove_all(path.as_ref().to_str().unwrap()).await;
#[cfg(not(target_family = "wasm"))]
tokio::fs::remove_dir_all(path.as_ref()).await
}
pub async fn remove_file(path: impl AsRef<Path>) -> Result<(), io::Error> {
#[cfg(target_family = "wasm")]
return super::asyn_opfs::file_remove(path.as_ref().to_str().unwrap()).await;
#[cfg(not(target_family = "wasm"))]
tokio::fs::remove_file(path.as_ref()).await
}
pub async fn rename(
src_file: impl AsRef<Path>,
dst_file: impl AsRef<Path>,
) -> Result<(), io::Error> {
#[cfg(target_family = "wasm")]
{
let src_file_name = src_file.as_ref().to_str().unwrap();
let dst_file_name = dst_file.as_ref().to_str().unwrap();
super::asyn_opfs::file_rename(src_file_name, dst_file_name).await
}
#[cfg(not(target_family = "wasm"))]
tokio::fs::rename(src_file, dst_file.as_ref()).await
}
pub async fn try_exists(path: impl AsRef<Path>) -> Result<bool, io::Error> {
#[cfg(target_family = "wasm")]
return super::asyn_opfs::exists(path.as_ref().to_str().unwrap()).await;
#[cfg(not(target_family = "wasm"))]
tokio::fs::try_exists(path.as_ref()).await
}
pub async fn write(
path: impl AsRef<Path>,
contents: impl AsRef<[u8]>,
) -> Result<(), io::Error> {
#[cfg(target_family = "wasm")]
return super::asyn_opfs::file_write(path.as_ref().to_str().unwrap(), contents.as_ref()).await;
#[cfg(not(target_family = "wasm"))]
tokio::fs::write(path, contents).await
}
pub async fn write_with_sync_all(
path: impl AsRef<Path>,
contents: impl AsRef<[u8]>,
) -> Result<(), io::Error> {
#[cfg(target_family = "wasm")]
{
let path = path.as_ref().to_str().unwrap();
super::asyn_opfs::file_write_if_absent(path, contents.as_ref()).await
}
#[cfg(not(target_family = "wasm"))]
{
use tokio::io::AsyncWriteExt;
let mut file = tokio::fs::OpenOptions::new().write(true).create_new(true).open(&path).await?;
file.write_all(contents.as_ref()).await?;
file.sync_all().await
}
}