heim_runtime/
fs.rs

1//! Async FS operations.
2
3use 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/// A reference to an open file in filesystem.
17#[derive(Debug)]
18pub struct File(shims::fs::File);
19
20impl File {
21    /// Attempt to open file in read-only mode.
22    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    /// Returns the raw Windows handle from file.
30    #[cfg(target_os = "windows")]
31    pub fn as_raw_handle(&self) -> RawHandle {
32        self.0.as_raw_handle()
33    }
34}
35
36/// Returns future which checks if path `path` points to some file.
37pub 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
44/// Read `path` file asynchronously and convert it contents into a string.
45pub 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
52/// Returns stream of lines yielded from file with `path` path.
53pub 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
60/// Returns future which tries to read the first line from file.
61pub 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
68/// Returns future which tries read the symlink.
69pub 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
76/// Returns stream of files and directories contained in the `path` directory.
77pub 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
84/// Read `path` file and try to parse it into a `R` type via `std::str::FromStr`.
85pub 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
94/// Returns stream which reads lines from file and tries to parse them with help of `FromStr` trait.
95pub 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}