1use std::fs;
4use std::io;
5use std::marker::Unpin;
6use std::path::{Path, PathBuf};
7use std::str::FromStr;
8
9#[cfg(target_os = "windows")]
10use std::os::windows::io::RawHandle;
11
12use heim_common::prelude::{Future, Stream, TryFutureExt};
13
14use crate::shims;
15
16#[derive(Debug)]
18pub struct File(shims::fs::File);
19
20impl File {
21 pub fn open<T>(path: T) -> impl Future<Output = io::Result<File>>
23 where
24 T: AsRef<Path> + Send + Unpin + 'static,
25 {
26 shims::fs::File::open(path).map_ok(File)
27 }
28
29 #[cfg(target_os = "windows")]
31 pub fn as_raw_handle(&self) -> RawHandle {
32 self.0.as_raw_handle()
33 }
34}
35
36pub fn path_exists<T>(path: T) -> impl Future<Output = bool>
38where
39 T: AsRef<Path> + Send + Unpin + 'static,
40{
41 shims::fs::path_exists(path)
42}
43
44pub fn read_to_string<T>(path: T) -> impl Future<Output = io::Result<String>>
46where
47 T: AsRef<Path> + Send + Unpin + 'static,
48{
49 shims::fs::read_to_string(path)
50}
51
52pub fn read_lines<T>(path: T) -> impl Stream<Item = io::Result<String>>
54where
55 T: AsRef<Path> + Send + Unpin + 'static,
56{
57 shims::fs::read_lines(path)
58}
59
60pub fn read_first_line<T>(path: T) -> impl Future<Output = io::Result<String>>
62where
63 T: AsRef<Path> + Send + Unpin + 'static,
64{
65 shims::fs::read_first_line(path)
66}
67
68pub fn read_link<T>(path: T) -> impl Future<Output = io::Result<PathBuf>>
70where
71 T: AsRef<Path> + Send + Unpin + 'static,
72{
73 shims::fs::read_link(path)
74}
75
76pub fn read_dir<T>(path: T) -> impl Stream<Item = io::Result<fs::DirEntry>>
78where
79 T: AsRef<Path> + Send + Unpin + 'static,
80{
81 shims::fs::read_dir(path)
82}
83
84pub fn read_into<T, R, E>(path: T) -> impl Future<Output = Result<R, E>>
86where
87 T: AsRef<Path> + Send + Unpin + 'static,
88 R: FromStr<Err = E>,
89 E: From<io::Error>,
90{
91 shims::fs::read_into(path)
92}
93
94pub fn read_lines_into<T, R, E>(path: T) -> impl Stream<Item = Result<R, E>>
96where
97 T: AsRef<Path> + Send + Unpin + 'static,
98 R: FromStr<Err = E>,
99 E: From<io::Error>,
100{
101 shims::fs::read_lines_into(path)
102}