Skip to content

Commit b309c9d

Browse files
jiangliualxiord
authored andcommitted
Normalize implementation of std::error::Error
Recently std::error::Error::description()/cause() have been softly deprecated in favor of Display and std::error::Error::source(). So change code to avoid using description(). All Error enumerations except cmdline::Error have implemented std::error::Error, so add the missing one. Fixes: #35 Signed-off-by: Liu Jiang <gerry@linux.alibaba.com>
1 parent ec930d7 commit b309c9d

File tree

10 files changed

+151
-156
lines changed

10 files changed

+151
-156
lines changed

coverage_config_x86_64.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"coverage_score": 83.4,
2+
"coverage_score": 80.8,
33
"exclude_path": "",
44
"crate_features": "bzimage,elf",
55
"exclude_path": "loader_gen"

src/cmdline/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ impl fmt::Display for Error {
3939
}
4040
}
4141

42+
impl std::error::Error for Error {}
43+
4244
/// Specialized [`Result`] type for command line operations.
4345
///
4446
/// [`Result`]: https://doc.rust-lang.org/std/result/enum.Result.html

src/configurator/aarch64/fdt.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
77
use vm_memory::{Bytes, GuestMemory};
88

9-
use std::error::Error as StdError;
109
use std::fmt;
1110

1211
use crate::configurator::{BootConfigurator, BootParams, Error as BootConfiguratorError, Result};
@@ -20,26 +19,20 @@ pub enum Error {
2019
WriteFDTToMemory,
2120
}
2221

23-
impl StdError for Error {
24-
fn description(&self) -> &str {
22+
impl fmt::Display for Error {
23+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2524
use Error::*;
26-
match self {
25+
let desc = match self {
2726
FDTPastRamEnd => "FDT does not fit in guest memory.",
28-
WriteFDTToMemory => "Error writing FDT in guest memory.",
29-
}
30-
}
31-
}
27+
WriteFDTToMemory => "error writing FDT in guest memory.",
28+
};
3229

33-
impl fmt::Display for Error {
34-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35-
write!(
36-
f,
37-
"Device Tree Boot Configurator Error: {}",
38-
StdError::description(self)
39-
)
30+
write!(f, "Device Tree Boot Configurator: {}", desc)
4031
}
4132
}
4233

