dir-structure 0.3.0

Model directory structures as plain Rust structs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//! Asynchronous virtual file system traits.
//!
//! These traits are similar to the ones in the [`vfs`](super::vfs) module, but they use
//! asynchronous I/O operations, and return `Future`s resolving to `Result`s instead of
//! directly returning `Result`s, in line with Rust's async programming model.
//!
//! These traits require the `async` feature to be enabled.
//!
//! They are designed to be used with async runtimes like Tokio or async-std.
//!
//! You should refer to the documentation of the [`vfs`](super::vfs) module for
//! more details on the individual methods, as the async versions have the same semantics,
//! just with async I/O.
//!
//! An important difference between the [`VfsAsync`] traits and their synchronous counterparts
//! is that the methods take _owned_ paths instead of references. This is because
//! the async methods typically need to move the path into the future, and
//! references would not be valid for the entire duration of the future.
//!
//! # Tool specific extensions of the async VFS traits
//!
//! Similarly to the synchronous VFS traits, there are also tool-specific extensions
//! of the async VFS traits, which are required to use certain tools with a specific
//! [`VfsAsync`] implementation.
//!
//! We list them here for convenience, but you should refer to the documentation of the
//! individual tools for more details.
//!
//! ## [`AtomicDir<T>`](crate::atomic_dir::AtomicDir)
//!
//! To use the [`AtomicDir<T>`](crate::atomic_dir::AtomicDir) wrapper type with your async VFS implementation,
//! the VFS type itself must implement the [`VfsSupportsTemporaryDirectories`](crate::atomic_dir::VfsSupportsTemporaryDirectories) trait.
//! See its documentation for more details.
//!
//! ## [Images](crate::image)
//!
//! To allow reading and writing image files using the types and traits from the
//! [`image`](crate::image) module, the following traits need to be implemented for your
//! async VFS type `YourVfsType`:
//!
//! - `impl<T: ImgFormat> ReadImageFromAsync<T> for YourVfsType`                                  to satisfy the bound `T: ReadFromAsync<'vfs, YourVfsType>`
//! - `impl<'a> WriteImageToAsync<'a> for YourVfsType`                                            to satisfy the bound `T: WriteToAsync<'a, YourVfsType>`
//! - `impl<'a> WriteImageToAsyncRef<'a, YourVfsType> for YourVfsType`                            to satisfy the bound `T: WriteToAsyncRef<'a, YourVfsType>`
//!
//! These impls are required because the image encoding and decoding operations are CPU-bound and blocking,
//! and thus cannot be implemented in a generic way for all async VFS implementations. They need to be implemented
//! specifically for each async VFS type.
//!
//! You can see the implementations for the [`TokioFsVfs`](crate::vfs::tokio_fs_vfs::TokioFsVfs) VFS, which use
//! [`tokio::task::spawn_blocking`] to offload the blocking operations to a separate thread pool.
//!
//! Your async VFS implementation might use a different async runtime, and thus might need to use a
//! different method to offload blocking operations, but that is the ideal approach to take.
//!
//! You can of course not implement these impls, but then you will not be able to use the image
//! encoding and decoding operations with your async VFS implementation.

use std::io;
use std::pin::Pin;
use std::task::Context;
use std::task::Poll;

use futures::AsyncWrite;
use futures::Stream;
use futures::io::AsyncRead;
use futures::io::AsyncSeek;
use pin_project::pin_project;

use crate::error::Error;
use crate::error::Result;
use crate::error::VfsResult;
use crate::prelude::*;
use crate::traits::vfs::DirEntryInfo;
use crate::traits::vfs::OwnedPathType;
use crate::traits::vfs::PathType;
use crate::traits::vfs::VfsCore;

