Function read_file

Source
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: 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

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(())
}