• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use base::IoctlNr;
6 use remain::sorted;
7 use std::fmt::{self, Display};
8 use std::io;
9 use std::num;
10 
11 #[sorted]
12 #[derive(Debug)]
13 pub enum Error {
14     DescriptorParse,
15     DescriptorRead(io::Error),
16     FdCloneFailed(io::Error),
17     InvalidActualLength(num::TryFromIntError),
18     InvalidBufferLength(num::TryFromIntError),
19     IoctlFailed(IoctlNr, base::Error),
20     NoDevice,
21     NoSuchDescriptor,
22     RcGetMutFailed,
23     RcUnwrapFailed,
24     TransferAlreadyCompleted,
25 }
26 
27 impl Display for Error {
28     #[remain::check]
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result29     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
30         use self::Error::*;
31 
32         #[sorted]
33         match self {
34             DescriptorParse => write!(f, "parsing descriptors failed"),
35             DescriptorRead(e) => write!(f, "reading descriptors from device failed: {}", e),
36             FdCloneFailed(e) => write!(f, "File::try_clone() failed: {}", e),
37             InvalidActualLength(e) => write!(f, "invalid actual_length in URB: {}", e),
38             InvalidBufferLength(e) => write!(f, "invalid transfer buffer length: {}", e),
39             IoctlFailed(nr, e) => write!(f, "USB ioctl 0x{:x} failed: {}", nr, e),
40             NoDevice => write!(f, "Device has been removed"),
41             NoSuchDescriptor => write!(f, "Requested descriptor not found"),
42             RcGetMutFailed => write!(f, "Rc::get_mut failed"),
43             RcUnwrapFailed => write!(f, "Rc::try_unwrap failed"),
44             TransferAlreadyCompleted => write!(f, "attempted to cancel already-completed transfer"),
45         }
46     }
47 }
48 
49 pub type Result<T> = std::result::Result<T, Error>;
50 
51 impl std::error::Error for Error {}
52