src/handlers/handler.rs

Lines

100.00 %

Functions

100.00 %

Regions

100.00 %

LineCountSource (jump to first uncovered line)
1
//
2
// File Name:    handler.rs
3
// Directory:    src/handlers
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
//! # Handler
26
//!
27
28
// #![allow(unused)]
29
pub mod handler_trait;
30
31
use std::{fmt, hash::Hash, io::Error};
32
pub use handler_trait::HandlerTrait;
33
34
///
35
/// Available handlers.
36
///
37
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq)]
38
pub enum Handler {
39
    ///
40
    /// Refers to the `ConsoleHandler` => `ConsoleType::StdOut`.
41
    ///
42
    #[default]
43
    Console,
44
    ///
45
    /// Refers to the `ConsoleHandler` => `ConsoleType::StdErr`.
46
    ///
47
    EConsole,
48
    ///
49
    /// Refers to the `FileHandler`.
50
    ///
51
    File,
52
    ///
53
    /// Refers to the `ConsoleHandler` => `ConsoleType::Production`.
54
    ///
55
    PConsole,
56
    ///
57
    /// Refers to the `StringHandler`.
58
    ///
59
    String,
60
    ///
61
    /// Refers to a custom handler; by default: `MockHandler`.
62
    ///
63
    Custom(String),
64
}
65
66
impl fmt::Display for Handler {
6710
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6810
        let text = match &self {
692
            Handler::Console => "Console",
701
            Handler::EConsole => "EConsole",
712
            Handler::File => "File",
721
            Handler::PConsole => "PConsole",
732
            Handler::String => "String",
742
            Handler::Custom(label) => &format!("Custom({label})"),
75
        };
76
7710
        write!(f, "Handler::{text}")
7810
    }
79
}
80
81
#[cfg(test)]
82
mod test {
83
    use super::Handler;
84
85
    #[test]
861
    fn handlers() {
871
        let console = Handler::Console;
881
        let econsole = Handler::EConsole;
891
        let pconsole = Handler::PConsole;
901
        let file = Handler::File;
911
        let string = Handler::String;
921
        let custom = Handler::Custom("MyCustom".to_string());
93
941
        assert_eq!(console.to_string(), "Handler::Console".to_string());
951
        assert_eq!(econsole.to_string(), "Handler::EConsole".to_string());
961
        assert_eq!(pconsole.to_string(), "Handler::PConsole".to_string());
971
        assert_eq!(file.to_string(), "Handler::File".to_string());
981
        assert_eq!(string.to_string(), "Handler::String".to_string());
991
        assert_eq!(custom.to_string(), "Handler::Custom(MyCustom)".to_string());
1001
    }
101
}