/// An asynchronous virtual file system.
///
/// This is the asynchronous counterpart to the [`Vfs`] trait in the [`vfs`](super::vfs) module.
///
/// Writing operations are provided by the [`WriteSupportingVfsAsync` trait](self::WriteSupportingVfsAsync).
pub trait VfsAsync: VfsCore + Send + Sync + Unpin {
    /// The type of the file returned by the [`open_read` method](VfsAsync::open_read).
    type RFile: AsyncRead + Send + Unpin;
    /// The future returned by the [`open_read` method](VfsAsync::open_read).
    type OpenReadFuture: Future<Output = VfsResult<Self::RFile, Self>> + Send + Unpin;

    /// Opens a file for reading, at the specified path.
    fn open_read(
        self: Pin<&Self>,
        path: <<Self as VfsCore>::Path as PathType>::OwnedPath,
    ) -> Self::OpenReadFuture;

    /// The future returned by the [`read` method](VfsAsync::read).
    type ReadFuture<'a>: Future<Output = VfsResult<Vec<u8>, Self>> + Send + Unpin + 'a
    where
        Self: 'a;

    /// Reads the contents of a file, at the specified path.
    fn read<'a>(
        self: Pin<&'a Self>,
        path: <<Self as VfsCore>::Path as PathType>::OwnedPath,
    ) -> Self::ReadFuture<'a>;

    /// The future returned by the [`read_string` method](VfsAsync::read_string).
    type ReadStringFuture<'a>: Future<Output = VfsResult<String, Self>> + Send + Unpin + 'a
    where
        Self: 'a;

    /// Reads the contents of a file, at the specified path, and returns it as a string.
    fn read_string<'a>(
        self: Pin<&'a Self>,
        path: <<Self as VfsCore>::Path as PathType>::OwnedPath,
    ) -> Self::ReadStringFuture<'a>;

    /// The future returned by the [`exists` method](VfsAsync::exists).
    type ExistsFuture<'a>: Future<Output = VfsResult<bool, Self>> + Send + 'a
    where
        Self: 'a;

    /// Checks if a file exists at the specified path.
    fn exists<'a>(
        self: Pin<&'a Self>,
        path: <<Self as VfsCore>::Path as PathType>::OwnedPath,
    ) -> Self::ExistsFuture<'a>;

    /// The future type returned by the [`is_dir` method](VfsAsync::is_dir).
    type IsDirFuture<'a>: Future<Output = VfsResult<bool, Self>> + Send + 'a
    where
        Self: 'a;

    /// Checks if a directory exists at the specified path.
    fn is_dir<'a>(
        self: Pin<&'a Self>,
        path: <<Self as VfsCore>::Path as PathType>::OwnedPath,
    ) -> Self::IsDirFuture<'a>;

    /// The stream type returned by the [`DirWalkFuture`](VfsAsync::DirWalkFuture).
    type DirWalk<'a>: Stream<Item = VfsResult<DirEntryInfo<<Self as VfsCore>::Path>, Self>>
        + Send
        + 'a
    where
        Self: 'a;

    /// The future type returned by the [`walk_dir` method](VfsAsync::walk_dir).
    type DirWalkFuture<'a>: Future<Output = VfsResult<Self::DirWalk<'a>, Self>> + Send + 'a
    where
        Self: 'a;

    /// Walks a directory at the given path, returning a stream of directory entries.
    fn walk_dir<'a>(
        self: Pin<&'a Self>,
        path: <<Self as VfsCore>::Path as PathType>::OwnedPath,
    ) -> Self::DirWalkFuture<'a>;
}

/// Marks that the [`RFile`](VfsAsync::RFile) type of this [`VfsAsync`] also implements
/// [`AsyncSeek`], allowing it to be used in contexts that require seeking, such as image decoding.
///
/// This trait is automatically implemented for any [`VfsAsync`] whose [`RFile`](VfsAsync::RFile) implements
/// [`AsyncSeek`].
pub trait VfsAsyncWithSeekRead: VfsAsync
where
    Self::RFile: AsyncSeek + Send + Unpin,
{
}

impl<T: VfsAsync> VfsAsyncWithSeekRead for T where T::RFile: AsyncSeek + Send + Unpin {}

