use std::fmt;
use std::io;
use std::path::PathBuf;
use std::time::Duration;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum LockType {
Shared,
Exclusive,
}
impl fmt::Display for LockType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
LockType::Shared => write!(f, "shared"),
LockType::Exclusive => write!(f, "exclusive"),
}
}
}
#[derive(Debug)]
pub enum FileLockError {
Timeout {
path: PathBuf,
lock_type: LockType,
timeout: Duration,
},
Io(io::Error),
Poisoned {
path: PathBuf,
},
}
impl fmt::Display for FileLockError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
FileLockError::Timeout {
path,
lock_type,
timeout,
} => write!(
f,
"failed to acquire {} lock on '{}' within {:?}",
lock_type,
path.display(),
timeout,
),
FileLockError::Io(e) => write!(f, "I/O error: {}", e),
FileLockError::Poisoned { path } => write!(
f,
"lock on '{}' was poisoned by a panicking thread",
path.display()
),
}
}
}
impl std::error::Error for FileLockError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
FileLockError::Io(e) => Some(e),
_ => None,
}
}
}
impl From<io::Error> for FileLockError {
fn from(e: io::Error) -> Self {
FileLockError::Io(e)
}
}