use crate::method::Method;
use crate::{Error, Result};
use std::path::{Path, PathBuf};
pub async fn write_async(path: impl AsRef<Path>, data: Vec<u8>) -> Result<()> {
super::require_runtime()?;
let path: PathBuf = path.as_ref().to_path_buf();
tokio::task::spawn_blocking(move || crate::quick::write(&path, &data))
.await
.map_err(join_error_to_io)?
}
pub async fn write_with_async(path: impl AsRef<Path>, data: Vec<u8>, method: Method) -> Result<()> {
super::require_runtime()?;
let path: PathBuf = path.as_ref().to_path_buf();
tokio::task::spawn_blocking(move || crate::quick::write_with(&path, &data, method))
.await
.map_err(join_error_to_io)?
}
pub async fn read_async(path: impl AsRef<Path>) -> Result<Vec<u8>> {
super::require_runtime()?;
let path: PathBuf = path.as_ref().to_path_buf();
tokio::task::spawn_blocking(move || crate::quick::read(&path))
.await
.map_err(join_error_to_io)?
}
pub async fn delete_async(path: impl AsRef<Path>) -> Result<()> {
super::require_runtime()?;
let path: PathBuf = path.as_ref().to_path_buf();
tokio::task::spawn_blocking(move || crate::quick::delete(&path))
.await
.map_err(join_error_to_io)?
}
fn join_error_to_io(e: tokio::task::JoinError) -> Error {
Error::Io(std::io::Error::other(format!(
"spawn_blocking task failed: {e}"
)))
}