pub fn read_file(
data_type: MameDataType,
workspace_path: &Path,
progress_callback: ProgressCallback,
) -> Result<HashMap<String, Machine>, Box<dyn Error + Send + Sync>>
Expand description
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
: TheMameDataType
that specifies which type of MAME data file to read (e.g., ROMs, DAT files).workspace_path
: A reference to aPath
representing the base directory where the data file is located.progress_callback
: A callback function of typeProgressCallback
that tracks progress and provides status updates. The callback receives aProgressInfo
struct containingprogress
,total
,message
, andcallback_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 areMachine
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 asCallbackType::Info
,CallbackType::Error
,CallbackType::Progress
, orCallbackType::Finish
.
§Example
ⓘ
fn main() -> Result<(), Box<dyn Error>> {
// Define the workspace path
let workspace_path = Path::new("playground");
// Create a progress bar
let progress_bar = ProgressBar::new(100);
progress_bar.set_style(
ProgressStyle::default_bar()
.template("{spinner:.green} [{elapsed_precise}] [{bar:20.cyan/blue}] {pos}/{len} ({eta}) {msg}")
.progress_chars("#>-"),
);
// Create a progress callback
let progress_callback: ProgressCallback = Box::new(move |progress_info: ProgressInfo| {
// Update the progress bar
match progress_info.callback_type {
CallbackType::Progress => {
progress_bar.set_length(progress_info.total);
progress_bar.set_position(progress_info.progress);
}
CallbackType::Info => {
progress_bar.set_message(progress_info.message);
}
CallbackType::Finish => {
progress_bar.set_length(progress_info.total);
progress_bar.set_position(progress_info.progress);
progress_bar.finish_with_message(progress_info.message);
}
CallbackType::Error => {
progress_bar.finish_with_message(progress_info.message);
}
}
});
// Read the file
let machines = read_file(MameDataType::Catver, workspace_path, progress_callback);
// Print the result
match machines {
Ok(machines) => {
println!("Machines loaded: {}", machines.len());
}
Err(e) => {
eprintln!("Error reading data files: {}", e);
}
}
Ok(())
}