/// Extension trait for [`VfsAsync`] that provides additional convenience methods.
pub trait VfsAsyncExt: VfsAsync {
    /// Reads a file / directory at the specified path, and parses it into the specified type using its
    /// [`ReadFromAsync`] implementation.
    ///
    /// This method takes `self` as a pinned reference, to ensure that the [`VfsAsync`] value
    /// is not moved while the read operation is in progress.
    fn read_typed_async_pinned<'a, T: ReadFromAsync<'a, Self>>(
        self: Pin<&'a Self>,
        path: impl Into<<<Self as VfsCore>::Path as PathType>::OwnedPath>,
    ) -> T::Future {
        T::read_from_async(path.into(), self)
    }

    /// Reads a file / directory at the specified path, and parses it into the specified type using its
    /// [`ReadFromAsync`] implementation.
    ///
    /// This method takes `self` as a regular reference, and pins it internally.
    fn read_typed_async<'a, T: ReadFromAsync<'a, Self>>(
        &'a self,
        path: impl Into<<<Self as VfsCore>::Path as PathType>::OwnedPath>,
    ) -> T::Future {
        Pin::new(self).read_typed_async_pinned::<T>(path)
    }
}

// Blanket impl.
impl<V: VfsAsync + ?Sized> VfsAsyncExt for V {}

/// A virtual file system that supports writing operations.
pub trait WriteSupportingVfsAsync: VfsAsync {
    /// The type of the file returned by the [`open_write` method](WriteSupportingVfsAsync::open_write).
    type WFile: AsyncWrite + Send + Unpin;

    /// The future type returned by the [`open_write` method](WriteSupportingVfsAsync::open_write).
    type OpenWriteFuture: Future<Output = VfsResult<Self::WFile, Self>> + Send + Unpin;

    /// Opens a file for writing, at the specified path.
    fn open_write(
        self: Pin<&Self>,
        path: <<Self as VfsCore>::Path as PathType>::OwnedPath,
    ) -> Self::OpenWriteFuture;

    /// The future type returned by the [`write` method](WriteSupportingVfsAsync::write).
    type WriteFuture<'a>: Future<Output = VfsResult<(), Self>> + Send + Unpin + 'a
    where
        Self: 'a;

    /// Writes the contents of a file, at the specified path.
    fn write<'d, 'a: 'd>(
        self: Pin<&'a Self>,
        path: <<Self as VfsCore>::Path as PathType>::OwnedPath,
        data: &'d [u8],
    ) -> Self::WriteFuture<'d>;

    /// The future type returned by the [`remove_dir_all` method](WriteSupportingVfsAsync::remove_dir_all).
    type RemoveDirAllFuture<'a>: Future<Output = VfsResult<(), Self>> + Send + 'a
    where
        Self: 'a;

    /// Removes a directory and all its contents.
    fn remove_dir_all<'a>(
        self: Pin<&'a Self>,
        path: <<Self as VfsCore>::Path as PathType>::OwnedPath,
    ) -> Self::RemoveDirAllFuture<'a>;

    /// The future type returned by the [`create_dir` method](WriteSupportingVfsAsync::create_dir).
    type CreateDirFuture<'a>: Future<Output = VfsResult<(), Self>> + Send + 'a
    where
        Self: 'a;
    /// Creates a new directory at the specified path.
    fn create_dir<'a>(
        self: Pin<&'a Self>,
        path: <<Self as VfsCore>::Path as PathType>::OwnedPath,
    ) -> Self::CreateDirFuture<'a>;

    /// The future type returned by the [`create_dir_all` method](WriteSupportingVfsAsync::create_dir_all).
    type CreateDirAllFuture<'a>: Future<Output = VfsResult<(), Self>> + Send + 'a
    where
        Self: 'a;
    /// Creates a new directory and all its parent directories at the specified path.
    fn create_dir_all<'a>(
        self: Pin<&'a Self>,
        path: <<Self as VfsCore>::Path as PathType>::OwnedPath,
    ) -> Self::CreateDirAllFuture<'a>;

    /// The future type returned by the [`create_parent_dir` method](WriteSupportingVfsAsync::create_parent_dir).
    type CreateParentDirFuture<'a>: Future<Output = VfsResult<(), Self>> + Send + 'a
    where
        Self: 'a;
    /// Creates a new parent directory at the specified path.
    fn create_parent_dir<'a>(
        self: Pin<&'a Self>,
        path: <<Self as VfsCore>::Path as PathType>::OwnedPath,
    ) -> Self::CreateParentDirFuture<'a>;
}

