io_fs/io.rs
1//! Filesystem I/O requests and responses.
2
3use std::{
4 collections::{HashMap, HashSet},
5 fmt,
6 path::PathBuf,
7};
8
9/// The filesystem I/O request and response enum, emitted by
10/// [coroutines] and processed by [runtimes].
11///
12/// Represents all the possible I/O requests that a filesystem
13/// coroutine can emit. Runtimes should be able to handle all
14/// variants.
15///
16/// [coroutines]: crate::coroutines
17/// [runtimes]: crate::runtimes
18#[derive(Clone)]
19pub enum FsIo {
20 /// I/O request to create a filesystem directory.
21 ///
22 /// Input: directory path
23 ///
24 /// Output: none
25 CreateDir(Result<(), PathBuf>),
26
27 /// I/O request to create multiple filesystem directories.
28 ///
29 /// Input: set of directory paths
30 ///
31 /// Output: none
32 CreateDirs(Result<(), HashSet<PathBuf>>),
33
34 /// I/O request to create a filesystem file.
35 ///
36 /// Input: tuple of file path and raw contents (bytes)
37 ///
38 /// Output: none
39 CreateFile(Result<(), (PathBuf, Vec<u8>)>),
40
41 /// I/O request to create multiple filesystem files.
42 ///
43 /// Input: map of path and raw contents (bytes)
44 ///
45 /// Output: none
46 CreateFiles(Result<(), HashMap<PathBuf, Vec<u8>>>),
47
48 /// I/O request to read entries from a filesystem directory.
49 ///
50 /// Input: directory path
51 ///
52 /// Output: set of entry paths
53 ReadDir(Result<HashSet<PathBuf>, PathBuf>),
54
55 /// I/O request to read a filesystem file.
56 ///
57 /// Input: file path
58 ///
59 /// Output: raw contents (bytes)
60 ReadFile(Result<Vec<u8>, PathBuf>),
61
62 /// I/O request to read multiple filesystem files.
63 ///
64 /// Input: set of file paths
65 ///
66 /// Output: map of path and raw contents (bytes)
67 ReadFiles(Result<HashMap<PathBuf, Vec<u8>>, HashSet<PathBuf>>),
68
69 /// I/O request to remove a filesystem directory.
70 ///
71 /// Input: directory path
72 ///
73 /// Output: none
74 RemoveDir(Result<(), PathBuf>),
75
76 /// I/O request to remove multiple filesystem directories.
77 ///
78 /// Input: set of directory paths
79 ///
80 /// Output: none
81 RemoveDirs(Result<(), HashSet<PathBuf>>),
82
83 /// I/O request to remove a filesystem file.
84 ///
85 /// Input: file path
86 ///
87 /// Output: none
88 RemoveFile(Result<(), PathBuf>),
89
90 /// I/O request to remove multiple filesystem files.
91 ///
92 /// Input: set of file paths
93 ///
94 /// Output: none
95 RemoveFiles(Result<(), HashSet<PathBuf>>),
96
97 /// I/O request to rename multiple filesystem files and/or
98 /// directories.
99 ///
100 /// Input: set of directory and/or file paths
101 ///
102 /// Output: none
103 Rename(Result<(), Vec<(PathBuf, PathBuf)>>),
104}
105
106impl fmt::Debug for FsIo {
107 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108 match self {
109 Self::CreateDir(Ok(_)) => f.write_str("create dir output"),
110 Self::CreateDir(Err(_)) => f.write_str("create dir input"),
111
112 Self::CreateDirs(Ok(_)) => f.write_str("create dirs output"),
113 Self::CreateDirs(Err(_)) => f.write_str("create dirs input"),
114
115 Self::CreateFile(Ok(_)) => f.write_str("create file output"),
116 Self::CreateFile(Err(_)) => f.write_str("create file input"),
117
118 Self::CreateFiles(Ok(_)) => f.write_str("create files output"),
119 Self::CreateFiles(Err(_)) => f.write_str("create files input"),
120
121 Self::ReadDir(Ok(_)) => f.write_str("read dir output"),
122 Self::ReadDir(Err(_)) => f.write_str("read dir input"),
123
124 Self::ReadFile(Ok(_)) => f.write_str("read file output"),
125 Self::ReadFile(Err(_)) => f.write_str("read file input"),
126
127 Self::ReadFiles(Ok(_)) => f.write_str("read files output"),
128 Self::ReadFiles(Err(_)) => f.write_str("read files input"),
129
130 Self::RemoveDir(Ok(_)) => f.write_str("remove dir output"),
131 Self::RemoveDir(Err(_)) => f.write_str("remove dir input"),
132
133 Self::RemoveDirs(Ok(_)) => f.write_str("remove dirs output"),
134 Self::RemoveDirs(Err(_)) => f.write_str("remove dirs input"),
135
136 Self::RemoveFile(Ok(_)) => f.write_str("remove file output"),
137 Self::RemoveFile(Err(_)) => f.write_str("remove file input"),
138
139 Self::RemoveFiles(Ok(_)) => f.write_str("remove files output"),
140 Self::RemoveFiles(Err(_)) => f.write_str("remove files input"),
141
142 Self::Rename(Ok(_)) => f.write_str("rename output"),
143 Self::Rename(Err(_)) => f.write_str("rename input"),
144 }
145 }
146}