diff --git a/src/state.rs b/src/state.rs index d0ef295..9bbff8f 100644 --- a/src/state.rs +++ b/src/state.rs @@ -361,14 +361,14 @@ pub struct VM { storage_dir: String, } -impl VM { - fn to_grpc_response(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 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::().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 for VM { - fn into(self) -> snp_proto::NewVmResp { - let uuid = self.uuid.clone(); - self.to_grpc_response(|args| snp_proto::NewVmResp { +impl From 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 for VM { - fn into(self) -> snp_proto::UpdateVmResp { - let uuid = self.uuid.clone(); - self.to_grpc_response(|args| snp_proto::UpdateVmResp { +impl From 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(), - }) + } } }