34+
impl std::error::Error for Error {}
35+
4336
impl From<Error> for BootConfiguratorError {
4437
fn from(err: Error) -> Self {
4538
BootConfiguratorError::Fdt(err)
@@ -142,11 +135,11 @@ mod tests {
142135
fn test_error_messages() {
143136
assert_eq!(
144137
format!("{}", Error::FDTPastRamEnd),
145-
"Device Tree Boot Configurator Error: FDT does not fit in guest memory."
138+
"Device Tree Boot Configurator: FDT does not fit in guest memory."
146139
);
147140
assert_eq!(
148141
format!("{}", Error::WriteFDTToMemory),
149-
"Device Tree Boot Configurator Error: Error writing FDT in guest memory."
142+
"Device Tree Boot Configurator: error writing FDT in guest memory."
150143
);
151144
}
152145
}

src/configurator/mod.rs

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
1919
use vm_memory::{Address, ByteValued, GuestAddress, GuestMemory};
2020

21-
use std::error::Error as StdError;
2221
use std::fmt;
2322

2423
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
@@ -54,33 +53,43 @@ pub enum Error {
5453
InvalidAddress,
5554
}
5655

57-
impl StdError for Error {
58-
fn description(&self) -> &str {
56+
impl fmt::Display for Error {
57+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5958
use Error::*;
60-
match self {
59+
let desc = match self {
6160
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
62-
Linux(ref e) => e.description(),
61+
Linux(ref _e) => "failed to configure boot parameter by Linux Boot protocol.",
6362
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
64-
Pvh(ref e) => e.description(),
63+
Pvh(ref _e) => "failed to configure boot parameter by PVH.",
6564
#[cfg(target_arch = "aarch64")]
66-
Fdt(ref e) => e.description(),
65+
Fdt(ref _e) => "failed to configure boot parameter by FDT.",
6766

6867
MissingStartAddress => {
69-
"Boot parameter was specified without its starting address in guest memory."
68+
"boot parameter was specified without its starting address in guest memory."
7069
}
71-
Overflow => "Boot parameter address overflows.",
72-
InvalidAddress => "Boot parameter address precedes the starting address.",
73-
}
70+
Overflow => "boot parameter address overflows.",
71+
InvalidAddress => "boot parameter address precedes the starting address.",
72+
};
73+
74+
write!(f, "Boot Configurator: {}", desc)
7475
}
7576
}
7677

77-
impl fmt::Display for Error {
78-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
79-
write!(
80-
f,
81-
"Boot Configurator Error: {}",
82-
StdError::description(self)
83-
)
78+
impl std::error::Error for Error {
79+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
80+
use Error::*;
81+
match self {
82+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
83+
Linux(ref e) => Some(e),
84+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
85+
Pvh(ref e) => Some(e),
86+
#[cfg(target_arch = "aarch64")]
87+
Fdt(ref e) => Some(e),
88+
89+
MissingStartAddress => None,
90+
Overflow => None,
91+
InvalidAddress => None,
92+
}
8493
}
8594
}
8695

@@ -455,57 +464,55 @@ mod tests {
455464
// Linux
456465
assert_eq!(
457466
format!("{}", Error::Linux(linux::Error::ZeroPagePastRamEnd)),
458-
"Boot Configurator Error: The zero page extends past the end of guest memory."
467+
"Boot Configurator: failed to configure boot parameter by Linux Boot protocol."
459468
);
460469
assert_eq!(
461470
format!("{}", Error::Linux(linux::Error::ZeroPageSetup)),
462-
"Boot Configurator Error: Error writing to the zero page of guest memory."
471+
"Boot Configurator: failed to configure boot parameter by Linux Boot protocol."
463472
);
464473

465474
// PVH
466475
assert_eq!(
467476
format!("{}", Error::Pvh(pvh::Error::MemmapTableMissing)),
468-
"Boot Configurator Error: No memory map was passed to the boot configurator."
477+
"Boot Configurator: failed to configure boot parameter by PVH."
469478
);
470479
assert_eq!(
471480
format!("{}", Error::Pvh(pvh::Error::MemmapTablePastRamEnd)),
472-
"Boot Configurator Error: \
473-
The memory map table extends past the end of guest memory."
481+
"Boot Configurator: failed to configure boot parameter by PVH."
474482
);
475483
assert_eq!(
476484
format!("{}", Error::Pvh(pvh::Error::MemmapTableSetup)),
477-
"Boot Configurator Error: Error writing memory map table to guest memory."
485+
"Boot Configurator: failed to configure boot parameter by PVH."
478486
);
479487
assert_eq!(
480488
format!("{}", Error::Pvh(pvh::Error::StartInfoPastRamEnd)),
481-
"Boot Configurator Error: \
482-
The hvm_start_info structure extends past the end of guest memory."
489+
"Boot Configurator: failed to configure boot parameter by PVH."
483490
);
484491
assert_eq!(
485492
format!("{}", Error::Pvh(pvh::Error::StartInfoSetup)),
486-
"Boot Configurator Error: Error writing hvm_start_info to guest memory."
493+
"Boot Configurator: failed to configure boot parameter by PVH."
487494
);
488495
}
489496

490497
#[cfg(target_arch = "aarch64")]
491498
// FDT
492499
assert_eq!(
493500
format!("{}", Error::Fdt(fdt::Error::WriteFDTToMemory)),
494-
"Boot Configurator Error: Error writing FDT in guest memory."
501+
"Boot Configurator: failed to configure boot parameter by FDT."
495502
);
496503

497504
assert_eq!(
498505
format!("{}", Error::MissingStartAddress),
499-
"Boot Configurator Error: \
500-
Boot parameter was specified without its starting address in guest memory."
506+
"Boot Configurator: \
507+
boot parameter was specified without its starting address in guest memory."
501508
);
502509
assert_eq!(
503510
format!("{}", Error::Overflow),
504-
"Boot Configurator Error: Boot parameter address overflows."
511+
"Boot Configurator: boot parameter address overflows."
505512
);
506513
assert_eq!(
507514
format!("{}", Error::InvalidAddress),
508-
"Boot Configurator Error: Boot parameter address precedes the starting address."
515+
"Boot Configurator: boot parameter address precedes the starting address."
509516
);
510517
}
511518

src/configurator/x86_64/linux.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use vm_memory::{Bytes, GuestMemory};
1616

1717
use crate::configurator::{BootConfigurator, BootParams, Error as BootConfiguratorError, Result};
1818

19-
use std::error::Error as StdError;
2019
use std::fmt;
2120

2221
/// Boot configurator for the Linux boot protocol.
@@ -31,26 +30,20 @@ pub enum Error {
3130
ZeroPageSetup,
3231
}
3332

34-
impl StdError for Error {
35-
fn description(&self) -> &str {
36-
use Error::*;
37-
match self {
38-
ZeroPagePastRamEnd => "The zero page extends past the end of guest memory.",
39-
ZeroPageSetup => "Error writing to the zero page of guest memory.",
40-
}
41-
}
42-
}
43-
4433
impl fmt::Display for Error {
4534
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
46-
write!(
47-
f,
48-
"Linux Boot Configurator Error: {}",
49-
StdError::description(self)
50-
)
35+
use Error::*;
36+
let desc = match self {
37+
ZeroPagePastRamEnd => "the zero page extends past the end of guest memory.",
38+
ZeroPageSetup => "error writing to the zero page of guest memory.",
39+
};
40+
41+
write!(f, "Linux Boot Configurator: {}", desc,)
5142
}
5243
}
5344

45+
impl std::error::Error for Error {}
46+
5447
impl From<Error> for BootConfiguratorError {
5548
fn from(err: Error) -> Self {
5649
BootConfiguratorError::Linux(err)
@@ -183,11 +176,11 @@ mod tests {
183176
fn test_error_messages() {
184177
assert_eq!(
185178
format!("{}", Error::ZeroPagePastRamEnd),
186-
"Linux Boot Configurator Error: The zero page extends past the end of guest memory."
179+
"Linux Boot Configurator: the zero page extends past the end of guest memory."
187180
);
188181
assert_eq!(
189182
format!("{}", Error::ZeroPageSetup),
190-
"Linux Boot Configurator Error: Error writing to the zero page of guest memory."
183+
"Linux Boot Configurator: error writing to the zero page of guest memory."
191184
);
192185
}
193186
}

src/configurator/x86_64/pvh.rs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use vm_memory::{ByteValued, Bytes, GuestMemory};
1717
use crate::configurator::{BootConfigurator, BootParams, Error as BootConfiguratorError, Result};
1818
use crate::loader_gen::start_info::{hvm_memmap_table_entry, hvm_modlist_entry, hvm_start_info};
1919

20-
use std::error::Error as StdError;
2120
use std::fmt;
2221

2322
/// Boot configurator for the PVH boot protocol.
@@ -40,34 +39,28 @@ pub enum Error {
4039
StartInfoSetup,
4140
}
4241

43-
impl StdError for Error {
44-
fn description(&self) -> &str {
42+
impl fmt::Display for Error {
43+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4544
use Error::*;
46-
match self {
45+
let desc = match self {
4746
MemmapTableAddressMissing => {
48-
"The starting address for the memory map wasn't passed to the boot configurator."
47+
"the starting address for the memory map wasn't passed to the boot configurator."
4948
}
50-
MemmapTableMissing => "No memory map was passed to the boot configurator.",
51-
MemmapTablePastRamEnd => "The memory map table extends past the end of guest memory.",
52-
MemmapTableSetup => "Error writing memory map table to guest memory.",
49+
MemmapTableMissing => "no memory map was passed to the boot configurator.",
50+
MemmapTablePastRamEnd => "the memory map table extends past the end of guest memory.",
51+
MemmapTableSetup => "error writing memory map table to guest memory.",
5352
StartInfoPastRamEnd => {
54-
"The hvm_start_info structure extends past the end of guest memory."
53+
"the hvm_start_info structure extends past the end of guest memory."
5554
}
56-
StartInfoSetup => "Error writing hvm_start_info to guest memory.",
57-
}
58-
}
59-
}
55+
StartInfoSetup => "error writing hvm_start_info to guest memory.",
56+
};
6057

61-
impl fmt::Display for Error {
62-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63-
write!(
64-
f,
65-
"PVH Boot Configurator Error: {}",
66-
StdError::description(self)
67-
)
58+
write!(f, "PVH Boot Configurator: {}", desc)
6859
}
6960
}
7061

62+
impl std::error::Error for Error {}
63+
7164
impl From<Error> for BootConfiguratorError {
7265
fn from(err: Error) -> Self {
7366
BootConfiguratorError::Pvh(err)
@@ -262,17 +255,20 @@ mod tests {
262255
fn test_error_messages() {
263256
assert_eq!(
264257
format!("{}", Error::MemmapTableMissing),
265-
"PVH Boot Configurator Error: No memory map was passed to the boot configurator."
258+
"PVH Boot Configurator: no memory map was passed to the boot configurator."
259+
);
260+
assert_eq!(
261+
format!("{}", Error::MemmapTablePastRamEnd),
262+
"PVH Boot Configurator: the memory map table extends past the end of guest memory."
266263
);
267-
assert_eq!(format!("{}", Error::MemmapTablePastRamEnd), "PVH Boot Configurator Error: The memory map table extends past the end of guest memory.");
268264
assert_eq!(
269265
format!("{}", Error::MemmapTableSetup),
270-
"PVH Boot Configurator Error: Error writing memory map table to guest memory."
266+
"PVH Boot Configurator: error writing memory map table to guest memory."
271267
);
272-
assert_eq!(format!("{}", Error::StartInfoPastRamEnd), "PVH Boot Configurator Error: The hvm_start_info structure extends past the end of guest memory.");
268+
assert_eq!(format!("{}", Error::StartInfoPastRamEnd), "PVH Boot Configurator: the hvm_start_info structure extends past the end of guest memory.");
273269
assert_eq!(
274270
format!("{}", Error::StartInfoSetup),
275-
"PVH Boot Configurator Error: Error writing hvm_start_info to guest memory."
271+
"PVH Boot Configurator: error writing hvm_start_info to guest memory."
276272
);
277273
}
278274
}

0 commit comments

Comments
 (0)