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
use std::error::Error;
use std::path::PathBuf;
use std::sync::*;
#[derive(Debug)]
pub enum ConfigError {
ConfigNotFound(String),
ConfigRecursiveNotFound(String),
ConfigTypeMismatch(String, &'static str, &'static str),
ConfigParseError(String, String),
ConfigRecursiveError(String),
ConfigFileNotExists(PathBuf),
ConfigFileNotSupported(PathBuf),
RefValueRecursiveError,
ConfigCause(Box<dyn Error + 'static>),
}
impl<E: Error + 'static> From<E> for ConfigError {
fn from(e: E) -> Self {
ConfigError::ConfigCause(Box::new(e))
}
}
impl ConfigError {
pub(crate) fn try_lock_err<T>(v: TryLockError<T>) -> Self {
match v {
TryLockError::WouldBlock => Self::RefValueRecursiveError,
TryLockError::Poisoned(e) => Self::lock_err(e),
}
}
pub(crate) fn lock_err<T>(_e: PoisonError<T>) -> Self {
todo!()
}
}
pub(crate) trait ConfigLock<'a, T> {
fn lock_c(&'a self) -> Result<MutexGuard<'a, T>, ConfigError>;
fn try_lock_c(&'a self) -> Result<MutexGuard<'a, T>, ConfigError>;
}
impl<'a, T> ConfigLock<'a, T> for Mutex<T> {
fn lock_c(&'a self) -> Result<MutexGuard<'a, T>, ConfigError> {
self.lock().map_err(ConfigError::lock_err)
}
fn try_lock_c(&'a self) -> Result<MutexGuard<'a, T>, ConfigError> {
self.try_lock().map_err(ConfigError::try_lock_err)
}
}