Skip to content

Commit

Permalink
fix: displaydoc
Browse files Browse the repository at this point in the history
Uses `displaydoc` to derive error messages from doc comments to reduce
duplication.

Signed-off-by: Jonathan Woollett-Light <[email protected]>
  • Loading branch information
Jonathan Woollett-Light committed Sep 21, 2023
1 parent 3156667 commit bc40632
Show file tree
Hide file tree
Showing 82 changed files with 613 additions and 1,033 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/api_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ serde = { version = "1.0.136", features = ["derive"] }
serde_derive = "1.0.136"
serde_json = "1.0.78"
thiserror = "1.0.32"
displaydoc = "0.2.4"

logger = { path = "../logger" }
micro_http = { git = "https://github.com/firecracker-microvm/micro-http" }
Expand Down
1 change: 1 addition & 0 deletions src/cpu-template-helper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ bench = false

[dependencies]
clap = { version = "4.4.4", features = ["derive", "string"] }
displaydoc = "0.2.4"
libc = "0.2.148"
serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.107"
Expand Down
8 changes: 3 additions & 5 deletions src/cpu-template-helper/src/fingerprint/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ use serde::Serialize;

use crate::fingerprint::{Fingerprint, FingerprintField};

#[derive(Debug, thiserror::Error)]
#[derive(Debug, thiserror::Error, displaydoc::Display)]

Check warning on line 8 in src/cpu-template-helper/src/fingerprint/compare.rs

View check run for this annotation

Codecov / codecov/patch

src/cpu-template-helper/src/fingerprint/compare.rs#L8

