OpenTally/src/logger.rs

105 lines
3.2 KiB
Rust
Raw Normal View History

/* OpenTally: Open-source election vote counting
* Copyright © 2021 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/>.
*/
2021-06-14 20:43:36 +10:00
/// Smart logger used in election counts
pub struct Logger<'a> {
2021-06-16 17:20:29 +10:00
/// [Vec] of log entries for the current stage
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
pub fn log(&mut self, entry: LogEntry<'a>) {
if let LogEntry::Smart(mut smart) = entry {
if self.entries.len() > 0 {
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);
}
}
2021-06-14 20:43:36 +10:00
/// Append a new literal log entry
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
pub fn log_smart(&mut self, template1: &'a str, template2: &'a str, data: Vec<&'a str>) {
self.log(LogEntry::Smart(SmartLogEntry {
template1: template1,
template2: template2,
data: data,
}));
}
2021-06-14 20:43:36 +10:00
/// 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();
}
}
2021-06-14 20:43:36 +10:00
/// Represents either a literal or smart log entry
pub enum LogEntry<'a> {
2021-06-16 17:20:29 +10:00
/// Smart log entry - see [SmartLogEntry]
Smart(SmartLogEntry<'a>),
2021-06-16 17:20:29 +10:00
/// Literal log entry
Literal(String)
}
2021-06-14 20:43:36 +10:00
/// Smart log entry
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]
pub fn render(&self) -> String {
if self.data.len() == 0 {
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-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-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());
}