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
//! Conversions for [`LogLevel`].

use crate::xterm::LogLevel;

use log::Level;

impl From<Level> for LogLevel {
    fn from(level: Level) -> LogLevel {
        match level {
            Level::Trace | Level::Debug => LogLevel::Debug,
            Level::Info => LogLevel::Info,
            Level::Warn => LogLevel::Warn,
            Level::Error => LogLevel::Error,
        }
    }
}

impl From<Option<Level>> for LogLevel {
    fn from(level: Option<Level>) -> LogLevel {
        level.map_or(LogLevel::Off, Into::into)
    }
}

#[derive(Debug, Clone)]
#[allow(clippy::module_name_repetitions)]
/// Type indicating that a [`LogLevel`] to [`Level`] conversion failed because
/// the [`LogLevel`] was [`Off`](LogLevel::Off).
pub struct LogLevelIsOff;

impl std::convert::TryFrom<LogLevel> for Level {
    type Error = LogLevelIsOff;

    #[allow(clippy::match_wildcard_for_single_variants)]
    fn try_from(level: LogLevel) -> Result<Level, LogLevelIsOff> {
        match level {
            LogLevel::Debug => Ok(Level::Debug),
            LogLevel::Info => Ok(Level::Info),
            LogLevel::Warn => Ok(Level::Warn),
            LogLevel::Error => Ok(Level::Error),
            LogLevel::Off => Err(LogLevelIsOff),
            _ => unreachable!(),
        }
    }
}