#[derive(Debug, Clone, PartialEq)]
pub enum LossKind {
FontInfo,
CharColor,
NamedStyle,
SectionSettings,
FloatingObject,
TextBox,
TrackChanges,
FormulaFallback,
Caption,
Other(String),
}
#[derive(Debug, Clone, PartialEq)]
pub struct LossEntry {
pub kind: LossKind,
pub location: String,
pub detail: String,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct LossReport {
entries: Vec<LossEntry>,
}
impl LossReport {
pub fn new() -> Self {
Self::default()
}
pub fn push(&mut self, entry: LossEntry) {
self.entries.push(entry);
}
pub fn merge(&mut self, other: LossReport) {
self.entries.extend(other.entries);
}
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
pub fn len(&self) -> usize {
self.entries.len()
}
pub fn iter(&self) -> impl Iterator<Item = &LossEntry> {
self.entries.iter()
}
}
impl IntoIterator for LossReport {
type Item = LossEntry;
type IntoIter = std::vec::IntoIter<LossEntry>;
fn into_iter(self) -> Self::IntoIter {
self.entries.into_iter()
}
}
impl<'a> IntoIterator for &'a LossReport {
type Item = &'a LossEntry;
type IntoIter = std::slice::Iter<'a, LossEntry>;
fn into_iter(self) -> Self::IntoIter {
self.entries.iter()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_report() {
let r = LossReport::new();
assert!(r.is_empty());
assert_eq!(r.len(), 0);
}
#[test]
fn push_and_iterate() {
let mut r = LossReport::new();
r.push(LossEntry {
kind: LossKind::FontInfo,
location: "section[0]/para[1]".to_string(),
detail: "Nanum Gothic".to_string(),
});
r.push(LossEntry {
kind: LossKind::Other("custom".to_string()),
location: "section[0]/para[2]".to_string(),
detail: "unknown field".to_string(),
});
assert!(!r.is_empty());
assert_eq!(r.len(), 2);
let kinds: Vec<_> = r.iter().map(|e| &e.kind).collect();
assert_eq!(kinds[0], &LossKind::FontInfo);
let count = r.into_iter().count();
assert_eq!(count, 2);
}
#[test]
fn default_is_empty() {
let r = LossReport::default();
assert!(r.is_empty());
}
}