Update main.rs
This commit is contained in:
parent
5ed65713d3
commit
1b3dd5c37e
21
src/main.rs
21
src/main.rs
|
|
@ -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.
|
||||
let items: Antares = match serde_json::from_str::<Antares>(&response) {
|
||||
Ok(items) => {
|
||||
|
|
@ -76,8 +84,8 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||
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<dyn Error>> {
|
|||
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()
|
||||
|
|
|
|||
Loading…
Reference in New Issue