io_fs/coroutines/
remove-file.rs

1//! I/O-free coroutine to remove a filesystem file.
2
3use std::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 remove a filesystem file.
13#[derive(Debug)]
14pub struct RemoveFile {
15    path: Option<PathBuf>,
16}
17
18impl RemoveFile {
19    /// Creates a new coroutine from the given file path.
20    pub fn new(path: impl Into<PathBuf>) -> Self {
21        let path = Some(path.into());
22        Self { path }
23    }
24
25    /// Makes remove file progress.
26    pub fn resume(&mut self, arg: Option<FsIo>) -> FsResult {
27        let Some(arg) = arg else {
28            let Some(path) = self.path.take() else {
29                return FsResult::Err(FsError::MissingInput);
30            };
31
32            trace!("wants I/O to remove fileectory at {}", path.display());
33            return FsResult::Io(FsIo::RemoveFile(Err(path)));
34        };
35
36        debug!("resume after creating fileectory");
37
38        let FsIo::RemoveFile(io) = arg else {
39            let err = FsError::InvalidArgument("remove file output", arg);
40            return FsResult::Err(err);
41        };
42
43        match io {
44            Ok(()) => FsResult::Ok(()),
45            Err(path) => FsResult::Io(FsIo::RemoveFile(Err(path))),
46        }
47    }
48}