From 3878e5c6ba99d238cd4e8c23df3b88914c19f374 Mon Sep 17 00:00:00 2001 From: Daniel Verkamp Date: Wed, 18 Aug 2021 14:24:53 -0700 Subject: [PATCH] usb_util: convert to ThisError and sort BUG=b:197143586 TEST=cargo check Change-Id: I747bd7889182addbe1781a6726f10f140c192d5e Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3105428 Tested-by: kokoro Commit-Queue: Daniel Verkamp Reviewed-by: Dennis Kempin --- Cargo.lock | 1 + usb_util/Cargo.toml | 1 + usb_util/src/error.rs | 39 +++++++++++++-------------------------- 3 files changed, 15 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9415aab596..23de9082c2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1215,6 +1215,7 @@ dependencies = [ "data_model", "libc", "remain", + "thiserror", "usb_sys", ] diff --git a/usb_util/Cargo.toml b/usb_util/Cargo.toml index c31f67a6f7..9be276ed12 100644 --- a/usb_util/Cargo.toml +++ b/usb_util/Cargo.toml @@ -9,5 +9,6 @@ assertions = { path = "../assertions" } data_model = { path = "../data_model" } libc = "*" remain = "*" +thiserror = "*" base = { path = "../base" } usb_sys = { path = "../usb_sys" } diff --git a/usb_util/src/error.rs b/usb_util/src/error.rs index 5b97c7e904..b24fccd36c 100644 --- a/usb_util/src/error.rs +++ b/usb_util/src/error.rs @@ -4,48 +4,35 @@ use base::IoctlNr; use remain::sorted; -use std::fmt::{self, Display}; use std::io; use std::num; +use thiserror::Error; #[sorted] -#[derive(Debug)] +#[derive(Error, Debug)] pub enum Error { + #[error("parsing descriptors failed")] DescriptorParse, + #[error("reading descriptors from device failed: {0}")] DescriptorRead(io::Error), + #[error("File::try_clone() failed: {0}")] FdCloneFailed(io::Error), + #[error("invalid actual_length in URB: {0}")] InvalidActualLength(num::TryFromIntError), + #[error("invalid transfer buffer length: {0}")] InvalidBufferLength(num::TryFromIntError), + #[error("USB ioctl 0x{0:x} failed: {1}")] IoctlFailed(IoctlNr, base::Error), + #[error("Device has been removed")] NoDevice, + #[error("Requested descriptor not found")] NoSuchDescriptor, + #[error("Rc::get_mut failed")] RcGetMutFailed, + #[error("Rc::try_unwrap failed")] RcUnwrapFailed, + #[error("attempted to cancel already-completed transfer")] TransferAlreadyCompleted, } -impl Display for Error { - #[remain::check] - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use self::Error::*; - - #[sorted] - match self { - DescriptorParse => write!(f, "parsing descriptors failed"), - DescriptorRead(e) => write!(f, "reading descriptors from device failed: {}", e), - FdCloneFailed(e) => write!(f, "File::try_clone() failed: {}", e), - InvalidActualLength(e) => write!(f, "invalid actual_length in URB: {}", e), - InvalidBufferLength(e) => write!(f, "invalid transfer buffer length: {}", e), - IoctlFailed(nr, e) => write!(f, "USB ioctl 0x{:x} failed: {}", nr, e), - NoDevice => write!(f, "Device has been removed"), - NoSuchDescriptor => write!(f, "Requested descriptor not found"), - RcGetMutFailed => write!(f, "Rc::get_mut failed"), - RcUnwrapFailed => write!(f, "Rc::try_unwrap failed"), - TransferAlreadyCompleted => write!(f, "attempted to cancel already-completed transfer"), - } - } -} - pub type Result = std::result::Result; - -impl std::error::Error for Error {}