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
70
71
72
73
74
use std::io;
use thiserror::Error;

/// SimpleLock's Result value override to inject our Error type.
pub type SimpleLockResult<T> = std::result::Result<T, SimpleLockError>;

/// SimpleLock's Error type.
#[derive(Debug, Error, PartialEq, Eq, PartialOrd, Ord)]
pub enum SimpleLockError {
    /// The Lock was already taken by another process, we cannot lock.
    ///
    /// This will only be returned if the [`LockConfig`] provided explicitly
    /// disables `hang_lock`. In other-words, if we want to fail immediately
    /// on the inability to lock.
    ///
    /// [`LockConfig`]: ./struct.LockConfig.html
    #[error("Lock is already locked!")]
    AlreadyLocked,

    /// The name given on Lock creation (either with [`default_lock`]
    /// or an implementation's constructor) is invalid.
    ///
    /// This can be due to OS path name restrictions or being invalid unicode, etc.
    ///
    /// [`default_lock`]: ./fn.default_lock.html
    #[error("Path or Name for lock was invalid!")]
    InvalidName,

    /// We failed to unlock the Lock.
    ///
    /// This can be due to a number of issues. Using [`FileLock`] as an example,
    /// if the temporary file disappears due to an unreliable OS File-Cleaner,
    /// its permissions change out-from-under-us, etc.
    ///
    /// [`FileLock`]: ./struct.FileLock.html
    #[error("Unable to unlock, reason: {0}")]
    UnableToUnlock(String),

    /// The File Descriptor was created with permissions you do not currently have.
    ///
    /// This may happen if the program was executed as a super-user and failed to clean up
    /// open files. If not using the provided utility functionality, please revisit them to
    /// see if any fit your needs. They may handle an edge-case your application hits.
    #[error("Permission Denied!")]
    PermissionDenied,

    /// Thrown when calling [`default_lock`] and no feature flag was enabled.
    ///
    /// [`default_lock`]: ./fn.default_lock.html
    #[error("No lock implementation was enabled, unable to create a default lock.")]
    NoLockEnabled,

    /// An unexpected error from one of the Lock implementations.
    /// If this happens, please file a ticket with us as we would like to
    /// harden this library over time.
    #[error("Unknown Error: {0:?}")]
    UnknownError(String),
}

impl From<io::Error> for SimpleLockError {
    fn from(other: io::Error) -> Self {
        other.kind().into()
    }
}

impl From<io::ErrorKind> for SimpleLockError {
    fn from(other: io::ErrorKind) -> Self {
        match other {
            io::ErrorKind::NotFound => SimpleLockError::InvalidName,
            io::ErrorKind::PermissionDenied => SimpleLockError::PermissionDenied,
            _ => SimpleLockError::UnknownError(format!("{:?}", other)),
        }
    }
}