switchy_fs/
lib.rs

1#![cfg_attr(feature = "fail-on-warnings", deny(warnings))]
2#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
3#![allow(clippy::multiple_crate_versions)]
4
5#[cfg(feature = "simulator")]
6pub mod simulator;
7#[cfg(feature = "std")]
8pub mod standard;
9#[cfg(feature = "tokio")]
10pub mod tokio;
11
12#[cfg(all(feature = "sync", feature = "std"))]
13pub trait GenericSyncFile:
14    Send + Sync + ::std::io::Read + ::std::io::Write + ::std::io::Seek
15{
16}
17
18#[cfg(all(feature = "async", feature = "tokio"))]
19pub trait GenericAsyncFile:
20    Send
21    + Sync
22    + switchy_async::io::AsyncRead
23    + switchy_async::io::AsyncWrite
24    + switchy_async::io::AsyncSeek
25{
26}
27
28#[allow(unused)]
29macro_rules! impl_open_options {
30    ($(,)?) => {
31        pub struct OpenOptions {
32            pub(crate) create: bool,
33            pub(crate) append: bool,
34            pub(crate) read: bool,
35            pub(crate) write: bool,
36            pub(crate) truncate: bool,
37        }
38
39        impl Default for OpenOptions {
40            fn default() -> Self {
41                Self::new()
42            }
43        }
44
45        impl OpenOptions {
46            #[must_use]
47            pub const fn new() -> Self {
48                Self {
49                    create: false,
50                    append: false,
51                    read: false,
52                    write: false,
53                    truncate: false,
54                }
55            }
56
57            #[must_use]
58            pub const fn create(mut self, create: bool) -> Self {
59                self.create = create;
60                self
61            }
62
63            #[must_use]
64            pub const fn append(mut self, append: bool) -> Self {
65                self.append = append;
66                self
67            }
68
69            #[must_use]
70            pub const fn read(mut self, read: bool) -> Self {
71                self.read = read;
72                self
73            }
74
75            #[must_use]
76            pub const fn write(mut self, write: bool) -> Self {
77                self.write = write;
78                self
79            }
80
81            #[must_use]
82            pub const fn truncate(mut self, truncate: bool) -> Self {
83                self.truncate = truncate;
84                self
85            }
86        }
87    };
88}
89
90#[allow(unused)]
91macro_rules! impl_sync_fs {
92    ($module:ident $(,)?) => {
93        #[cfg(feature = "sync")]
94        pub mod sync {
95            pub use $crate::$module::sync::{File, create_dir_all, read_to_string, remove_dir_all};
96
97            impl_open_options!();
98        }
99    };
100}
101
102#[allow(unused)]
103macro_rules! impl_async_fs {
104    ($module:ident $(,)?) => {
105        #[cfg(feature = "async")]
106        pub mod unsync {
107            pub use $crate::$module::unsync::{
108                File, create_dir_all, read_to_string, remove_dir_all,
109            };
110
111            impl_open_options!();
112
113            #[cfg(feature = "sync")]
114            impl OpenOptions {
115                #[must_use]
116                pub const fn into_sync(self) -> crate::sync::OpenOptions {
117                    crate::sync::OpenOptions {
118                        create: self.create,
119                        append: self.append,
120                        read: self.read,
121                        write: self.write,
122                        truncate: self.truncate,
123                    }
124                }
125            }
126
127            #[cfg(feature = "sync")]
128            impl From<OpenOptions> for crate::sync::OpenOptions {
129                fn from(value: OpenOptions) -> Self {
130                    value.into_sync()
131                }
132            }
133        }
134    };
135}
136
137#[cfg(feature = "simulator")]
138impl_sync_fs!(simulator);
139#[cfg(feature = "simulator")]
140impl_async_fs!(simulator);
141
142#[cfg(all(not(feature = "simulator"), feature = "std"))]
143impl_sync_fs!(standard);
144
145#[cfg(all(not(feature = "simulator"), feature = "tokio"))]
146impl_async_fs!(tokio);