#![allow(dead_code)]
use std::collections::HashMap;
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CellPath {
pub table_ctrl_idx: usize,
pub row: u16,
pub col: u16,
pub inner_para_idx: usize,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WarningKind {
LinesegArrayEmpty,
LinesegUncomputed,
LinesegTextRunReflow,
}
impl fmt::Display for WarningKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use WarningKind::*;
match self {
LinesegArrayEmpty => write!(f, "lineseg 배열이 비어있음"),
LinesegUncomputed => write!(f, "lineseg 가 미계산 상태 (line_height=0)"),
LinesegTextRunReflow => write!(f, "lineseg 가 문단당 1개 (한컴 textRun reflow 의존)"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ValidationWarning {
pub section_idx: usize,
pub paragraph_idx: usize,
pub cell_path: Option<CellPath>,
pub kind: WarningKind,
}
#[derive(Debug, Clone, Default)]
pub struct ValidationReport {
pub warnings: Vec<ValidationWarning>,
}
impl ValidationReport {
pub fn new() -> Self {
Self::default()
}
pub fn is_empty(&self) -> bool {
self.warnings.is_empty()
}
pub fn len(&self) -> usize {
self.warnings.len()
}
pub fn push(&mut self, w: ValidationWarning) {
self.warnings.push(w);
}
pub fn summary(&self) -> HashMap<String, usize> {
let mut map = HashMap::new();
for w in &self.warnings {
let key = format!("{}", w.kind);
*map.entry(key).or_insert(0) += 1;
}
map
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn report_default_is_empty() {
let r = ValidationReport::new();
assert!(r.is_empty());
assert_eq!(r.len(), 0);
}
#[test]
fn summary_groups_by_kind() {
let mut r = ValidationReport::new();
r.push(ValidationWarning {
section_idx: 0,
paragraph_idx: 1,
cell_path: None,
kind: WarningKind::LinesegArrayEmpty,
});
r.push(ValidationWarning {
section_idx: 0,
paragraph_idx: 2,
cell_path: None,
kind: WarningKind::LinesegUncomputed,
});
r.push(ValidationWarning {
section_idx: 0,
paragraph_idx: 3,
cell_path: None,
kind: WarningKind::LinesegUncomputed,
});
let summary = r.summary();
assert_eq!(summary.len(), 2);
assert_eq!(summary.get("lineseg 배열이 비어있음").copied(), Some(1));
assert_eq!(
summary
.get("lineseg 가 미계산 상태 (line_height=0)")
.copied(),
Some(2)
);
}
#[test]
fn warning_display_messages() {
assert_eq!(
format!("{}", WarningKind::LinesegArrayEmpty),
"lineseg 배열이 비어있음"
);
assert_eq!(
format!("{}", WarningKind::LinesegUncomputed),
"lineseg 가 미계산 상태 (line_height=0)"
);
}
#[test]
fn cell_path_equality() {
let a = CellPath {
table_ctrl_idx: 0,
row: 1,
col: 2,
inner_para_idx: 3,
};
let b = CellPath {
table_ctrl_idx: 0,
row: 1,
col: 2,
inner_para_idx: 3,
};
assert_eq!(a, b);
}
}