From 1075209e2a48281ddb202d73e0163a06da43aa76 Mon Sep 17 00:00:00 2001 From: Bo Chen Date: Thu, 24 Jun 2021 06:29:42 -0700 Subject: [PATCH] vmm: Handle ApiRequest::VmCreate in a separate function It simplifies a bit the `Vmm::control_loop` and reads better to be consistent with other `ApiRequest` handlers. Also, it removes the repetitive `ApiError::VmAlreadyCreated` and makes `ApiError::VmCreate` useful. No functional changes. Signed-off-by: Bo Chen --- vmm/src/api/mod.rs | 3 --- vmm/src/lib.rs | 23 +++++++++++++++-------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/vmm/src/api/mod.rs b/vmm/src/api/mod.rs index 1eceafe01..0aeb694cd 100644 --- a/vmm/src/api/mod.rs +++ b/vmm/src/api/mod.rs @@ -64,9 +64,6 @@ pub enum ApiError { /// The VM could not boot. VmBoot(VmError), - /// The VM is already created. - VmAlreadyCreated, - /// The VM could not be created. VmCreate(VmError), diff --git a/vmm/src/lib.rs b/vmm/src/lib.rs index b54825dc5..6113066b4 100644 --- a/vmm/src/lib.rs +++ b/vmm/src/lib.rs @@ -343,6 +343,17 @@ impl Vmm { }) } + fn vm_create(&mut self, config: Arc>) -> result::Result<(), VmError> { + // We only store the passed VM config. + // The VM will be created when being asked to boot it. + if self.vm_config.is_none() { + self.vm_config = Some(config); + Ok(()) + } else { + Err(VmError::VmAlreadyCreated) + } + } + fn vm_boot(&mut self) -> result::Result<(), VmError> { // Create a new VM if we don't have one yet. if self.vm.is_none() { @@ -1142,14 +1153,10 @@ impl Vmm { info!("API request event: {:?}", api_request); match api_request { ApiRequest::VmCreate(config, sender) => { - // We only store the passed VM config. - // The VM will be created when being asked to boot it. - let response = if self.vm_config.is_none() { - self.vm_config = Some(config); - Ok(ApiResponsePayload::Empty) - } else { - Err(ApiError::VmAlreadyCreated) - }; + let response = self + .vm_create(config) + .map_err(ApiError::VmCreate) + .map(|_| ApiResponsePayload::Empty); sender.send(response).map_err(Error::ApiResponseSend)?; }