From 4a1cbbd06de7713f28ef204225418b1a6238bc0c Mon Sep 17 00:00:00 2001 From: czybjtu Date: Thu, 1 Jun 2023 17:29:35 +0800 Subject: [PATCH] fix: use thiserror::Error in vmm_config vmm: use thiserror::Error Signed-off-by: czybjtu --- src/vmm/src/vmm_config/boot_source.rs | 23 ++-------- src/vmm/src/vmm_config/logger.rs | 26 +----------- src/vmm/src/vmm_config/metrics.rs | 26 +----------- src/vmm/src/vmm_config/mmds.rs | 42 ++++--------------- .../integration_tests/build/test_coverage.py | 4 +- 5 files changed, 18 insertions(+), 103 deletions(-) diff --git a/src/vmm/src/vmm_config/boot_source.rs b/src/vmm/src/vmm_config/boot_source.rs index cd4bf806527..217f3b8f533 100644 --- a/src/vmm/src/vmm_config/boot_source.rs +++ b/src/vmm/src/vmm_config/boot_source.rs @@ -1,7 +1,6 @@ // Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -use std::fmt::{Display, Formatter, Result}; use std::fs::File; use std::io; @@ -38,33 +37,19 @@ pub struct BootSourceConfig { } /// Errors associated with actions on `BootSourceConfig`. -#[derive(Debug)] +#[derive(Debug, thiserror::Error)] pub enum BootSourceConfigError { /// The kernel file cannot be opened. + #[error("The kernel file cannot be opened: {0}")] InvalidKernelPath(io::Error), /// The initrd file cannot be opened. + #[error("The initrd file cannot be opened due to invalid path or invalid permissions. {0}")] InvalidInitrdPath(io::Error), /// The kernel command line is invalid. + #[error("The kernel command line is invalid: {0}")] InvalidKernelCommandLine(String), } -impl Display for BootSourceConfigError { - fn fmt(&self, f: &mut Formatter) -> Result { - use self::BootSourceConfigError::*; - match *self { - InvalidKernelPath(ref err) => write!(f, "The kernel file cannot be opened: {}", err), - InvalidInitrdPath(ref err) => write!( - f, - "The initrd file cannot be opened due to invalid path or invalid permissions. {}", - err, - ), - InvalidKernelCommandLine(ref err) => { - write!(f, "The kernel command line is invalid: {}", err.as_str()) - } - } - } -} - /// Holds the kernel specification (both configuration as well as runtime details). #[derive(Default)] pub struct BootSource { diff --git a/src/vmm/src/vmm_config/logger.rs b/src/vmm/src/vmm_config/logger.rs index dd7c3a7ae3b..168d17a0943 100644 --- a/src/vmm/src/vmm_config/logger.rs +++ b/src/vmm/src/vmm_config/logger.rs @@ -2,7 +2,6 @@ // SPDX-License-Identifier: Apache-2.0 //! Auxiliary module for configuring the logger. -use std::fmt::{Display, Formatter}; use std::path::PathBuf; use logger::{LevelFilter, LOGGER}; @@ -106,21 +105,13 @@ impl LoggerConfig { } /// Errors associated with actions on the `LoggerConfig`. -#[derive(Debug)] +#[derive(Debug, thiserror::Error)] pub enum LoggerConfigError { /// Cannot initialize the logger due to bad user input. + #[error("{}", format!("{:?}", .0).replace('\"', ""))] InitializationFailure(String), } -impl Display for LoggerConfigError { - fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { - use self::LoggerConfigError::*; - match *self { - InitializationFailure(ref err_msg) => write!(f, "{}", err_msg.replace('\"', "")), - } - } -} - /// Configures the logger as described in `logger_cfg`. pub fn init_logger( logger_cfg: LoggerConfig, @@ -215,19 +206,6 @@ mod tests { } } - #[test] - fn test_error_display() { - assert_eq!( - format!( - "{}", - LoggerConfigError::InitializationFailure(String::from( - "Failed to initialize logger" - )) - ), - "Failed to initialize logger" - ); - } - #[test] fn test_new_logger_config() { let logger_config = diff --git a/src/vmm/src/vmm_config/metrics.rs b/src/vmm/src/vmm_config/metrics.rs index 8978ab6aa83..9cbc0d554b7 100644 --- a/src/vmm/src/vmm_config/metrics.rs +++ b/src/vmm/src/vmm_config/metrics.rs @@ -2,7 +2,6 @@ // SPDX-License-Identifier: Apache-2.0 //! Auxiliary module for configuring the metrics system. -use std::fmt::{Display, Formatter}; use std::path::PathBuf; use logger::METRICS; @@ -18,21 +17,13 @@ pub struct MetricsConfig { } /// Errors associated with actions on the `MetricsConfig`. -#[derive(Debug)] +#[derive(Debug, thiserror::Error)] pub enum MetricsConfigError { /// Cannot initialize the metrics system due to bad user input. + #[error("{}", format!("{:?}", .0).replace('\"', ""))] InitializationFailure(String), } -impl Display for MetricsConfigError { - fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { - use self::MetricsConfigError::*; - match *self { - InitializationFailure(ref err_msg) => write!(f, "{}", err_msg.replace('\"', "")), - } - } -} - /// Configures the metrics as described in `metrics_cfg`. pub fn init_metrics(metrics_cfg: MetricsConfig) -> std::result::Result<(), MetricsConfigError> { let writer = FcLineWriter::new( @@ -67,17 +58,4 @@ mod tests { assert!(init_metrics(desc.clone()).is_ok()); assert!(init_metrics(desc).is_err()); } - - #[test] - fn test_error_display() { - assert_eq!( - format!( - "{}", - MetricsConfigError::InitializationFailure(String::from( - "Failed to initialize metrics" - )) - ), - "Failed to initialize metrics" - ); - } } diff --git a/src/vmm/src/vmm_config/mmds.rs b/src/vmm/src/vmm_config/mmds.rs index 2fdc9d14aae..d2c0a6f4619 100644 --- a/src/vmm/src/vmm_config/mmds.rs +++ b/src/vmm/src/vmm_config/mmds.rs @@ -1,7 +1,5 @@ // Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 - -use std::fmt::{Display, Formatter, Result}; use std::net::Ipv4Addr; use mmds::data_store; @@ -40,46 +38,22 @@ impl MmdsConfig { } /// MMDS configuration related errors. -#[derive(Debug)] +#[derive(Debug, thiserror::Error)] pub enum MmdsConfigError { /// The network interfaces list provided is empty. + #[error("The list of network interface IDs that allow forwarding MMDS requests is empty.")] EmptyNetworkIfaceList, /// The provided IPv4 address is not link-local valid. + #[error("The MMDS IPv4 address is not link local.")] InvalidIpv4Addr, /// The network interfaces list provided contains IDs that /// does not correspond to any existing network interface. + #[error( + "The list of network interface IDs provided contains at least one ID that does not \ + correspond to any existing network interface." + )] InvalidNetworkInterfaceId, /// MMDS version could not be configured. + #[error("The MMDS could not be configured to version {0}: {1}")] MmdsVersion(MmdsVersion, data_store::Error), } - -impl Display for MmdsConfigError { - fn fmt(&self, f: &mut Formatter<'_>) -> Result { - match self { - MmdsConfigError::EmptyNetworkIfaceList => { - write!( - f, - "The list of network interface IDs that allow forwarding MMDS requests is \ - empty." - ) - } - MmdsConfigError::InvalidIpv4Addr => { - write!(f, "The MMDS IPv4 address is not link local.") - } - MmdsConfigError::InvalidNetworkInterfaceId => { - write!( - f, - "The list of network interface IDs provided contains at least one ID that \ - does not correspond to any existing network interface." - ) - } - MmdsConfigError::MmdsVersion(version, err) => { - write!( - f, - "The MMDS could not be configured to version {}: {}", - version, err - ) - } - } - } -} diff --git a/tests/integration_tests/build/test_coverage.py b/tests/integration_tests/build/test_coverage.py index c220f9f95c9..0430709562f 100644 --- a/tests/integration_tests/build/test_coverage.py +++ b/tests/integration_tests/build/test_coverage.py @@ -25,9 +25,9 @@ def is_on_skylake(): # Checkout the cpuid crate. In the future other # differences may appear. if utils.is_io_uring_supported(): - COVERAGE_DICT = {"Intel": 83.66, "AMD": 83.24, "ARM": 83.06} + COVERAGE_DICT = {"Intel": 83.76, "AMD": 83.34, "ARM": 83.12} else: - COVERAGE_DICT = {"Intel": 80.90, "AMD": 80.45, "ARM": 80.06} + COVERAGE_DICT = {"Intel": 81.02, "AMD": 80.55, "ARM": 80.12} PROC_MODEL = proc.proc_type()