Added proper error handling to extract_memory_method

This commit is contained in:
François Autin 2023-03-10 10:32:20 +01:00
parent 187614d49e
commit 5155b62b06
No known key found for this signature in database
GPG Key ID: 343F5D382E1DD77C

View File

@ -150,8 +150,16 @@ impl Machine {
/// ///
/// - **machine** contains the memory /// - **machine** contains the memory
pub fn extract_memory(machine: &mut Machine){ pub fn extract_memory(machine: &mut Machine){
let mut file = File::create("burritos_memory.txt").unwrap(); let file_path = "burritos_memory.txt";
file.write(&machine.main_memory); let write_to_file = |path| -> std::io::Result<File> {
let mut file = File::create(path)?;
file.write_all(&machine.main_memory)?;
Ok(file)
};
match write_to_file(file_path) {
Err(e) => eprintln!("Failed to write memory to file: {}", e),
Ok(_) => println!("Memory extracted to {}", file_path)
};
} }
pub fn print_machine_status(machine: &mut Machine) { pub fn print_machine_status(machine: &mut Machine) {