arch: serial: Use thiserror and sorted for Error enum

BUG=none
TEST=cargo check

Change-Id: Ib8c07eb54af730c7a0ffaab67c02d6fb14a7efa5
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3096438
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Keiichi Watanabe <keiichiw@chromium.org>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
This commit is contained in:
Keiichi Watanabe 2021-08-16 17:36:10 +09:00 committed by Commit Bot
parent 9568bb44b8
commit 407b320fea
4 changed files with 35 additions and 47 deletions

1
Cargo.lock generated
View file

@ -54,6 +54,7 @@ dependencies = [
"libc",
"minijail",
"power_monitor",
"remain",
"resources",
"sync",
"thiserror",

View file

@ -10,16 +10,17 @@ gdb = ["gdbstub_arch"]
[dependencies]
acpi_tables = { path = "../acpi_tables" }
base = { path = "../base" }
devices = { path = "../devices" }
gdbstub_arch = { version = "0.1.0", optional = true }
hypervisor = { path = "../hypervisor" }
kernel_cmdline = { path = "../kernel_cmdline" }
libc = "*"
minijail = "*"
power_monitor = { path = "../power_monitor" }
remain = "*"
resources = { path = "../resources" }
sync = { path = "../sync" }
base = { path = "../base" }
thiserror = "1.0.20"
vm_control = { path = "../vm_control" }
vm_memory = { path = "../vm_memory" }
power_monitor = { path = "../power_monitor" }
thiserror = "1.0.20"

View file

@ -11,28 +11,30 @@ use std::ffi::CString;
use std::io;
use std::mem::size_of;
use remain::sorted;
use thiserror::Error as ThisError;
#[sorted]
#[derive(ThisError, Debug)]
pub enum Error {
#[error("Parse error reading FDT parameters")]
FdtFileParseError,
#[error("Error writing FDT to guest memory")]
FdtGuestMemoryWriteError,
#[error("I/O error reading FDT parameters code={0}")]
FdtIoError(io::Error),
#[error("Strings cannot contain NUL")]
InvalidString,
#[error("Attempted to end a node that was not the most recent")]
OutOfOrderEndNode,
#[error("Properties may not be added after a node has been ended")]
PropertyAfterEndNode,
#[error("Property value size must fit in 32 bits")]
PropertyValueTooLarge,
#[error("Total size must fit in 32 bits")]
TotalSizeTooLarge,
#[error("Strings cannot contain NUL")]
InvalidString,
#[error("Attempted to end a node that was not the most recent")]
OutOfOrderEndNode,
#[error("Attempted to call finish without ending all nodes")]
UnclosedNode,
#[error("Error writing FDT to guest memory")]
FdtGuestMemoryWriteError,
#[error("Parse error reading FDT parameters")]
FdtFileParseError,
#[error("I/O error reading FDT parameters code={0}")]
FdtIoError(io::Error),
}
pub type Result<T> = std::result::Result<T, Error>;

View file

@ -17,39 +17,33 @@ use std::time::Duration;
use base::{error, info, read_raw_stdin, syslog, AsRawDescriptor, Event, RawDescriptor};
use devices::{Bus, ProtectionType, ProxyDevice, Serial, SerialDevice};
use minijail::Minijail;
use remain::sorted;
use sync::Mutex;
use thiserror::Error as ThisError;
use crate::DeviceRegistrationError;
#[derive(Debug)]
#[sorted]
#[derive(ThisError, Debug)]
pub enum Error {
#[error("Unable to clone an Event: {0}")]
CloneEvent(base::Error),
#[error("Unable to open/create file: {0}")]
FileError(std::io::Error),
InvalidSerialHardware(String),
InvalidSerialType(String),
#[error("Serial device path is invalid")]
InvalidPath,
#[error("Invalid serial hardware: {0}")]
InvalidSerialHardware(String),
#[error("Invalid serial type: {0}")]
InvalidSerialType(String),
#[error("Serial device type file requires a path")]
PathRequired,
#[error("Failed to create unbound socket")]
SocketCreateFailed,
#[error("Serial device type {0} not implemented")]
Unimplemented(SerialType),
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::Error::*;
match self {
CloneEvent(e) => write!(f, "unable to clone an Event: {}", e),
FileError(e) => write!(f, "unable to open/create file: {}", e),
InvalidSerialHardware(e) => write!(f, "invalid serial hardware: {}", e),
InvalidSerialType(e) => write!(f, "invalid serial type: {}", e),
InvalidPath => write!(f, "serial device path is invalid"),
PathRequired => write!(f, "serial device type file requires a path"),
SocketCreateFailed => write!(f, "failed to create unbound socket"),
Unimplemented(e) => write!(f, "serial device type {} not implemented", e.to_string()),
}
}
}
/// Enum for possible type of serial devices
#[derive(Clone, Debug)]
pub enum SerialType {
@ -462,25 +456,15 @@ pub fn add_serial_devices(
Ok(())
}
#[derive(Debug)]
#[sorted]
#[derive(ThisError, Debug)]
pub enum GetSerialCmdlineError {
#[error("Error appending to cmdline: {0}")]
KernelCmdline(kernel_cmdline::Error),
#[error("Hardware {0} not supported as earlycon")]
UnsupportedEarlyconHardware(SerialHardware),
}
impl Display for GetSerialCmdlineError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::GetSerialCmdlineError::*;
match self {
KernelCmdline(e) => write!(f, "error appending to cmdline: {}", e),
UnsupportedEarlyconHardware(hw) => {
write!(f, "hardware {} not supported as earlycon", hw)
}
}
}
}
pub type GetSerialCmdlineResult<T> = std::result::Result<T, GetSerialCmdlineError>;
/// Add serial options to the provided `cmdline` based on `serial_parameters`.