OpenTally/src/logger.rs

109 lines
3.3 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* OpenTally: Open-source election vote counting
* Copyright © 20212022 Lee Yingtong Li (RunasSudo)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/// Smart logger used in election counts
#[derive(Clone)]
pub struct Logger<'a> {
/// [Vec] of log entries for the current stage
pub entries: Vec<LogEntry<'a>>,
}
impl<'a> Logger<'a> {
/// Append a new entry to the log
///
/// If consecutive smart log entries have the same templates, they will be merged
pub fn log(&mut self, entry: LogEntry<'a>) {
if let LogEntry::Smart(mut smart) = entry {
if !self.entries.is_empty() {
if let LogEntry::Smart(last_smart) = self.entries.last_mut().unwrap() {
if last_smart.template1 == smart.template1 && last_smart.template2 == smart.template2 {
last_smart.data.append(&mut smart.data);
} else {
self.entries.push(LogEntry::Smart(smart));
}
} else {
self.entries.push(LogEntry::Smart(smart));
}
} else {
self.entries.push(LogEntry::Smart(smart));
}
} else {
self.entries.push(entry);
}
}
/// Append a new literal log entry
pub fn log_literal(&mut self, literal: String) {
self.log(LogEntry::Literal(literal));
}
/// Append a new smart log entry
///
/// If consecutive smart log entries have the same templates, they will be merged
pub fn log_smart(&mut self, template1: &'a str, template2: &'a str, data: Vec<&'a str>) {
self.log(LogEntry::Smart(SmartLogEntry {
template1,
template2,
data,
}));
}
/// Render the log to a [String]
pub fn render(&self) -> Vec<String> {
return self.entries.iter().map(|e| match e {
LogEntry::Smart(smart) => smart.render(),
LogEntry::Literal(literal) => literal.to_string(),
}).collect();
}
}
/// Represents either a literal or smart log entry
#[derive(Clone)]
pub enum LogEntry<'a> {
/// Smart log entry - see [SmartLogEntry]
Smart(SmartLogEntry<'a>),
/// Literal log entry
Literal(String)
}
/// Smart log entry
#[derive(Clone)]
pub struct SmartLogEntry<'a> {
template1: &'a str,
template2: &'a str,
data: Vec<&'a str>,
}
impl<'a> SmartLogEntry<'a> {
/// Render the [SmartLogEntry] to a [String]
pub fn render(&self) -> String {
if self.data.is_empty() {
panic!("Attempted to format smart log entry with no data");
} else if self.data.len() == 1 {
return String::from(self.template1).replace("{}", self.data.first().unwrap());
} else {
return String::from(self.template2).replace("{}", &smart_join(&self.data));
}
}
}
/// Join the given strings, with commas and terminal "and"
#[allow(clippy::ptr_arg)]
pub fn smart_join(data: &Vec<&str>) -> String {
return format!("{} and {}", data[0..data.len()-1].join(", "), data.last().unwrap());
}