use std::fs;
use std::io;
use std::marker::Unpin;
use std::path::{Path, PathBuf};
use std::str::FromStr;
#[cfg(target_os = "windows")]
use std::os::windows::io::RawHandle;
use heim_common::prelude::{Future, Stream, TryFutureExt};
use crate::shims;
#[derive(Debug)]
pub struct File(shims::fs::File);
impl File {
pub fn open<T>(path: T) -> impl Future<Output = io::Result<File>>
where
T: AsRef<Path> + Send + Unpin + 'static,
{
shims::fs::File::open(path).map_ok(File)
}
#[cfg(target_os = "windows")]
pub fn as_raw_handle(&self) -> RawHandle {
self.0.as_raw_handle()
}
}
pub fn path_exists<T>(path: T) -> impl Future<Output = bool>
where
T: AsRef<Path> + Send + Unpin + 'static,
{
shims::fs::path_exists(path)
}
pub fn read_to_string<T>(path: T) -> impl Future<Output = io::Result<String>>
where
T: AsRef<Path> + Send + Unpin + 'static,
{
shims::fs::read_to_string(path)
}
pub fn read_lines<T>(path: T) -> impl Stream<Item = io::Result<String>>
where
T: AsRef<Path> + Send + Unpin + 'static,
{
shims::fs::read_lines(path)
}
pub fn read_first_line<T>(path: T) -> impl Future<Output = io::Result<String>>
where
T: AsRef<Path> + Send + Unpin + 'static,
{
shims::fs::read_first_line(path)
}
pub fn read_link<T>(path: T) -> impl Future<Output = io::Result<PathBuf>>
where
T: AsRef<Path> + Send + Unpin + 'static,
{
shims::fs::read_link(path)
}
pub fn read_dir<T>(path: T) -> impl Stream<Item = io::Result<fs::DirEntry>>
where
T: AsRef<Path> + Send + Unpin + 'static,
{
shims::fs::read_dir(path)
}
pub fn read_into<T, R, E>(path: T) -> impl Future<Output = Result<R, E>>
where
T: AsRef<Path> + Send + Unpin + 'static,
R: FromStr<Err = E>,
E: From<io::Error>,
{
shims::fs::read_into(path)
}
pub fn read_lines_into<T, R, E>(path: T) -> impl Stream<Item = Result<R, E>>
where
T: AsRef<Path> + Send + Unpin + 'static,
R: FromStr<Err = E>,
E: From<io::Error>,
{
shims::fs::read_lines_into(path)
}