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
//! I/O-free coroutine to list Vdir collections.
use std::{
collections::HashSet,
path::{Path, PathBuf},
};
use io_fs::{
coroutines::{read_dir::ReadDir, read_files::ReadFiles},
error::{FsError, FsResult},
io::FsIo,
};
use thiserror::Error;
use crate::{
collection::Collection,
constants::{COLOR, DESCRIPTION, DISPLAYNAME},
};
/// Errors that can occur during the coroutine progression.
#[derive(Clone, Debug, Error)]
pub enum ListCollectionsError {
/// An error occured during the directory listing.
#[error("List Vdir collections error")]
ListDirsError(#[source] FsError),
/// An error occured during the metadata file listing.
#[error("Read Vdir collections' metadata error")]
ListFilesError(#[source] FsError),
}
/// Output emitted when the coroutine terminates its progression.
#[derive(Clone, Debug)]
pub enum ListCollectionsResult {
/// The coroutine successfully terminated its progression.
Ok(HashSet<Collection>),
/// The coroutine encountered an error.
Err(ListCollectionsError),
/// An I/O needs to be processed in order to make the coroutine
/// progress further.
Io(FsIo),
}
#[derive(Debug)]
enum State {
ListCollections(ReadDir),
ReadMetadataFiles(HashSet<PathBuf>, ReadFiles),
}
/// I/O-free coroutine to list Vdir collections.
#[derive(Debug)]
pub struct ListCollections {
state: State,
}
impl ListCollections {
/// Creates a new coroutine from the given root path.
pub fn new(root: impl AsRef<Path>) -> Self {
let fs = ReadDir::new(root.as_ref());
let state = State::ListCollections(fs);
Self { state }
}
/// Makes the coroutine progress.
pub fn resume(&mut self, mut arg: Option<FsIo>) -> ListCollectionsResult {
loop {
match &mut self.state {
State::ListCollections(fs) => {
let mut collection_paths = match fs.resume(arg.take()) {
FsResult::Ok(paths) => paths,
FsResult::Io(io) => break ListCollectionsResult::Io(io),
FsResult::Err(err) => {
let err = ListCollectionsError::ListDirsError(err);
break ListCollectionsResult::Err(err);
}
};
collection_paths.retain(|path| path.is_dir());
let mut metadata_paths = HashSet::new();
for dir in &collection_paths {
let name_path = dir.join(DISPLAYNAME);
if name_path.is_file() {
metadata_paths.insert(name_path);
}
let desc_path = dir.join(DESCRIPTION);
if desc_path.is_file() {
metadata_paths.insert(desc_path);
}
let color_path = dir.join(COLOR);
if color_path.is_file() {
metadata_paths.insert(color_path);
}
}
let flow = ReadFiles::new(metadata_paths);
self.state = State::ReadMetadataFiles(collection_paths, flow);
}
State::ReadMetadataFiles(collection_paths, fs) => {
let mut metadata = match fs.resume(arg.take()) {
FsResult::Ok(meta) => meta,
FsResult::Io(io) => break ListCollectionsResult::Io(io),
FsResult::Err(err) => {
let err = ListCollectionsError::ListFilesError(err);
break ListCollectionsResult::Err(err);
}
};
let mut collections = HashSet::new();
for path in collection_paths.clone() {
let display_name = path.join(DISPLAYNAME);
let description = path.join(DESCRIPTION);
let color = path.join(COLOR);
let mut collection = Collection {
path,
display_name: None,
description: None,
color: None,
};
if let Some(name) = &metadata.remove(&display_name) {
let name = String::from_utf8_lossy(name);
if name.trim().is_empty() {
collection.display_name = None
} else {
collection.display_name = Some(name.to_string());
}
}
if let Some(desc) = &metadata.remove(&description) {
let desc = String::from_utf8_lossy(desc);
if desc.trim().is_empty() {
collection.description = None
} else {
collection.description = Some(desc.to_string());
}
}
if let Some(color) = &metadata.remove(&color) {
let color = String::from_utf8_lossy(color);
if color.trim().is_empty() {
collection.color = None
} else {
collection.color = Some(color.to_string());
}
}
collections.insert(collection);
}
break ListCollectionsResult::Ok(collections);
}
}
}
}
}