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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//!
//! Error types
//!

use nix::errno::Errno;
use std::error::Error;
use std::fmt;

/// A general system error that can be returned by any DRM command.
///
/// Receiving this error likely indicates a bug in either the program, this
/// crate, or the underlying operating system.
#[derive(Debug)]
pub enum SystemError {
    /// A command was attempted using an invalid file descriptor.
    InvalidFileDescriptor,

    /// Provided memory area is inaccessible.
    ///
    /// Receiving this error indicates a bug in this crate.
    MemoryFault,

    /// One or more arguments used are invalid.
    ///
    /// This can be due to the system not supporting a feature or value.
    InvalidArgument,

    /// A command was attempted using a non-DRM device.
    InvalidFileType,

    /// Permission denied.
    PermissionDenied,

    /// Unknown system error.
    Unknown {
        /// Unknown [`nix::errno::Errno`] returned by the system call.
        errno: Errno,
    },
}

impl fmt::Display for SystemError {
    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> fmt::Result {
        fmt.write_str(match self {
            SystemError::InvalidFileDescriptor => "invalid file descriptor",
            SystemError::MemoryFault => "invalid memory access",
            SystemError::InvalidArgument => "invalid argument",
            SystemError::InvalidFileType => "invalid file type",
            SystemError::PermissionDenied => "permission denied",
            SystemError::Unknown { errno } => {
                return write!(fmt, "unknown system error: {}", errno)
            }
        })
    }
}

impl Error for SystemError {}

impl From<Errno> for SystemError {
    fn from(errno: Errno) -> SystemError {
        match errno {
            Errno::EBADF => SystemError::InvalidFileDescriptor,
            Errno::EFAULT => SystemError::MemoryFault,
            Errno::EINVAL => SystemError::InvalidArgument,
            Errno::ENOTTY => SystemError::InvalidFileDescriptor,
            Errno::EACCES => SystemError::PermissionDenied,
            _ => SystemError::Unknown { errno },
        }
    }
}