|
1 | | -//! Module dedicated to the [`CreateFile`] I/O-free coroutine. |
| 1 | +//! I/O-free coroutine to create a filesystem file. |
2 | 2 |
|
3 | 3 | use std::path::PathBuf; |
4 | 4 |
|
5 | 5 | use log::{debug, trace}; |
6 | | -use thiserror::Error; |
7 | 6 |
|
8 | | -use crate::io::FsIo; |
| 7 | +use crate::{ |
| 8 | + error::{FsError, FsResult}, |
| 9 | + io::FsIo, |
| 10 | +}; |
9 | 11 |
|
10 | | -#[derive(Clone, Debug, Error)] |
11 | | -pub enum CreateFileError { |
12 | | - #[error("Missing input: contents missing or already consumed")] |
13 | | - MissingInput, |
14 | | - #[error("Invalid argument: expected {0}, got {1:?}")] |
15 | | - InvalidArgument(&'static str, FsIo), |
16 | | -} |
17 | | - |
18 | | -#[derive(Clone, Debug)] |
19 | | -pub enum CreateFileResult { |
20 | | - Ok, |
21 | | - Err(CreateFileError), |
22 | | - Io(FsIo), |
23 | | -} |
24 | | - |
25 | | -/// I/O-free coroutine for creating a fileectory. |
| 12 | +/// I/O-free coroutine to create a filesystem file. |
26 | 13 | #[derive(Debug)] |
27 | 14 | pub struct CreateFile { |
28 | 15 | contents: Option<(PathBuf, Vec<u8>)>, |
29 | 16 | } |
30 | 17 |
|
31 | 18 | impl CreateFile { |
32 | | - /// Creates a new coroutine from the given fileectory path. |
| 19 | + /// Creates a new coroutine from the given file path and contents. |
33 | 20 | pub fn new(path: impl Into<PathBuf>, contents: impl IntoIterator<Item = u8>) -> Self { |
34 | 21 | let contents = contents.into_iter().collect(); |
35 | 22 | let contents = Some((path.into(), contents)); |
36 | 23 | Self { contents } |
37 | 24 | } |
38 | 25 |
|
39 | | - /// Makes create file progress. |
40 | | - pub fn resume(&mut self, arg: Option<FsIo>) -> CreateFileResult { |
| 26 | + /// Makes the coroutine progress. |
| 27 | + pub fn resume(&mut self, arg: Option<FsIo>) -> FsResult { |
41 | 28 | let Some(arg) = arg else { |
42 | 29 | let Some((path, contents)) = self.contents.take() else { |
43 | | - return CreateFileResult::Err(CreateFileError::MissingInput); |
| 30 | + return FsResult::Err(FsError::MissingInput); |
44 | 31 | }; |
45 | 32 |
|
46 | 33 | trace!("wants I/O to create file at {}", path.display()); |
47 | | - return CreateFileResult::Io(FsIo::CreateFile(Err((path, contents)))); |
| 34 | + return FsResult::Io(FsIo::CreateFile(Err((path, contents)))); |
48 | 35 | }; |
49 | 36 |
|
50 | 37 | debug!("resume after creating file"); |
51 | 38 |
|
52 | 39 | let FsIo::CreateFile(io) = arg else { |
53 | | - let err = CreateFileError::InvalidArgument("create file output", arg); |
54 | | - return CreateFileResult::Err(err); |
| 40 | + let err = FsError::InvalidArgument("create file output", arg); |
| 41 | + return FsResult::Err(err); |
55 | 42 | }; |
56 | 43 |
|
57 | 44 | match io { |
58 | | - Ok(()) => CreateFileResult::Ok, |
59 | | - Err(path) => CreateFileResult::Io(FsIo::CreateFile(Err(path))), |
| 45 | + Ok(()) => FsResult::Ok(()), |
| 46 | + Err(path) => FsResult::Io(FsIo::CreateFile(Err(path))), |
60 | 47 | } |
61 | 48 | } |
62 | 49 | } |
0 commit comments