2021-05-29 02:13:47 +10:00
|
|
|
/* OpenTally: Open-source election vote counting
|
2022-04-16 02:27:59 +10:00
|
|
|
* Copyright © 2021–2022 Lee Yingtong Li (RunasSudo)
|
2021-05-29 02:13:47 +10:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2021-06-14 20:43:36 +10:00
|
|
|
/// Smart logger used in election counts
|
2022-04-16 02:27:59 +10:00
|
|
|
#[derive(Clone)]
|
2021-05-29 02:13:47 +10:00
|
|
|
pub struct Logger<'a> {
|
2021-06-16 17:20:29 +10:00
|
|
|
/// [Vec] of log entries for the current stage
|
2021-05-29 02:13:47 +10:00
|
|
|
pub entries: Vec<LogEntry<'a>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Logger<'a> {
|
2021-06-14 20:43:36 +10:00
|
|
|
/// Append a new entry to the log
|
|
|
|
///
|
|
|
|
/// If consecutive smart log entries have the same templates, they will be merged
|
2021-05-29 02:13:47 +10:00
|
|
|
pub fn log(&mut self, entry: LogEntry<'a>) {
|
|
|
|
if let LogEntry::Smart(mut smart) = entry {
|
2021-10-27 19:52:51 +11:00
|
|
|
if !self.entries.is_empty() {
|
2021-05-29 02:13:47 +10:00
|
|
|
if let LogEntry::Smart(last_smart) = self.entries.last_mut().unwrap() {
|
|
|
|
if last_smart.template1 == smart.template1 && last_smart.template2 == smart.template2 {
|
2021-10-27 19:52:51 +11:00
|
|
|
last_smart.data.append(&mut smart.data);
|
2021-05-29 02:13:47 +10:00
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-14 20:43:36 +10:00
|
|
|
/// Append a new literal log entry
|
2021-05-29 02:13:47 +10:00
|
|
|
pub fn log_literal(&mut self, literal: String) {
|
|
|
|
self.log(LogEntry::Literal(literal));
|
|
|
|
}
|
|
|
|
|
2021-06-14 20:43:36 +10:00
|
|
|
/// Append a new smart log entry
|
|
|
|
///
|
|
|
|
/// If consecutive smart log entries have the same templates, they will be merged
|
2021-05-29 02:13:47 +10:00
|
|
|
pub fn log_smart(&mut self, template1: &'a str, template2: &'a str, data: Vec<&'a str>) {
|
|
|
|
self.log(LogEntry::Smart(SmartLogEntry {
|
2021-10-27 19:52:51 +11:00
|
|
|
template1,
|
|
|
|
template2,
|
|
|
|
data,
|
2021-05-29 02:13:47 +10:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2021-06-14 20:43:36 +10:00
|
|
|
/// Render the log to a [String]
|
2021-05-29 02:13:47 +10:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-14 20:43:36 +10:00
|
|
|
/// Represents either a literal or smart log entry
|
2022-04-16 02:27:59 +10:00
|
|
|
#[derive(Clone)]
|
2021-05-29 02:13:47 +10:00
|
|
|
pub enum LogEntry<'a> {
|
2021-06-16 17:20:29 +10:00
|
|
|
/// Smart log entry - see [SmartLogEntry]
|
2021-05-29 02:13:47 +10:00
|
|
|
Smart(SmartLogEntry<'a>),
|
2021-06-16 17:20:29 +10:00
|
|
|
/// Literal log entry
|
2021-05-29 02:13:47 +10:00
|
|
|
Literal(String)
|
|
|
|
}
|
|
|
|
|
2021-06-14 20:43:36 +10:00
|
|
|
/// Smart log entry
|
2022-04-16 02:27:59 +10:00
|
|
|
#[derive(Clone)]
|
2021-05-29 02:13:47 +10:00
|
|
|
pub struct SmartLogEntry<'a> {
|
|
|
|
template1: &'a str,
|
|
|
|
template2: &'a str,
|
|
|
|
data: Vec<&'a str>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> SmartLogEntry<'a> {
|
2021-06-14 20:43:36 +10:00
|
|
|
/// Render the [SmartLogEntry] to a [String]
|
2021-05-29 02:13:47 +10:00
|
|
|
pub fn render(&self) -> String {
|
2021-10-27 19:52:51 +11:00
|
|
|
if self.data.is_empty() {
|
2021-05-29 02:13:47 +10:00
|
|
|
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 {
|
2021-06-13 00:56:18 +10:00
|
|
|
return String::from(self.template2).replace("{}", &smart_join(&self.data));
|
2021-05-29 02:13:47 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-13 00:56:18 +10:00
|
|
|
|
2021-06-14 20:43:36 +10:00
|
|
|
/// Join the given strings, with commas and terminal "and"
|
2021-10-27 19:52:51 +11:00
|
|
|
#[allow(clippy::ptr_arg)]
|
2021-06-13 00:56:18 +10:00
|
|
|
pub fn smart_join(data: &Vec<&str>) -> String {
|
|
|
|
return format!("{} and {}", data[0..data.len()-1].join(", "), data.last().unwrap());
|
|
|
|
}
|