Update main.rs

This commit is contained in:
Villers Krisztián 2026-05-04 17:02:11 +02:00
parent 5ed65713d3
commit 1b3dd5c37e
1 changed files with 16 additions and 5 deletions

View File

@ -65,6 +65,14 @@ fn main() -> Result<(), Box<dyn Error>> {
} }
}; };
// 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. // Try to deserialize into strongly-typed `Antares` list.
let items: Antares = match serde_json::from_str::<Antares>(&response) { let items: Antares = match serde_json::from_str::<Antares>(&response) {
Ok(items) => { Ok(items) => {
@ -76,8 +84,8 @@ fn main() -> Result<(), Box<dyn Error>> {
return Err(Box::new(e)); return Err(Box::new(e));
} }
}; };
fs::write("antares.json", &pretty).map_err(|e| { fs::write(&antares_file, &pretty).map_err(|e| {
logger.log_error(&format!("Failed to write antares.json: {}", e)); logger.log_error(&format!("Failed to write {}: {}", antares_file, e));
Box::new(std::io::Error::new(std::io::ErrorKind::Other, e.to_string())) Box::new(std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))
})?; })?;
logger.log(&format!("API call successful - Fetched {} items", items.len())); logger.log(&format!("API call successful - Fetched {} items", items.len()));
@ -93,13 +101,16 @@ fn main() -> Result<(), Box<dyn Error>> {
return Err(Box::new(e)); return Err(Box::new(e));
} }
}; };
fs::write("antares.json", &pretty).map_err(|e| { fs::write(&antares_file, &pretty).map_err(|e| {
logger.log_error(&format!("Failed to write antares.json: {}", e)); logger.log_error(&format!("Failed to write {}: {}", antares_file, e));
Box::new(std::io::Error::new(std::io::ErrorKind::Other, e.to_string())) Box::new(std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))
})?; })?;
logger.log_info("Response saved as JSON (schema mismatch)"); logger.log_info("Response saved as JSON (schema mismatch)");
} else { } 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)"); logger.log_info("Response saved as raw text (not valid JSON)");
} }
Vec::new() Vec::new()