use std::fs::Metadata;
use crate::{
io::{self, asyncify},
prelude::*,
};
pub async fn create_dir_all(path: impl AsRef<Utf8Path>) -> io::Result<()> {
let path = path.as_ref().to_owned();
asyncify(move || {
std::fs::create_dir_all(&path).map_err_std_io(|io| {
Report::new(io.clone()).wrap_err(format!("ystd::fs::create_dir_all({})", path))
})
})
.await
}
pub async fn read(path: impl AsRef<Utf8Path>) -> io::Result<Vec<u8>> {
let path = path.as_ref().to_owned();
asyncify(move || {
std::fs::read(&path)
.map_err_std_io(|io| Report::new(io).wrap_err(format!("ystd::fs::read({})", path)))
})
.await
}
pub async fn read_to_string(path: impl AsRef<Utf8Path>) -> io::Result<String> {
let path = path.as_ref().to_owned();
asyncify(move || {
std::fs::read_to_string(&path).map_err_std_io(|io| {
Report::new(io).wrap_err(format!("ystd::fs::read_to_string({})", path))
})
})
.await
}
pub async fn canonicalize_utf8(path: impl AsRef<Utf8Path>) -> io::Result<Utf8PathBuf> {
let path = path.as_ref().to_owned();
let path = crate::io::asyncify(move || {
path.0.canonicalize_utf8().map(Utf8PathBuf).map_err(|io| {
let io = Arc::new(io);
io::Error::empty(
Report::new(io.clone()).wrap_err(format!("ystd::fs::canonicalize({})", path)),
)
.with_io(io)
})
})
.await?;
Ok(path)
}
pub async fn canonicalize(path: impl AsRef<Utf8Path>) -> io::Result<Utf8PathBuf> {
canonicalize_utf8(path).await
}
pub async fn metadata(path: impl AsRef<Utf8Path>) -> io::Result<Metadata> {
let path = path.as_ref().to_owned();
asyncify(move || {
std::fs::metadata(&path)
.map_err_std_io(|io| Report::new(io).wrap_err(format!("ystd::fs::metadata({})", path)))
})
.await
}
pub async fn write(path: impl AsRef<Utf8Path>, contents: impl AsRef<[u8]>) -> io::Result<()> {
let path = path.as_ref().to_owned();
let contents = contents.as_ref().to_owned();
asyncify(move || {
std::fs::write(&path, contents).map_err_std_io(|io| {
Report::new(io).wrap_err(format!("ystd::fs::write({})", path))
})
})
.await
}