src/handlers/string_handler.rs
Lines
100.00 %
Functions
100.00 %
Regions
100.00 %
| Line | Count | Source (jump to first uncovered line) |
|---|---|---|
| 1 | // | |
| 2 | // File Name: string_handler.rs | |
| 3 | // Project Name: flogging | |
| 4 | // | |
| 5 | // Copyright (C) 2025 Bradley Willcott | |
| 6 | // | |
| 7 | // SPDX-License-Identifier: GPL-3.0-or-later | |
| 8 | // | |
| 9 | // This library (crate) is free software: you can redistribute it and/or modify | |
| 10 | // it under the terms of the GNU General Public License as published by | |
| 11 | // the Free Software Foundation, either version 3 of the License, or | |
| 12 | // (at your option) any later version. | |
| 13 | // | |
| 14 | // This library (crate) is distributed in the hope that it will be useful, | |
| 15 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 17 | // GNU General Public License for more details. | |
| 18 | // | |
| 19 | // You should have received a copy of the GNU General Public License | |
| 20 | // along with this library (crate). If not, see <https://www.gnu.org/licenses/>. | |
| 21 | // | |
| 22 | ||
| 23 | //! | |
| 24 | //! # StringHandler | |
| 25 | //! | |
| 26 | use crate::*; | |
| 27 | use ::{, ::}; | |
| 28 | ||
| 29 | /// | |
| 30 | /// Publishes log entries to an internal list. | |
| 31 | /// | |
| 32 | /// The list can then be accessed via: [get_log()][StringHandler::get_log()]. | |
| 33 | /// | |
| 34 | #[derive(,)] | |
| 35 | pub struct StringHandler { | |
| 36 | formatter: | |
| 37 | log: <> | |
| 38 | } | |
| 39 | ||
| 40 | impl StringHandler { | |
| 41 | 12 | fn new() -> Self { |
| 42 | 12 | { |
| 43 | 12 | : ::create(None), |
| 44 | 12 | : Vec::(), |
| 45 | 12 | } |
| 46 | 12 | } |
| 47 | ||
| 48 | 4 | fn log(&self) -> { |
| 49 | 4 | let mut= String::(); |
| 50 | ||
| 51 | 10 | forin &self{ |
| 52 | 10 | push_str(); |
| 53 | 10 | push('\n'); |
| 54 | 10 | } |
| 55 | ||
| 56 | 4 | |
| 57 | 4 | } |
| 58 | } | |
| 59 | ||
| 60 | impl ::for StringHandler { | |
| 61 | 1 | fn fmt(&selff: &mut ::<_>) -> :: { |
| 62 | 1 | let= selfto_string()len(); |
| 63 | 1 | let= "-"repeat(+ 3); |
| 64 | ||
| 65 | 1 | write!( "{}\n{line}\n{}", self, selflog()) |
| 66 | 1 | } |
| 67 | } | |
| 68 | ||
| 69 | impl for StringHandler { | |
| 70 | 12 | fn create(_name: &str) -> <Self> |
| 71 | 12 | where |
| 72 | 12 | Self: |
| 73 | { | |
| 74 | 12 | Ok(::()) |
| 75 | 12 | } |
| 76 | ||
| 77 | 1 | fn close(&mut self) {} |
| 78 | ||
| 79 | 1 | fn flush(&mut self) { |
| 80 | 1 | selfclear(); |
| 81 | 1 | } |
| 82 | ||
| 83 | 1 | fn get_formatter(&self) -> { |
| 84 | 1 | selfclone() |
| 85 | 1 | } |
| 86 | ||
| 87 | 3 | fn get_log(&self) -> { |
| 88 | 3 | selflog() |
| 89 | 3 | } |
| 90 | ||
| 91 | 1 | fn is_open(&self) -> bool { |
| 92 | 1 | true |
| 93 | 1 | } |
| 94 | ||
| 95 | #[allow()] | |
| 96 | 30 | fn publish(&mut selflog_entry: &) { |
| 97 | 30 | selfpush(selfformat()); |
| 98 | 30 | } |
| 99 | ||
| 100 | 1 | fn set_formatter(&mut selfformatter:) { |
| 101 | 1 | self=; |
| 102 | 1 | } |
| 103 | ||
| 104 | /// | |
| 105 | /// This is a 'NoOp' fn. Use `get_log()`, as this already | |
| 106 | /// has the required functionality. | |
| 107 | /// | |
| 108 | 1 | fn set_test_mode(&mut self_state: bool) {} |
| 109 | } | |
| 110 | ||
| 111 | #[cfg()] | |
| 112 | mod tests { | |
| 113 | use crate::*; | |
| 114 | ||
| 115 | #[test] | |
| 116 | 1 | fn string_handler() { |
| 117 | 1 | let= "flogging::handlers::string_handler::tests-> [INFO ] trait methods |
| 118 | 1 | flogging::handlers::string_handler::tests-> [WARNING] The sky is falling!\n" |
| 119 | 1 | to_string(); |
| 120 | ||
| 121 | 1 | let mut= ::(module_path!()); |
| 122 | ||
| 123 | 1 | info("trait methods"); |
| 124 | 1 | warning("The sky is falling!"); |
| 125 | ||
| 126 | 1 | let=get_handler(crate::::)unwrap(); |
| 127 | 1 | set_test_mode(true); |
| 128 | 1 | assert!(is_open()); |
| 129 | 1 | assert_eq!( |
| 130 | 1 | get_formatter()to_string(), |
| 131 | 1 | "dt_fmt: \"\" - fmt_string: \"{mod_path}->{fn_name} [{level:7}] {message}\"" |
| 132 | 1 | to_string() |
| 133 | ); | |
| 134 | 1 | assert_eq!(,get_log()); |
| 135 | 1 | flush(); |
| 136 | ||
| 137 | 1 | assert_eq!(get_log(), ""to_string()); |
| 138 | 1 | close(); |
| 139 | 1 | } |
| 140 | } |