Here's the context for the code:
#[derive(serde::Deserialize)]
#[serde(rename_all = "PascalCase")]
struct Instance {
//instance_lifecycle: String,
placement: Placement,
r#type: String,
tags: Vec<InstanceTag>,
instance_id: String,
}
let instances_list: InstancesList =
serde_json::from_str(&instances).expect("Json was not well formatted.");
for reservation in &instances_list.reservations{
for instance in &reservation.instances{
//if instance.life_cycle == "spot"
if instance["Lifecycle"] == "spot"{
continue;
}
Originally, I had "Lifecycle" as part of the Instance struct, but the file I'm parsing essentially looks like:
Reservations: {
Instances: {
a: .......
b:.......
c:........
d:........
}
Instances:{
a: .......
b:.......
c:........
d:........
Lifecycle:.....
}
}
And only one of the Instances has a "Lifecycle", and the other doesn't. So originally I had Lifecycle as part of the Instance struct, but that leads to a JSON parsing error since it doesn't appear in one of the Instances in the file(shown through comments in the code). As a result, I need to check if Lifecycle is in Instances so I tried:
if instance["Lifecycle"] == "spot"{
However, I get the following error:
error[E0608]: cannot index into a value of type &Instance
--> src/main.rs:116:16
|
116 | if instance["Lifecycle"] == "spot"{
| ^^^^^^^^^^^^^^^^^^^^^
Would appreciate any input. Thanks for your time and help.