I have a model that uses some based embedded structs, and I’m looking for the best way to retrieve values. I’m omitting a lot of the model for simplicity:
#[derive(Debug, Serialize, Deserialize)]
pub struct Devices {
#[serde(rename = "_id")]
pub id: Option<bson::oid::ObjectId>,
pub system: System,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct System {
pub id: Option<u32>,
}
Currently I’m using a match on the result to check for an error, but assuming that passes, I’m then using a match to get to the underlying optional id.
let device_id = match &result {
Ok(Some(device)) => match device.system.id {
Some(id) => id,
None => 0,
},
Ok(None) => 0,
Err(e) => panic!("DB error: {}", e),
};
As an example, I’m trying to get something like the following to work (I know the following doesn’t work) - yet.
pub fn get_device_id(model: &Devices) -> Option<u32> {
model.system.as_ref().and_then(|system| system.id)
}
Any thoughts would be most appreciated. Thanks