Skip to content

Commit 6aeb68f

Browse files
committed
refactor: make use of common FsError and FsResult
Also improved inline documentation.
1 parent 6c3d530 commit 6aeb68f

File tree

19 files changed

+309
-338
lines changed

19 files changed

+309
-338
lines changed

src/coroutines/create-dir.rs

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,15 @@
1-
//! Module dedicated to the [`CreateDir`] I/O-free coroutine.
1+
//! I/O-free coroutine to create a filesystem directory.
22
33
use std::path::PathBuf;
44

55
use log::{debug, trace};
6-
use thiserror::Error;
76

8-
use crate::io::FsIo;
7+
use crate::{
8+
error::{FsError, FsResult},
9+
io::FsIo,
10+
};
911

10-
#[derive(Clone, Debug, Error)]
11-
pub enum CreateDirError {
12-
#[error("Missing input: path 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 CreateDirResult {
20-
Ok,
21-
Err(CreateDirError),
22-
Io(FsIo),
23-
}
24-
25-
/// I/O-free coroutine for creating a directory.
12+
/// I/O-free coroutine to create a filesystem directory.
2613
#[derive(Debug)]
2714
pub struct CreateDir {
2815
path: Option<PathBuf>,
@@ -35,27 +22,27 @@ impl CreateDir {
3522
Self { path }
3623
}
3724

38-
/// Makes create dir progress.
39-
pub fn resume(&mut self, arg: Option<FsIo>) -> CreateDirResult {
25+
/// Makes the coroutine progress.
26+
pub fn resume(&mut self, arg: Option<FsIo>) -> FsResult {
4027
let Some(arg) = arg else {
4128
let Some(path) = self.path.take() else {
42-
return CreateDirResult::Err(CreateDirError::MissingInput);
29+
return FsResult::Err(FsError::MissingInput);
4330
};
4431

4532
trace!("wants I/O to create directory at {}", path.display());
46-
return CreateDirResult::Io(FsIo::CreateDir(Err(path)));
33+
return FsResult::Io(FsIo::CreateDir(Err(path)));
4734
};
4835

4936
debug!("resume after creating directory");
5037

5138
let FsIo::CreateDir(io) = arg else {
52-
let err = CreateDirError::InvalidArgument("create dir output", arg);
53-
return CreateDirResult::Err(err);
39+
let err = FsError::InvalidArgument("create dir output", arg);
40+
return FsResult::Err(err);
5441
};
5542

5643
match io {
57-
Ok(()) => CreateDirResult::Ok,
58-
Err(path) => CreateDirResult::Io(FsIo::CreateDir(Err(path))),
44+
Ok(()) => FsResult::Ok(()),
45+
Err(path) => FsResult::Io(FsIo::CreateDir(Err(path))),
5946
}
6047
}
6148
}

src/coroutines/create-dirs.rs

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,48 @@
1-
//! Module dedicated to the [`CreateDirs`] I/O-free coroutine.
1+
//! I/O-free coroutine to create multiple filesystem directories.
22
33
use std::{collections::HashSet, path::PathBuf};
44

55
use log::{debug, trace};
6-
use thiserror::Error;
76

8-
use crate::io::FsIo;
7+
use crate::{
8+
error::{FsError, FsResult},
9+
io::FsIo,
10+
};
911

10-
#[derive(Clone, Debug, Error)]
11-
pub enum CreateDirsError {
12-
#[error("Missing input: paths 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 CreateDirsResult {
20-
Ok,
21-
Err(CreateDirsError),
22-
Io(FsIo),
23-
}
24-
25-
/// I/O-free coroutine for creating a dirsectory.
12+
/// I/O-free coroutine to create multiple filesystem directories.
2613
#[derive(Debug)]
2714
pub struct CreateDirs {
2815
paths: Option<HashSet<PathBuf>>,
2916
}
3017

3118
impl CreateDirs {
32-
/// Creates a new coroutine from the given dirsectory path.
19+
/// Creates a new coroutine from the given directory path.
3320
pub fn new(paths: impl IntoIterator<Item = PathBuf>) -> Self {
3421
let paths = Some(paths.into_iter().collect());
3522
Self { paths }
3623
}
3724

38-
/// Makes create dirs progress.
39-
pub fn resume(&mut self, arg: Option<FsIo>) -> CreateDirsResult {
25+
/// Makes the coroutine progress.
26+
pub fn resume(&mut self, arg: Option<FsIo>) -> FsResult {
4027
let Some(arg) = arg else {
4128
let Some(paths) = self.paths.take() else {
42-
return CreateDirsResult::Err(CreateDirsError::MissingInput);
29+
return FsResult::Err(FsError::MissingInput);
4330
};
4431

4532
trace!("wants I/O to create directories: {paths:?}");
46-
return CreateDirsResult::Io(FsIo::CreateDirs(Err(paths)));
33+
return FsResult::Io(FsIo::CreateDirs(Err(paths)));
4734
};
4835

4936
debug!("resume after creating directories");
5037

5138
let FsIo::CreateDirs(io) = arg else {
52-
let err = CreateDirsError::InvalidArgument("create dirs output", arg);
53-
return CreateDirsResult::Err(err);
39+
let err = FsError::InvalidArgument("create dirs output", arg);
40+
return FsResult::Err(err);
5441
};
5542

5643
match io {
57-
Ok(()) => CreateDirsResult::Ok,
58-
Err(path) => CreateDirsResult::Io(FsIo::CreateDirs(Err(path))),
44+
Ok(()) => FsResult::Ok(()),
45+
Err(path) => FsResult::Io(FsIo::CreateDirs(Err(path))),
5946
}
6047
}
6148
}

src/coroutines/create-file.rs

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,49 @@
1-
//! Module dedicated to the [`CreateFile`] I/O-free coroutine.
1+
//! I/O-free coroutine to create a filesystem file.
22
33
use std::path::PathBuf;
44

55
use log::{debug, trace};
6-
use thiserror::Error;
76

8-
use crate::io::FsIo;
7+
use crate::{
8+
error::{FsError, FsResult},
9+
io::FsIo,
10+
};
911

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.
2613
#[derive(Debug)]
2714
pub struct CreateFile {
2815
contents: Option<(PathBuf, Vec<u8>)>,
2916
}
3017

3118
impl CreateFile {
32-
/// Creates a new coroutine from the given fileectory path.
19+
/// Creates a new coroutine from the given file path and contents.
3320
pub fn new(path: impl Into<PathBuf>, contents: impl IntoIterator<Item = u8>) -> Self {
3421
let contents = contents.into_iter().collect();
3522
let contents = Some((path.into(), contents));
3623
Self { contents }
3724
}
3825

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 {
4128
let Some(arg) = arg else {
4229
let Some((path, contents)) = self.contents.take() else {
43-
return CreateFileResult::Err(CreateFileError::MissingInput);
30+
return FsResult::Err(FsError::MissingInput);
4431
};
4532

4633
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))));
4835
};
4936

