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
171
172
173
174
175
176
177
178
179
180
use crate;
use crate::;
use HashMap;
use Error;
use Path;
use Arc;
use thread;
/// Reads and processes a specific MAME data file based on the provided data type.
///
/// This function handles the retrieval of a MAME data file from the extracted folder and processes it
/// by reading its contents and returning a map of machine details. It first checks if the required
/// data file is present in the expected location and then reads the file using a specialized function
/// for the provided `MameDataType`. Progress updates and messages are provided via a callback function.
///
/// # Parameters
/// - `data_type`: The `MameDataType` that specifies which type of MAME data file to read (e.g., ROMs, DAT files).
/// - `workspace_path`: A reference to a `Path` representing the base directory where the data file is located.
/// - `progress_callback`: A callback function of type `ProgressCallback` that tracks progress and provides status updates.
/// The callback receives a `ProgressInfo` struct containing `progress`, `total`, `message`, and `callback_type`.
///
/// # Returns
/// Returns a `Result<HashMap<String, Machine>, Box<dyn Error + Send + Sync>>`:
/// - On success: Contains a `HashMap` where the keys are machine names and the values are `Machine` structs representing detailed information about each MAME machine.
/// - On failure: Contains an error if the data file is not found, cannot be read, or if there are issues accessing the file system.
///
/// # Errors
/// This function will return an error if:
/// - The data file is not found in the expected location.
/// - The data file cannot be read due to permission issues or file corruption.
/// - There are errors in processing the data file content using the corresponding read function.
///
/// # Callback
/// The progress callback function provides real-time updates on the reading process and other status information. It receives:
/// - `progress`: The current progress of the operation (e.g., number of entries).
/// - `total`: The total number of items to be processed.
/// - `message`: A status message indicating the current operation (e.g., "Checking if data file is present", "Reading file").
/// — `callback_type`: The type of callback, such as `CallbackType::Info`, `CallbackType::Error`, `CallbackType::Progress`, or `CallbackType::Finish`.
///
/// # Example
///
/// Reads and processes all MAME data files available for the specified workspace path.
///
/// This function manages the concurrent reading of multiple MAME data files. For each `MameDataType`,
/// it spawns a separate thread to handle the file reading process. The function waits for all threads
/// to complete and then combines the results into a single `HashMap` of machine details. Progress updates
/// and messages are provided via a shared callback function.
///
/// # Parameters
/// - `workspace_path`: A reference to a `Path` representing the base directory where all data files are located.
/// - `progress_callback`: A shared callback function of type `SharedProgressCallback` that tracks progress and provides status updates.
/// The callback receives a `ProgressInfo` struct containing `progress`, `total`, `message`, and `callback_type`.
///
/// # Returns
/// Returns a `Result<HashMap<String, Machine>, Box<dyn Error + Send + Sync>>`:
/// - On success: Contains a `HashMap` where the keys are machine names and the values are `Machine` structs representing detailed information about each MAME machine.
/// - On failure: Contains an error if any data file cannot be read, or if there are issues joining the threads.
///
/// # Errors
/// This function will return an error if:
/// - Any thread fails to complete successfully or panics.
/// - There are issues reading any data file due to permission problems, file corruption, or missing files.
/// - There are errors during the merging of machine data, such as data inconsistencies.
///
/// # Concurrency
/// This function uses multiple threads to read MAME data files concurrently. Each thread handles the reading of a specific
/// data type file (`MameDataType`). The function waits for all threads to complete using `join()`, and any errors encountered
/// are captured and logged. The shared progress callback is used to provide real-time updates across all threads.
///
/// # Callback
/// The shared progress callback function provides real-time updates on the reading process for each data type and other status information. It receives:
/// - `progress`: The current progress of the operation for a specific data type (e.g., number of files processed).
/// - `total`: The total number of items to be processed (if available).
/// - `message`: A status message indicating the current operation (e.g., "Reading file", "Processing data").
/// - `callback_type`: The type of callback, such as `CallbackType::Info`, `CallbackType::Error`, `CallbackType::Progress`, or `CallbackType::Finish`.
///
/// # Example
///