Skip to content

Commit

Permalink
use TryFrom instead of From, as then a database edit cannot panic Nexus
Browse files Browse the repository at this point in the history
  • Loading branch information
jmpesp committed Oct 16, 2024
1 parent 412d665 commit 0a45763
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions nexus/db-model/src/volume_resource_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,31 +102,40 @@ impl VolumeResourceUsageRecord {
}
}

impl From<VolumeResourceUsageRecord> for VolumeResourceUsage {
fn from(record: VolumeResourceUsageRecord) -> VolumeResourceUsage {
impl TryFrom<VolumeResourceUsageRecord> for VolumeResourceUsage {
type Error = String;

fn try_from(
record: VolumeResourceUsageRecord,
) -> Result<VolumeResourceUsage, String> {
match record.usage_type {
VolumeResourceUsageType::ReadOnlyRegion => {
VolumeResourceUsage::ReadOnlyRegion {
region_id: record
.region_id
.expect("valid read-only region usage record"),
}
let Some(region_id) = record.region_id else {
return Err("valid read-only region usage record".into());
};

Ok(VolumeResourceUsage::ReadOnlyRegion { region_id })
}

VolumeResourceUsageType::RegionSnapshot => {
VolumeResourceUsage::RegionSnapshot {
dataset_id: record
.region_snapshot_dataset_id
.expect("valid region snapshot usage record"),

region_id: record
.region_snapshot_region_id
.expect("valid region snapshot usage record"),

snapshot_id: record
.region_snapshot_snapshot_id
.expect("valid region snapshot usage record"),
}
let Some(dataset_id) = record.region_snapshot_dataset_id else {
return Err("valid region snapshot usage record".into());
};

let Some(region_id) = record.region_snapshot_region_id else {
return Err("valid region snapshot usage record".into());
};

let Some(snapshot_id) = record.region_snapshot_snapshot_id
else {
return Err("valid region snapshot usage record".into());
};

Ok(VolumeResourceUsage::RegionSnapshot {
dataset_id,
region_id,
snapshot_id,
})
}
}
}
Expand Down

0 comments on commit 0a45763

Please sign in to comment.