io_fs/coroutines/
create-dirs.rs

1//! I/O-free coroutine to create multiple filesystem directories.
2
3use std::{collections::HashSet, path::PathBuf};
4
5use log::{debug, trace};
6
7use crate::{
8    error::{FsError, FsResult},
9    io::FsIo,
10};
11
12/// I/O-free coroutine to create multiple filesystem directories.
13#[derive(Debug)]
14pub struct CreateDirs {
15    paths: Option<HashSet<PathBuf>>,
16}
17
18impl CreateDirs {
19    /// Creates a new coroutine from the given directory path.
20    pub fn new(paths: impl IntoIterator<Item = PathBuf>) -> Self {
21        let paths = Some(paths.into_iter().collect());
22        Self { paths }
23    }
24
25    /// Makes the coroutine progress.
26    pub fn resume(&mut self, arg: Option<FsIo>) -> FsResult {
27        let Some(arg) = arg else {
28            let Some(paths) = self.paths.take() else {
29                return FsResult::Err(FsError::MissingInput);
30            };
31
32            trace!("wants I/O to create directories: {paths:?}");
33            return FsResult::Io(FsIo::CreateDirs(Err(paths)));
34        };
35
36        debug!("resume after creating directories");
37
38        let FsIo::CreateDirs(io) = arg else {
39            let err = FsError::InvalidArgument("create dirs output", arg);
40            return FsResult::Err(err);
41        };
42
43        match io {
44            Ok(()) => FsResult::Ok(()),
45            Err(path) => FsResult::Io(FsIo::CreateDirs(Err(path))),
46        }
47    }
48}