replace very confusing code with idiomatic rust

This commit is contained in:
ghe0 2025-01-18 16:13:38 +02:00
parent abf09e8d26
commit 1e893b49c0
Signed by: ghe0
GPG Key ID: 451028EE56A0FBB4

@ -361,14 +361,14 @@ pub struct VM {
storage_dir: String,
}
impl VM {
fn to_grpc_response<T>(self, build_resp: impl FnOnce(snp_proto::MeasurementArgs) -> T) -> T {
let mut nic_index: u32 = if self.fw_ports.is_empty() { 0 } else { 1 };
impl From<VM> for snp_proto::MeasurementArgs {
fn from(vm: VM) -> Self {
let mut nic_index: u32 = if vm.fw_ports.is_empty() { 0 } else { 1 };
let mut ips = Vec::new();
let mut dtrfs_api_endpoint = String::new();
// TODO: when brain supports multiple IPs per VM, fix this
for nic in self.nics {
for nic in vm.nics {
for ip in nic.ips {
if ip.address.parse::<std::net::Ipv4Addr>().is_ok() {
dtrfs_api_endpoint = ip.address.clone();
@ -386,39 +386,37 @@ impl VM {
if !dtrfs_api_endpoint.is_empty() {
dtrfs_api_endpoint += ":22";
} else {
dtrfs_api_endpoint += &format!("{}:{}", IP_INFO.ip, self.fw_ports[0].0);
dtrfs_api_endpoint += &format!("{}:{}", IP_INFO.ip, vm.fw_ports[0].0);
}
let args = snp_proto::MeasurementArgs {
snp_proto::MeasurementArgs {
dtrfs_api_endpoint,
exposed_ports: self.fw_ports.iter().map(|(host_port, _)| *host_port as u32).collect(),
exposed_ports: vm.fw_ports.iter().map(|(host_port, _)| *host_port as u32).collect(),
ips,
ovmf_hash: OVMF_HASH.to_string(),
};
build_resp(args)
}
}
}
impl Into<snp_proto::NewVmResp> for VM {
fn into(self) -> snp_proto::NewVmResp {
let uuid = self.uuid.clone();
self.to_grpc_response(|args| snp_proto::NewVmResp {
impl From<VM> for snp_proto::NewVmResp {
fn from(vm: VM) -> Self {
let uuid = vm.uuid.clone();
snp_proto::NewVmResp {
uuid,
args: Some(args),
args: Some(vm.into()),
error: "".to_string(),
})
}
}
}
impl Into<snp_proto::UpdateVmResp> for VM {
fn into(self) -> snp_proto::UpdateVmResp {
let uuid = self.uuid.clone();
self.to_grpc_response(|args| snp_proto::UpdateVmResp {
impl From<VM> for snp_proto::UpdateVmResp {
fn from(vm: VM) -> Self {
let uuid = vm.uuid.clone();
snp_proto::UpdateVmResp {
uuid,
args: Some(args),
args: Some(vm.into()),
error: "".to_string(),
})
}
}
}