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
use std::ffi::OsString;

/// InvalidLogFormat is an error returned on a LogFormat parse attempt when an
/// invalid logger format name is passed.
#[derive(Debug)]
pub struct InvalidLogFormat;

impl std::fmt::Display for InvalidLogFormat {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "invalid log format")
    }
}

impl std::error::Error for InvalidLogFormat {}

/// LogFormatFromEnvError captures all possible errors that can occur when
/// log format is constructred from the system environment variables.
#[derive(Debug)]
pub enum LogFormatFromEnvError {
    NotPresent,
    NotUnicode(OsString),
    InvalidFormat(String),
}

impl std::fmt::Display for LogFormatFromEnvError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            LogFormatFromEnvError::NotPresent => write!(f, "environment variable not found"),
            LogFormatFromEnvError::NotUnicode(s) => {
                write!(f, "environment variable was not valid unicode: {:?}", s)
            }
            LogFormatFromEnvError::InvalidFormat(s) => {
                write!(f, "environment variable was not a valid format name: {}", s)
            }
        }
    }
}

impl std::error::Error for LogFormatFromEnvError {}

/// LogFormatFromEnvWithDefaultError captures all possible errors that can occur
/// when log format is constructred from the system environment variables with
/// error cases handled by default assignment logic excluded.
#[derive(Debug)]
pub enum LogFormatFromEnvWithDefaultError {
    NotUnicode(OsString),
    InvalidFormat(String),
}

impl std::fmt::Display for LogFormatFromEnvWithDefaultError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            LogFormatFromEnvWithDefaultError::NotUnicode(s) => {
                write!(f, "environment variable was not valid unicode: {:?}", s)
            }
            LogFormatFromEnvWithDefaultError::InvalidFormat(s) => {
                write!(f, "environment variable was not a valid format name: {}", s)
            }
        }
    }
}

impl std::error::Error for LogFormatFromEnvWithDefaultError {}