src/handlers/formatters/formatter.rs

Lines

100.00 %

Functions

100.00 %

Regions

100.00 %

LineCountSource (jump to first uncovered line)
1
//
2
// File Name:    formatter.rs
3
// Directory:    src/handlers/formatters
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
//! # Formatter
26
//!
27
28
use std::fmt;
29
use super::*;
30
31
///
32
/// Provides wrappers for holding each type of formatter.
33
///
34
#[derive(Debug, Clone)]
35
pub enum Formatter {
36
37
    ///
38
    ///  ISO 8601 / RFC 3339 date & time format.
39
    ///
40
    Iso8601(Iso8601Formatter),
41
42
    ///
43
    ///  Simple format.
44
    ///
45
    Simple(SimpleFormatter),
46
47
    ///
48
    ///  Unix Timestamp format.
49
    ///
50
    UnixTimestamp(UnixTimestampFormatter),
51
52
    ///
53
    /// Custom format.
54
    ///
55
    /// Used to hold user provided formatter.
56
    ///
57
    Custom(Box<dyn FormatTrait>),
58
}
59
60
impl Formatter {
61
    ///
62
    /// Format the text of the `log_entry`, in accordance with the formatting
63
    /// for this [formatter](enum.Formatter.html).
64
    ///
65
    /// ## Parameters
66
    /// - `log_entry` - The log entry to be formatted.
67
    ///
68157
    pub fn format(&self, log_entry: &LogEntry) -> String {
69157
        match self {
7057
            Formatter::Iso8601(f) => f.format(log_entry),
7192
            Formatter::Simple(f) => f.format(log_entry),
723
            Formatter::UnixTimestamp(f) => f.format(log_entry),
735
            Formatter::Custom(f) => f.format(log_entry),
74
        }
75157
    }
76
77
    #[allow(dead_code)]
784
    pub(crate) fn width(&self) -> usize {
794
        15
804
    }
81
}
82
83
impl Default for Formatter {
841
    fn default() -> Self {
851
        FormatType::default().create(None)
861
    }
87
}
88
89
impl fmt::Display for Formatter {
9022
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
9122
        match self {
923
            Formatter::Iso8601(formatter) => formatter.fmt(f),
9314
            Formatter::Simple(formatter) => formatter.fmt(f),
941
            Formatter::UnixTimestamp(formatter) => formatter.fmt(f),
954
            Formatter::Custom(formatter) => formatter.fmt(f),
96
        }
9722
    }
98
}