src/handlers/console_handler/console_type.rs

Lines

100.00 %

Functions

100.00 %

Regions

100.00 %

LineCountSource (jump to first uncovered line)
1
//
2
// File Name:    console_type.rs
3
// Directory:    src/handlers/console_handler
4
// Project Name: flogging
5
//
6
// Copyright (C) 2025 Bradley Willcott
7
//
8
// SPDX-License-Identifier: GPL-3.0-or-later
9
//
10
// This library (crate) is free software: you can redistribute it and/or modify
11
// it under the terms of the GNU General Public License as published by
12
// the Free Software Foundation, either version 3 of the License, or
13
// (at your option) any later version.
14
//
15
// This library (crate) is distributed in the hope that it will be useful,
16
// but WITHOUT ANY WARRANTY; without even the implied warranty of
17
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
// GNU General Public License for more details.
19
//
20
// You should have received a copy of the GNU General Public License
21
// along with this library (crate).  If not, see <https://www.gnu.org/licenses/>.
22
//
23
24
//!
25
//! # ConsoleType
26
//!
27
28
use std::{fmt, str::FromStr};
29
use strum::{EnumIter, IntoEnumIterator};
30
31
///
32
/// `ConsoleType` configures the `ConsoleHandler`'s output.
33
///
34
#[derive(Debug, Default, EnumIter, PartialEq, Eq)]
35
pub enum ConsoleType {
36
    #[default]
37
    ///
38
    /// Prints to `stdout`.
39
    ///
40
    StdOut,
41
    ///
42
    /// Print to `stderr`.
43
    ///
44
    StdErr,
45
    ///
46
    /// If `log_entry.level` is `LeveL::INFO`, then
47
    /// prints unformatted `log_entry.msg` to `stdout`, else
48
    /// prints formatted `log_entry.msg` to `stderr`.
49
    ///
50
    Production,
51
}
52
53
impl ConsoleType {
54
    ///
55
    /// Converts a console type to its string version.
56
    ///
5737
    pub const fn as_str(&self) -> &'static str {
5837
        match self {
5923
            ConsoleType::StdOut => "stdout",
608
            ConsoleType::StdErr => "stderr",
616
            ConsoleType::Production => "production",
62
        }
6337
    }
64
}
65
66
impl fmt::Display for ConsoleType {
673
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
683
        self.as_str().fmt(f)
693
    }
70
}
71
72
///
73
/// Returned from `FromStr::from_str()` when an unknown string
74
/// is passed-in.
75
///
76
#[derive(Debug)]
77
pub struct ConsoleTypeError {
78
    #[allow(dead_code)]
79
    msg: String,
80
}
81
82
impl FromStr for ConsoleType {
83
    type Err = ConsoleTypeError;
84
8535
    fn from_str(s: &str) -> Result<Self, Self::Err> {
8635
        match s {
8722
            "stdout" => Ok(ConsoleType::StdOut),
887
            "stderr" => Ok(ConsoleType::StdErr),
895
            "production" => Ok(ConsoleType::Production),
901
            _ => Err(ConsoleTypeError {
911
                msg: format!("Unknown console type: {s}"),
921
            }),
93
        }
9435
    }
95
}
96
97
#[cfg(test)]
98
mod tests {
99
    use super::*;
100
    use std::io::{Result, Write};
101
102
    #[test]
1031
    fn as_str_to_from_str() {
1041
        for console_type in ConsoleType::iter() {
1053
            let label = console_type.as_str();
1063
            let console_type2 = ConsoleType::from_str(label).unwrap();
1073
            assert_eq!(console_type, console_type2);
108
        }
1091
    }
110
111
    #[test]
1121
    fn from_str_fail() {
1131
        let bad_label = "file";
1141
        assert!(ConsoleType::from_str(bad_label).is_err());
1151
    }
116
117
    #[test]
1181
    fn console_display() {
1191
        let expected = "stdout\nstderr\nproduction\n".to_string();
1201
        let mut buf = Vec::new();
121
1223
        for console_type in ConsoleType::iter() {
1233
            writeln!(&mut buf, "{console_type}").expect("writeln!() failed");
1243
        }
125
1261
        assert_eq!(expected, String::from_utf8(buf).unwrap());
1271
    }
128
}