summary refs log tree commit diff
path: root/usb_util/src/error.rs
blob: 409be9840c5a2d66732852e7fbbf7fca7668fe87 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2019 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use libc::c_ulong;
use remain::sorted;
use std::fmt::{self, Display};
use std::io;
use std::num;

#[sorted]
#[derive(Debug)]
pub enum Error {
    DescriptorParse,
    DescriptorRead(io::Error),
    FdCloneFailed(io::Error),
    InvalidActualLength(num::TryFromIntError),
    InvalidBufferLength(num::TryFromIntError),
    IoctlFailed(c_ulong, sys_util::Error),
    NoDevice,
    NoSuchDescriptor,
    RcGetMutFailed,
    RcUnwrapFailed,
    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<T> = std::result::Result<T, Error>;

impl std::error::Error for Error {}