/// Marks that the [`WFile`](WriteSupportingVfsAsync::WFile) type of this [`WriteSupportingVfsAsync`] also implements
/// [`AsyncSeek`], allowing it to be used in
/// contexts that require seeking.
pub trait VfsAsyncWithSeekWrite: WriteSupportingVfsAsync
where
    Self::WFile: AsyncSeek + Send + Unpin,
{
}

impl<T: WriteSupportingVfsAsync> VfsAsyncWithSeekWrite for T where T::WFile: AsyncSeek + Send + Unpin
{}

/// Extension trait for [`WriteSupportingVfsAsync`] that provides additional convenience methods.
pub trait WriteSupportingVfsAsyncExt: WriteSupportingVfsAsync {
    /// Writes a file / directory at the specified path, using the specified data type's
    /// [`WriteToAsync`] implementation.
    ///
    /// This method takes `self` as a pinned reference, to ensure that the `VfsAsync` implementation
    /// is not moved while the write operation is in progress.
    fn write_typed_async_ref_pinned<'r, 'a: 'r, T: WriteToAsyncRef<'a, Self>>(
        self: Pin<&'r Self>,
        path: impl Into<<<Self as VfsCore>::Path as PathType>::OwnedPath>,
        value: &'r T,
    ) -> T::Future<'r> {
        T::write_to_async_ref(value, path.into(), self)
    }

    /// Writes a file / directory at the specified path, using the specified data type's
    /// [`WriteToAsync`] implementation.
    ///
    /// This method takes `self` as a regular reference, and pins it internally.
    fn write_typed_async_ref<'r, 'a: 'r, T: WriteToAsyncRef<'a, Self>>(
        &'r self,
        path: impl Into<<<Self as VfsCore>::Path as PathType>::OwnedPath>,
        data: &'r T,
    ) -> T::Future<'r>
    where
        Self: Unpin,
    {
        Pin::new(self).write_typed_async_ref_pinned(path, data)
    }

    /// Writes a file / directory at the specified path, using the specified data type's
    /// [`WriteToAsync`] implementation.
    ///
    /// This method takes `self` as a pinned reference, to ensure that the `VfsAsync` implementation
    /// is not moved while the write operation is in progress.
    fn write_typed_async_pinned<'a, T: WriteToAsync<'a, Self>>(
        self: Pin<&'a Self>,
        path: impl Into<<<Self as VfsCore>::Path as PathType>::OwnedPath>,
        value: T,
    ) -> T::Future {
        value.write_to_async(path.into(), self)
    }

    /// Writes a file / directory at the specified path, using the specified data type's
    /// [`WriteToAsync`] implementation.
    ///
    /// This method takes `self` as a regular reference, and pins it internally.
    fn write_typed_async<'a, T: WriteToAsync<'a, Self>>(
        &'a self,
        path: impl Into<<<Self as VfsCore>::Path as PathType>::OwnedPath>,
        value: T,
    ) -> T::Future
    where
        Self: Unpin,
    {
        Pin::new(self).write_typed_async_pinned(path, value)
    }
}

// Blanket impl.
impl<V: WriteSupportingVfsAsync + ?Sized> WriteSupportingVfsAsyncExt for V {}

