use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PathStep {
Section(usize),
Paragraph(usize),
Control(usize),
TableCell {
row: u16,
col: u16,
},
Style(usize),
}
impl fmt::Display for PathStep {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PathStep::Section(i) => write!(f, "sec[{}]", i),
PathStep::Paragraph(i) => write!(f, "para[{}]", i),
PathStep::Control(i) => write!(f, "ctrl[{}]", i),
PathStep::TableCell { row, col } => write!(f, "cell[r{},c{}]", row, col),
PathStep::Style(i) => write!(f, "style[{}]", i),
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct NodePath {
steps: Vec<PathStep>,
}
impl NodePath {
pub fn root() -> Self {
Self::default()
}
pub fn child(&self, step: PathStep) -> Self {
let mut steps = self.steps.clone();
steps.push(step);
Self { steps }
}
pub fn steps(&self) -> &[PathStep] {
&self.steps
}
pub fn is_root(&self) -> bool {
self.steps.is_empty()
}
pub fn section(&self) -> Option<usize> {
self.steps.iter().find_map(|s| match s {
PathStep::Section(i) => Some(*i),
_ => None,
})
}
pub fn paragraph(&self) -> Option<usize> {
self.steps.iter().rev().find_map(|s| match s {
PathStep::Paragraph(i) => Some(*i),
_ => None,
})
}
}
impl fmt::Display for NodePath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.steps.is_empty() {
return write!(f, "doc");
}
for (i, step) in self.steps.iter().enumerate() {
if i > 0 {
write!(f, "/")?;
}
write!(f, "{}", step)?;
}
Ok(())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum FindingKind {
SectionCountChanged,
ParagraphAdded,
ParagraphRemoved,
TextChanged,
ParagraphStyleChanged,
TableShapeChanged,
ControlCountChanged,
ControlKindChanged,
StyleCountChanged,
StyleChanged,
}
impl FindingKind {
pub const ALL: [FindingKind; 10] = [
FindingKind::SectionCountChanged,
FindingKind::ParagraphAdded,
FindingKind::ParagraphRemoved,
FindingKind::TextChanged,
FindingKind::ParagraphStyleChanged,
FindingKind::TableShapeChanged,
FindingKind::ControlCountChanged,
FindingKind::ControlKindChanged,
FindingKind::StyleCountChanged,
FindingKind::StyleChanged,
];
pub fn label(&self) -> &'static str {
match self {
FindingKind::SectionCountChanged => "sectionCountChanged",
FindingKind::ParagraphAdded => "paragraphAdded",
FindingKind::ParagraphRemoved => "paragraphRemoved",
FindingKind::TextChanged => "textChanged",
FindingKind::ParagraphStyleChanged => "paragraphStyleChanged",
FindingKind::TableShapeChanged => "tableShapeChanged",
FindingKind::ControlCountChanged => "controlCountChanged",
FindingKind::ControlKindChanged => "controlKindChanged",
FindingKind::StyleCountChanged => "styleCountChanged",
FindingKind::StyleChanged => "styleChanged",
}
}
}
impl fmt::Display for FindingKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.label())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Finding {
pub path: NodePath,
pub kind: FindingKind,
pub detail: String,
}
impl Finding {
pub fn to_json(&self) -> serde_json::Value {
serde_json::json!({
"path": self.path.to_string(),
"kind": self.kind.label(),
"detail": self.detail,
})
}
}
impl fmt::Display for Finding {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} {}: {}", self.path, self.kind.label(), self.detail)
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct DiffOptions {
pub ignore_whitespace: bool,
pub max_findings: Option<usize>,
}
impl DiffOptions {
pub fn ignore_whitespace(mut self, yes: bool) -> Self {
self.ignore_whitespace = yes;
self
}
pub fn max_findings(mut self, max: usize) -> Self {
self.max_findings = Some(max);
self
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct DocumentDiff {
pub identical: bool,
pub findings: Vec<Finding>,
pub truncated: bool,
}
impl DocumentDiff {
pub fn summary(&self) -> DiffSummary {
super::summary::summarize(self)
}
pub fn to_json(&self) -> serde_json::Value {
serde_json::json!({
"identical": self.identical,
"truncated": self.truncated,
"findingCount": self.findings.len(),
"findings": self.findings.iter().map(Finding::to_json).collect::<Vec<_>>(),
"summary": self.summary().to_json(),
})
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct DiffSummary {
pub total: usize,
pub truncated: bool,
pub by_kind: Vec<(FindingKind, usize)>,
}
impl DiffSummary {
pub fn count_of(&self, kind: FindingKind) -> usize {
self.by_kind
.iter()
.find(|(k, _)| *k == kind)
.map(|(_, n)| *n)
.unwrap_or(0)
}
pub fn to_json(&self) -> serde_json::Value {
let mut map = serde_json::Map::new();
for (kind, count) in &self.by_kind {
map.insert(kind.label().to_string(), serde_json::json!(count));
}
serde_json::json!({
"total": self.total,
"truncated": self.truncated,
"byKind": serde_json::Value::Object(map),
})
}
}