5037
debug!("resume after creating file");
5138

5239
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);
5542
};
5643

5744
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))),
6047
}
6148
}
6249
}

src/coroutines/create-files.rs

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,22 @@
1-
//! Module dedicated to the [`CreateFiles`] I/O-free coroutine.
1+
//! I/O-free coroutine to create multiple filesystem files.
22
33
use std::{collections::HashMap, path::PathBuf};
44

55
use log::{debug, trace};
6-
use thiserror::Error;
76

8-
use crate::io::FsIo;
7+
use crate::{
8+
error::{FsError, FsResult},
9+
io::FsIo,
10+
};
911

10-
#[derive(Clone, Debug, Error)]
11-
pub enum CreateFilesError {
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 CreateFilesResult {
20-
Ok,
21-
Err(CreateFilesError),
22-
Io(FsIo),
23-
}
24-
25-
/// I/O-free coroutine for creating a file.
12+
/// I/O-free coroutine to create multiple filesystem files.
2613
#[derive(Debug)]
2714
pub struct CreateFiles {
2815
contents: Option<HashMap<PathBuf, Vec<u8>>>,
2916
}
3017

3118
impl CreateFiles {
32-
/// Creates a new coroutine from the given file path and contents.
19+
/// Creates a new coroutine from the given file paths and contents.
3320
pub fn new(
3421
contents: impl IntoIterator<Item = (impl Into<PathBuf>, impl IntoIterator<Item = u8>)>,
3522
) -> Self {
@@ -40,27 +27,27 @@ impl CreateFiles {
4027
Self { contents }
4128
}
4229

43-
/// Makes create files progress.
44-
pub fn resume(&mut self, arg: Option<FsIo>) -> CreateFilesResult {
30+
/// Makes the coroutine progress.
31+
pub fn resume(&mut self, arg: Option<FsIo>) -> FsResult {
4532
let Some(arg) = arg else {
4633
let Some(contents) = self.contents.take() else {
47-
return CreateFilesResult::Err(CreateFilesError::MissingInput);
34+
return FsResult::Err(FsError::MissingInput);
4835
};
4936

5037
trace!("wants I/O to create files");
51-
return CreateFilesResult::Io(FsIo::CreateFiles(Err(contents)));
38+
return FsResult::Io(FsIo::CreateFiles(Err(contents)));
5239
};
5340

5441
debug!("resume after creating files");
5542

5643
let FsIo::CreateFiles(io) = arg else {
57-
let err = CreateFilesError::InvalidArgument("create files output", arg);
58-
return CreateFilesResult::Err(err);
44+
let err = FsError::InvalidArgument("create files output", arg);
45+
return FsResult::Err(err);
5946
};
6047

6148
match io {
62-
Ok(()) => CreateFilesResult::Ok,
63-
Err(path) => CreateFilesResult::Io(FsIo::CreateFiles(Err(path))),
49+
Ok(()) => FsResult::Ok(()),
50+
Err(path) => FsResult::Io(FsIo::CreateFiles(Err(path))),
6451
}
6552
}
6653
}

src/coroutines/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! Collection of I/O-free, resumable and composable filesystem state
22
//! machines.
33
//!
4-
//! Coroutines emit [`Io`] requests that need to be processed by
4+
//! Coroutines emit [I/O] requests that need to be processed by
55
//! [runtimes] in order to continue their progression.
66
//!
7-
//! [`Io`]: crate::Io
7+
//! [I/O]: crate::io
88
//! [runtimes]: crate::runtimes
99
1010
#[path = "create-dir.rs"]

src/coroutines/read-dir.rs

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,50 @@
1-
//! Module dedicated to the [`ReadDir`] I/O-free coroutine.
1+
//! I/O-free coroutine to read entries contained inside a filesystem
2+
//! directory.
23
34
use std::{collections::HashSet, path::PathBuf};
45

56
use log::{debug, trace};
6-
use thiserror::Error;
77

8-
use crate::io::FsIo;
8+
use crate::{
9+
error::{FsError, FsResult},
10+
io::FsIo,
11+
};
912

10-
#[derive(Clone, Debug, Error)]
11-
pub enum ReadDirError {
12-
#[error("Missing input: path 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 ReadDirResult {
20-
Ok(HashSet<PathBuf>),
21-
Err(ReadDirError),
22-
Io(FsIo),
23-
}
24-
25-
/// I/O-free coroutine for reading directory entries.
13+
/// I/O-free coroutine to read entries contained inside a filesystem
14+
/// directory.
2615
#[derive(Debug)]
2716
pub struct ReadDir {
2817
path: Option<PathBuf>,
2918
}
3019

3120
impl ReadDir {
32-
/// Reads a new coroutine from the given directory path.
21+
/// Creates a new coroutine from the given directory path.
3322
pub fn new(path: impl Into<PathBuf>) -> Self {
3423
let input = Some(path.into());
3524
Self { path: input }
3625
}
3726

3827
/// Makes the coroutine progress.
39-
pub fn resume(&mut self, arg: Option<FsIo>) -> ReadDirResult {
28+
pub fn resume(&mut self, arg: Option<FsIo>) -> FsResult<HashSet<PathBuf>> {
4029
let Some(arg) = arg else {
4130
let Some(path) = self.path.take() else {
42-
return ReadDirResult::Err(ReadDirError::MissingInput);
31+
return FsResult::Err(FsError::MissingInput);
4332
};
4433

4534
trace!("wants I/O to read directory at {}", path.display());
46-
return ReadDirResult::Io(FsIo::ReadDir(Err(path)));
35+
return FsResult::Io(FsIo::ReadDir(Err(path)));
4736
};
4837

4938
debug!("resume after reading directory");
5039

5140
let FsIo::ReadDir(io) = arg else {
52-
let err = ReadDirError::InvalidArgument("read dir output", arg);
53-
return ReadDirResult::Err(err);
41+
let err = FsError::InvalidArgument("read dir output", arg);
42+
return FsResult::Err(err);
5443
};
5544

5645
match io {
57-
Ok(paths) => ReadDirResult::Ok(paths),
58-
Err(path) => ReadDirResult::Io(FsIo::ReadDir(Err(path))),
46+
Ok(paths) => FsResult::Ok(paths),
47+
Err(path) => FsResult::Io(FsIo::ReadDir(Err(path))),
5948
}
6049
}
6150
}

0 commit comments

Comments
 (0)