diff --git a/src/main.rs b/src/main.rs index b4b7340..ffc23e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -65,6 +65,14 @@ fn main() -> Result<(), Box> { } }; + // Ensure `temp/` exists and prepare path for `antares.json`. + let temp_dir = "temp"; + let antares_file = format!("{}/antares.json", temp_dir); + fs::create_dir_all(temp_dir).map_err(|e| { + logger.log_error(&format!("Failed to create temp directory '{}': {}", temp_dir, e)); + Box::new(std::io::Error::new(std::io::ErrorKind::Other, e.to_string())) + })?; + // Try to deserialize into strongly-typed `Antares` list. let items: Antares = match serde_json::from_str::(&response) { Ok(items) => { @@ -76,8 +84,8 @@ fn main() -> Result<(), Box> { return Err(Box::new(e)); } }; - fs::write("antares.json", &pretty).map_err(|e| { - logger.log_error(&format!("Failed to write antares.json: {}", e)); + fs::write(&antares_file, &pretty).map_err(|e| { + logger.log_error(&format!("Failed to write {}: {}", antares_file, e)); Box::new(std::io::Error::new(std::io::ErrorKind::Other, e.to_string())) })?; logger.log(&format!("API call successful - Fetched {} items", items.len())); @@ -93,13 +101,16 @@ fn main() -> Result<(), Box> { return Err(Box::new(e)); } }; - fs::write("antares.json", &pretty).map_err(|e| { - logger.log_error(&format!("Failed to write antares.json: {}", e)); + fs::write(&antares_file, &pretty).map_err(|e| { + logger.log_error(&format!("Failed to write {}: {}", antares_file, e)); Box::new(std::io::Error::new(std::io::ErrorKind::Other, e.to_string())) })?; logger.log_info("Response saved as JSON (schema mismatch)"); } else { - fs::write("antares.json", &response)?; + fs::write(&antares_file, &response).map_err(|e| { + logger.log_error(&format!("Failed to write {}: {}", antares_file, e)); + Box::new(std::io::Error::new(std::io::ErrorKind::Other, e.to_string())) + })?; logger.log_info("Response saved as raw text (not valid JSON)"); } Vec::new()