Added line #L8 was not covered by tests
pub enum FingerprintCompareError {
/// Difference detected between source and target.
#[error("Difference detected between source and target:\n{0}")]
/// Difference detected between source and target:\n{0}
DiffDetected(String),
/// Failed to serialize/deserialize JSON.
#[error("Failed to serialize/deserialize JSON: {0}")]
/// Failed to serialize/deserialize JSON: {0}
Serde(#[from] serde_json::Error),
}

Expand Down
14 changes: 5 additions & 9 deletions src/cpu-template-helper/src/fingerprint/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,15 @@ use vmm::Vmm;

use crate::fingerprint::Fingerprint;

#[derive(Debug, thiserror::Error)]
#[derive(Debug, thiserror::Error, displaydoc::Display)]

Check warning on line 11 in src/cpu-template-helper/src/fingerprint/dump.rs

View check run for this annotation

Codecov / codecov/patch

src/cpu-template-helper/src/fingerprint/dump.rs#L11

Added line #L11 was not covered by tests
pub enum FingerprintDumpError {
/// Failed to dump CPU configuration.
#[error("Failed to dump CPU config: {0}")]
/// Failed to dump CPU config: {0}
DumpCpuConfig(#[from] crate::template::dump::DumpError),
/// Failed to read sysfs file.
#[error("Failed to read {0}: {1}")]
/// Failed to read {0}: {1}
ReadSysfsFile(String, std::io::Error),
/// Failed to get kernel version.
#[error("Failed to get kernel version: {0}")]
/// Failed to get kernel version: {0}
GetKernelVersion(std::io::Error),
/// Shell command failed.
#[error("`{0}` failed: {1}")]
/// `{0}` failed: {1}
ShellCommand(String, String),
}

Expand Down
20 changes: 10 additions & 10 deletions src/cpu-template-helper/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ mod fingerprint;
mod template;
mod utils;

#[derive(Debug, thiserror::Error)]
#[derive(Debug, thiserror::Error, displaydoc::Display)]

Check warning on line 16 in src/cpu-template-helper/src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/cpu-template-helper/src/main.rs#L16

Added line #L16 was not covered by tests
enum HelperError {
#[error("Failed to operate file: {0}")]
/// Failed to operate file: {0}
FileIo(#[from] std::io::Error),
#[error("{0}")]
/// {0}
FingerprintCompare(#[from] fingerprint::compare::FingerprintCompareError),
#[error("{0}")]
/// {0}
FingerprintDump(#[from] fingerprint::dump::FingerprintDumpError),
#[error("CPU template is not specified: {0}")]
/// CPU template is not specified: {0}
NoCpuTemplate(#[from] GetCpuTemplateError),
#[error("Failed to serialize/deserialize JSON file: {0}")]
/// Failed to serialize/deserialize JSON file: {0}
Serde(#[from] serde_json::Error),
#[error("{0}")]
/// {0}
Utils(#[from] UtilsError),
#[error("{0}")]
/// {0}
TemplateDump(#[from] template::dump::DumpError),
#[error("{0}")]
/// {0}
TemplateStrip(#[from] template::strip::StripError),
#[error("{0}")]
/// {0}
TemplateVerify(#[from] template::verify::VerifyError),
}

Expand Down
5 changes: 2 additions & 3 deletions src/cpu-template-helper/src/template/dump/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ use crate::template::dump::aarch64::config_to_template;
#[cfg(target_arch = "x86_64")]
use crate::template::dump::x86_64::config_to_template;

#[derive(Debug, thiserror::Error)]
#[derive(Debug, thiserror::Error, displaydoc::Display)]

Check warning on line 19 in src/cpu-template-helper/src/template/dump/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/cpu-template-helper/src/template/dump/mod.rs#L19

Added line #L19 was not covered by tests
pub enum DumpError {
/// Failed to dump CPU configuration.
#[error("Failed to dump CPU config: {0}")]
/// Failed to dump CPU config: {0}
DumpCpuConfig(#[from] DumpCpuConfigError),
}

Expand Down
3 changes: 1 addition & 2 deletions src/cpu-template-helper/src/template/strip/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ mod x86_64;
#[cfg(target_arch = "x86_64")]
pub use x86_64::strip;

#[derive(Debug, thiserror::Error)]
#[derive(Debug, thiserror::Error, displaydoc::Display)]

Check warning on line 21 in src/cpu-template-helper/src/template/strip/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/cpu-template-helper/src/template/strip/mod.rs#L21

Added line #L21 was not covered by tests
pub enum StripError {
/// The number of inputs should be two or more.
#[error("The number of inputs should be two or more.")]
NumberOfInputs,
}

Expand Down
7 changes: 4 additions & 3 deletions src/cpu-template-helper/src/template/verify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ mod x86_64;
pub use x86_64::verify;

#[rustfmt::skip]
#[derive(Debug, thiserror::Error)]
#[derive(Debug, thiserror::Error, displaydoc::Display)]
pub enum VerifyError {
#[error("{0} not found in CPU configuration.")]
/// {0} not found in CPU configuration.
KeyNotFound(String),
#[error("Value for {0} mismatched.\n{1}")]
#[rustfmt::skip]
#[doc = "Value for {0} mismatched.\n{1}"]
ValueMismatched(String, String),
}

Expand Down
8 changes: 3 additions & 5 deletions src/cpu-template-helper/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,11 @@ impl<V: Numeric> DiffString<V> for V {
}
}

#[derive(Debug, thiserror::Error)]
#[derive(Debug, thiserror::Error, displaydoc::Display)]

Check warning on line 61 in src/cpu-template-helper/src/utils/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/cpu-template-helper/src/utils/mod.rs#L61

Added line #L61 was not covered by tests
pub enum UtilsError {
/// Failed to create VmResources.
#[error("Failed to create VmResources: {0}")]
/// Failed to create VmResources: {0}
CreateVmResources(vmm::resources::ResourcesError),
/// Failed to build microVM.
#[error("Failed to build microVM: {0}")]
/// Failed to build microVM: {0}
BuildMicroVm(#[from] StartMicrovmError),
}

Expand Down
1 change: 1 addition & 0 deletions src/firecracker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ name = "firecracker"
bench = false

[dependencies]
displaydoc = "0.2.4"
event-manager = "0.3.0"
libc = "0.2.148"
serde_json = "1.0.107"
Expand Down
12 changes: 6 additions & 6 deletions src/firecracker/src/api_server_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ use vmm::rpc_interface::{
use vmm::vmm_config::instance_info::InstanceInfo;
use vmm::{EventManager, FcExitCode, Vmm};

#[derive(Debug, thiserror::Error)]
#[derive(Debug, thiserror::Error, displaydoc::Display)]

Check warning on line 23 in src/firecracker/src/api_server_adapter.rs

View check run for this annotation

Codecov / codecov/patch

src/firecracker/src/api_server_adapter.rs#L23

Added line #L23 was not covered by tests
pub enum ApiServerError {
#[error("MicroVMStopped without an error: {0:?}")]
/// MicroVMStopped without an error: {0:?}
MicroVMStoppedWithoutError(FcExitCode),
#[error("MicroVMStopped with an error: {0:?}")]
/// MicroVMStopped with an error: {0:?}
MicroVMStoppedWithError(FcExitCode),
#[error("Failed to open the API socket at: {0}. Check that it is not already used.")]
/// Failed to open the API socket at: {0}. Check that it is not already used.
FailedToBindSocket(String),
#[error("Failed to bind and run the HTTP server: {0}")]
/// Failed to bind and run the HTTP server: {0}
FailedToBindAndRunHttpServer(ServerError),
#[error("Failed to build MicroVM from Json: {0}")]
/// Failed to build MicroVM from Json: {0}
BuildFromJson(crate::BuildFromJsonError),
}

Expand Down
50 changes: 25 additions & 25 deletions src/firecracker/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,37 +39,37 @@ const DEFAULT_INSTANCE_ID: &str = "anonymous-instance";
const FIRECRACKER_VERSION: &str = env!("FIRECRACKER_VERSION");
const MMDS_CONTENT_ARG: &str = "metadata";

#[derive(Debug, thiserror::Error)]
#[derive(Debug, thiserror::Error, displaydoc::Display)]

Check warning on line 42 in src/firecracker/src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/firecracker/src/main.rs#L42

Added line #L42 was not covered by tests
enum MainError {
#[error("Failed to register signal handlers: {0}")]
/// Failed to register signal handlers: {0}
RegisterSignalHandlers(#[source] utils::errno::Error),
#[error("Arguments parsing error: {0} \n\nFor more information try --help.")]
/// Arguments parsing error: {0} \n\nFor more information try --help.
ParseArguments(#[from] utils::arg_parser::Error),
#[error("When printing Snapshot Data format: {0}")]
/// When printing Snapshot Data format: {0}
PrintSnapshotDataFormat(#[from] SnapshotVersionError),
#[error("Invalid value for logger level: {0}.Possible values: [Error, Warning, Info, Debug]")]
/// Invalid value for logger level: {0}.Possible values: [Error, Warning, Info, Debug]
InvalidLogLevel(LoggerConfigError),
#[error("Could not initialize logger: {0}")]
/// Could not initialize logger: {0}
LoggerInitialization(LoggerConfigError),
#[error("Could not initialize metrics: {0:?}")]
/// Could not initialize metrics: {0:?}
MetricsInitialization(MetricsConfigError),
#[error("Seccomp error: {0}")]
/// Seccomp error: {0}
SeccompFilter(FilterError),
#[error("Failed to resize fd table: {0}")]
/// Failed to resize fd table: {0}
ResizeFdtable(ResizeFdTableError),
#[error("RunWithApiError error: {0}")]
/// RunWithApiError error: {0}
RunWithApi(ApiServerError),
#[error("RunWithoutApiError error: {0}")]
/// RunWithoutApiError error: {0}
RunWithoutApiError(RunWithoutApiError),
}

#[derive(Debug, thiserror::Error)]
#[derive(Debug, thiserror::Error, displaydoc::Display)]

Check warning on line 66 in src/firecracker/src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/firecracker/src/main.rs#L66

Added line #L66 was not covered by tests
enum ResizeFdTableError {
#[error("Failed to get RLIMIT_NOFILE")]
/// Failed to get RLIMIT_NOFILE
GetRlimit,
#[error("Failed to call dup2 to resize fdtable")]
/// Failed to call dup2 to resize fdtable
Dup2(io::Error),
#[error("Failed to close dup2'd file descriptor")]
/// Failed to close dup2'd file descriptor
Close(io::Error),
}

Expand Down Expand Up @@ -524,13 +524,13 @@ fn print_supported_snapshot_versions() {
}
}

#[derive(Debug, thiserror::Error)]
#[derive(Debug, thiserror::Error, displaydoc::Display)]

Check warning on line 527 in src/firecracker/src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/firecracker/src/main.rs#L527

Added line #L527 was not covered by tests
enum SnapshotVersionError {
#[error("Unable to open snapshot state file: {0}")]
/// Unable to open snapshot state file: {0}
OpenSnapshot(io::Error),
#[error("Invalid data format version of snapshot file: {0}")]
/// Invalid data format version of snapshot file: {0}
SnapshotVersion(SnapshotError),
#[error("Cannot translate snapshot data version {0} to Firecracker microVM version")]
/// Cannot translate snapshot data version {0} to Firecracker microVM version
FirecrackerVersion(u16),
}

Expand All @@ -551,11 +551,11 @@ fn print_snapshot_data_format(snapshot_path: &str) -> Result<(), SnapshotVersion
Ok(())
}

#[derive(Debug, thiserror::Error)]
#[derive(Debug, thiserror::Error, displaydoc::Display)]

Check warning on line 554 in src/firecracker/src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/firecracker/src/main.rs#L554

Added line #L554 was not covered by tests
pub enum BuildFromJsonError {
#[error("Configuration for VMM from one single json failed: {0}")]
/// Configuration for VMM from one single json failed: {0}
ParseFromJson(vmm::resources::ResourcesError),
#[error("Could not Start MicroVM from one single json: {0}")]
/// Could not Start MicroVM from one single json: {0}
StartMicroVM(StartMicrovmError),
}

Expand Down Expand Up @@ -586,11 +586,11 @@ fn build_microvm_from_json(
Ok((vm_resources, vmm))
}

#[derive(Debug, thiserror::Error)]
#[derive(Debug, thiserror::Error, displaydoc::Display)]

Check warning on line 589 in src/firecracker/src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/firecracker/src/main.rs#L589

Added line #L589 was not covered by tests
enum RunWithoutApiError {
#[error("MicroVMStopped without an error: {0:?}")]
/// MicroVMStopped without an error: {0:?}
Shutdown(FcExitCode),
#[error("Failed to build MicroVM from Json: {0}")]
/// Failed to build MicroVM from Json: {0}
BuildMicroVMFromJson(BuildFromJsonError),
}

Expand Down
Loading

0 comments on commit bc40632

Please sign in to comment.