#[pin_project(project_replace = CreateParentDirDefaultFutureProjOwn)]
#[doc(hidden)]
pub enum CreateParentDirDefaultFuture<'a, Vfs: WriteSupportingVfsAsync + 'a>
where
    for<'f> Vfs::ExistsFuture<'f>: Future<Output = VfsResult<bool, Vfs>> + Unpin,
    for<'f> Vfs::CreateDirAllFuture<'f>: Future<Output = VfsResult<(), Vfs>> + Unpin,
{
    Poison,
    Start {
        vfs: Pin<&'a Vfs>,
        path: <<Vfs as VfsCore>::Path as PathType>::OwnedPath,
    },
    ExistsFuture {
        vfs: Pin<&'a Vfs>,
        path: <<Vfs as VfsCore>::Path as PathType>::OwnedPath,
        exists_future: Vfs::ExistsFuture<'a>,
    },
    CreateDirAllFuture {
        vfs: Pin<&'a Vfs>,
        create_dir_all_future: Vfs::CreateDirAllFuture<'a>,
    },
}

impl<'a, Vfs: WriteSupportingVfsAsync + 'a> Future for CreateParentDirDefaultFuture<'a, Vfs>
where
    for<'f> Vfs::ExistsFuture<'f>: Future<Output = VfsResult<bool, Vfs>> + Unpin,
    for<'f> Vfs::CreateDirAllFuture<'f>: Future<Output = VfsResult<(), Vfs>> + Unpin,
{
    type Output = VfsResult<(), Vfs>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.as_mut().project_replace(Self::Poison);
        match this {
            CreateParentDirDefaultFutureProjOwn::Start { vfs, path } => {
                self.project_replace(Self::ExistsFuture {
                    exists_future: vfs.exists(path.clone()),
                    vfs,
                    path,
                });
                cx.waker().wake_by_ref();
                Poll::Pending
            }
            CreateParentDirDefaultFutureProjOwn::ExistsFuture {
                vfs,
                path,
                mut exists_future,
            } => match Pin::new(&mut exists_future).poll(cx) {
                Poll::Ready(Ok(true)) => Poll::Ready(Ok(())),
                Poll::Ready(Ok(false)) => {
                    self.project_replace(Self::CreateDirAllFuture {
                        create_dir_all_future: vfs.create_dir_all(path),
                        vfs,
                    });
                    cx.waker().wake_by_ref();
                    Poll::Pending
                }
                Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
                Poll::Pending => {
                    self.project_replace(Self::ExistsFuture {
                        vfs,
                        path,
                        exists_future,
                    });
                    Poll::Pending
                }
            },
            CreateParentDirDefaultFutureProjOwn::CreateDirAllFuture {
                vfs,
                mut create_dir_all_future,
            } => match Pin::new(&mut create_dir_all_future).poll(cx) {
                Poll::Ready(Ok(())) => Poll::Ready(Ok(())),
                Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
                Poll::Pending => {
                    self.project_replace(Self::CreateDirAllFuture {
                        vfs,
                        create_dir_all_future,
                    });
                    Poll::Pending
                }
            },
            CreateParentDirDefaultFutureProjOwn::Poison => {
                panic!("CreateParentDirDefaultFuture polled after completion")
            }
        }
    }
}

#[pin_project(project = IoErrorWrapperFutureProj)]
#[doc(hidden)]
pub struct IoErrorWrapperFuture<T, F: Future<Output = io::Result<T>>, P: OwnedPathType> {
    #[pin]
    future: F,
    path: P,
}

impl<T, F, P> IoErrorWrapperFuture<T, F, P>
where
    F: Future<Output = io::Result<T>>,
    P: OwnedPathType,
{
    pub fn new(path: P, future: F) -> Self {
        Self { future, path }
    }
}

impl<T, F, P> Future for IoErrorWrapperFuture<T, F, P>
where
    F: Future<Output = io::Result<T>>,
    P: OwnedPathType + Clone,
{
    type Output = Result<T, P>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.as_mut().project();
        match this.future.poll(cx) {
            Poll::Ready(Ok(value)) => Poll::Ready(Ok(value)),
            Poll::Ready(Err(e)) => {
                let path = self.path.clone();
                Poll::Ready(Err(Error::Io(path, e)))
            }
            Poll::Pending => Poll::Pending,
        }
    }
}