src/handlers/console_handler/console_type.rs
Lines
100.00 %
Functions
100.00 %
Regions
100.00 %
| Line | Count | Source (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 ::{, str::}; | |
| 29 | use ::{,}; | |
| 30 | ||
| 31 | /// | |
| 32 | /// `ConsoleType` configures the `ConsoleHandler`'s output. | |
| 33 | /// | |
| 34 | #[derive(,,,,)] | |
| 35 | pub enum ConsoleType { | |
| 36 | #[default] | |
| 37 | /// | |
| 38 | /// Prints to `stdout`. | |
| 39 | /// | |
| 40 | , | |
| 41 | /// | |
| 42 | /// Print to `stderr`. | |
| 43 | /// | |
| 44 | , | |
| 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 | , | |
| 51 | } | |
| 52 | ||
| 53 | impl ConsoleType { | |
| 54 | /// | |
| 55 | /// Converts a console type to its string version. | |
| 56 | /// | |
| 57 | 37 | pub const fn as_str(&self) -> &'static str { |
| 58 | 37 | match self { |
| 59 | 23 | ::=> "stdout", |
| 60 | 8 | ::=> "stderr", |
| 61 | 6 | ::=> "production", |
| 62 | } | |
| 63 | 37 | } |
| 64 | } | |
| 65 | ||
| 66 | impl ::for ConsoleType { | |
| 67 | 3 | fn fmt(&selff: &mut ::<_>) -> :: { |
| 68 | 3 | selfas_str()fmt() |
| 69 | 3 | } |
| 70 | } | |
| 71 | ||
| 72 | /// | |
| 73 | /// Returned from `FromStr::from_str()` when an unknown string | |
| 74 | /// is passed-in. | |
| 75 | /// | |
| 76 | #[derive()] | |
| 77 | pub struct ConsoleTypeError { | |
| 78 | | |
| 79 | msg: | |
| 80 | } | |
| 81 | ||
| 82 | impl for ConsoleType { | |
| 83 | type Err =; | |
| 84 | ||
| 85 | 35 | fn from_str(s: &str) -> <SelfSelf::> { |
| 86 | 35 | match{ |
| 87 | 22 | "stdout" => Ok(::), |
| 88 | 7 | "stderr" => Ok(::), |
| 89 | 5 | "production" => Ok(::), |
| 90 | 1 | _ => Err({ |
| 91 | 1 | : format!("Unknown console type: {s}"), |
| 92 | 1 | }), |
| 93 | } | |
| 94 | 35 | } |
| 95 | } | |
| 96 | ||
| 97 | #[cfg()] | |
| 98 | mod tests { | |
| 99 | use super::*; | |
| 100 | use ::::{Result,}; | |
| 101 | ||
| 102 | #[test] | |
| 103 | 1 | fn as_str_to_from_str() { |
| 104 | 1 | forin ::() { |
| 105 | 3 | let=as_str(); |
| 106 | 3 | let= ::()unwrap(); |
| 107 | 3 | assert_eq!(,); |
| 108 | } | |
| 109 | 1 | } |
| 110 | ||
| 111 | #[test] | |
| 112 | 1 | fn from_str_fail() { |
| 113 | 1 | let= "file"; |
| 114 | 1 | assert!(::()is_err()); |
| 115 | 1 | } |
| 116 | ||
| 117 | #[test] | |
| 118 | 1 | fn console_display() { |
| 119 | 1 | let= "stdout\nstderr\nproduction\n"to_string(); |
| 120 | 1 | let mut= Vec::(); |
| 121 | ||
| 122 | 3 | forin ::() { |
| 123 | 3 | writeln!( "{console_type}")expect("writeln!() failed"); |
| 124 | 3 | } |
| 125 | ||
| 126 | 1 | assert_eq!(, String::()unwrap()); |
| 127 | 1 | } |
| 128 | } |