use std::collections::{BTreeMap, HashMap, HashSet};
use super::{
EditError, EditOutcome, EditPlan, EditResult, ExistingRevisions, OperationKind, Resolved,
StoryPart, Transaction, check_update_fields, comment_holds_kept_text, comment_text_of,
controls, enclose_deleted_paragraph_comment_references, lift_deletion_comments,
revision_counts, sections, tracked, whole,
};
use crate::comparer::WmlComparerSettings;
use crate::inspect::{body_paragraph_nodes, project_paragraph, source_sha256};
use crate::namespaces::W;
use crate::result::{Effect, codes};
use crate::validate::{self, Finding};
fn finding_key(f: &Finding) -> (String, String, String, String) {
(
f.set.clone(),
f.code.clone(),
f.part.clone(),
f.location.clone(),
)
}
pub fn introduced_findings(baseline: &[Finding], candidate: &[Finding]) -> Vec<Finding> {
let held: HashSet<_> = baseline.iter().map(finding_key).collect();
let mut seen = HashSet::new();
candidate
.iter()
.filter(|f| !held.contains(&finding_key(f)))
.filter(|f| seen.insert(finding_key(f)))
.cloned()
.collect()
}
const BLOCKING_CODES: &[&str] = &["BOOKMARK_CROSSES_REVISION", "CELL_FINAL_MARK_REVISION"];
fn blocking(f: &Finding) -> bool {
f.word_fatal || BLOCKING_CODES.contains(&f.code.as_str())
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PrimaryBytes {
Source,
Candidate,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct CandidateChecks {
pub baseline: Vec<Finding>,
pub introduced: Vec<Finding>,
pub unresolved: Vec<Finding>,
pub integrity: bool,
pub operation_postconditions: bool,
}
impl CandidateChecks {
pub fn verification(&self) -> crate::result::Verification {
let outcome = |ran| if ran { "passed" } else { "skipped" }.to_string();
crate::result::Verification {
integrity: outcome(self.integrity),
operation_postconditions: outcome(self.operation_postconditions),
baseline_findings: Some(self.baseline.clone()),
introduced_findings: Some(self.introduced.clone()),
unresolved_findings: Some(self.unresolved.clone()),
..Default::default()
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum PrimaryRole {
#[default]
Clean,
Redline,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct PrepareOptions {
pub require_change: bool,
pub exact_text: bool,
pub role: PrimaryRole,
pub syntax: super::PlanSyntax,
pub skip_failed: bool,
}
impl Default for PrepareOptions {
fn default() -> Self {
Self {
require_change: false,
exact_text: false,
role: PrimaryRole::Clean,
syntax: super::PlanSyntax::V1,
skip_failed: false,
}
}
}
#[derive(Clone, Debug)]
pub struct PreparedEdit {
pub result: EditResult,
pub source_sha256: String,
pub request_sha256: String,
pub primary: Vec<u8>,
pub primary_bytes: PrimaryBytes,
pub primary_sha256: String,
pub primary_effect: Effect,
pub checks: CandidateChecks,
pub correspondence: Option<crate::expect::SourceToOutputCorrespondence>,
pub skipped: Vec<super::SkippedOperation>,
}
pub fn prepare(
source: &[u8],
plan: &EditPlan,
options: &PrepareOptions,
) -> Result<PreparedEdit, EditError> {
if options.skip_failed {
return super::skip::prepare_skipping(source, plan, options);
}
let known = SourceChecks::default();
prepare_once(source, plan, options, &known).map_err(|failure| {
let mut reruns = Reruns::new(|ops: &[usize]| {
let part = EditPlan {
operations: ops.iter().map(|&i| plan.operations[i].clone()).collect(),
..plan.clone()
};
prepare_once(source, &part, options, &known)
.err()
.and_then(|f| f.refusal)
.map(|(refusals, _)| refusals)
})
.knowing(plan.operations.len(), &failure);
attributed(*failure, plan.operations.len(), true, &mut reruns)
})
}
pub(super) fn prepare_blaming(
source: &[u8],
plan: &EditPlan,
count: usize,
options: &PrepareOptions,
) -> Result<PreparedEdit, Box<Blame>> {
let known = SourceChecks::default();
let failure = match prepare_once(source, plan, options, &known) {
Ok(prepared) => return Ok(prepared),
Err(failure) => failure,
};
let Some((refusals, outcomes)) = failure.refusal.clone() else {
return Err(Box::new(Blame::Plain(failure.error)));
};
let mut reruns = Reruns::new(|ops: &[usize]| {
let part = EditPlan {
operations: ops
.iter()
.copied()
.chain(count..plan.operations.len())
.map(|i| plan.operations[i].clone())
.collect(),
..plan.clone()
};
prepare_once(source, &part, options, &known)
.err()
.and_then(|f| f.refusal)
.map(|(refusals, _)| refusals)
})
.knowing(count, &failure);
let mut distinct: Vec<&Refusal> = Vec::new();
for refusal in &refusals {
if !distinct.iter().any(|d| d.same(refusal)) {
distinct.push(refusal);
}
}
let bits = (usize::BITS - count.leading_zeros()) as usize;
let alone = distinct.len() * bits >= count;
let mut blamed: Vec<(usize, EditError)> = Vec::new();
for refusal in distinct {
let single = alone
.then(|| (0..count).find(|&i| reruns.reproduces(&[i], refusal)))
.flatten()
.map(|i| vec![i]);
let Some(ops) =
single.or_else(|| culprits(count, true, |ops| reruns.reproduces(ops, refusal)))
else {
return Err(Box::new(Blame::Candidate(
attributed(*failure, count, true, &mut reruns),
Vec::new(),
)));
};
if !blamed.iter().any(|(i, _)| *i == ops[0]) {
let error = advised(
naming(refusal.error.clone(), &ops, &outcomes),
failure.advice,
);
blamed.push((ops[0], error));
}
}
Err(Box::new(Blame::Candidate(failure.error, blamed)))
}
pub(super) enum Blame {
Plain(EditError),
Candidate(EditError, Vec<(usize, EditError)>),
}
#[derive(Default)]
struct SourceChecks {
findings: std::cell::OnceCell<Option<Vec<Finding>>>,
revision_ids: std::cell::OnceCell<Option<HashMap<(String, String), usize>>>,
}
#[derive(Clone, Debug)]
struct Refusal {
artifact: &'static str,
code: String,
part: String,
subject: String,
error: EditError,
}
impl Refusal {
fn same(&self, other: &Self) -> bool {
self.artifact == other.artifact
&& self.code == other.code
&& self.part == other.part
&& self.subject == other.subject
}
}
type Refused = Vec<Refusal>;
fn refused(
artifact: &'static str,
code: &str,
part: &str,
subject: String,
message: String,
) -> Refusal {
Refusal {
artifact,
code: code.to_string(),
part: part.to_string(),
subject,
error: err("CANDIDATE_INVALID", None, message),
}
}
struct Failure {
error: EditError,
refusal: Option<(Refused, Vec<EditOutcome>)>,
advice: Option<&'static str>,
}
struct Reruns<F> {
rerun: F,
met: HashMap<Vec<usize>, Option<Refused>>,
}
impl<F: FnMut(&[usize]) -> Option<Refused>> Reruns<F> {
fn new(rerun: F) -> Self {
Self {
rerun,
met: HashMap::new(),
}
}
fn knowing(mut self, count: usize, failure: &Failure) -> Self {
if let Some((refusals, _)) = &failure.refusal {
self.met
.insert((0..count).collect(), Some(refusals.clone()));
}
self
}
fn reproduces(&mut self, ops: &[usize], refusal: &Refusal) -> bool {
let rerun = &mut self.rerun;
self.met
.entry(ops.to_vec())
.or_insert_with(|| rerun(ops))
.as_ref()
.is_some_and(|met| met.iter().any(|r| r.same(refusal)))
}
}
fn advised(mut error: EditError, advice: Option<&'static str>) -> EditError {
if let Some(advice) = advice {
error.message = format!("{}; {advice}", error.message);
}
error
}
impl From<EditError> for Box<Failure> {
fn from(error: EditError) -> Self {
Box::new(Failure {
error,
refusal: None,
advice: None,
})
}
}
fn attributed<F: FnMut(&[usize]) -> Option<Refused>>(
failure: Failure,
count: usize,
together: bool,
reruns: &mut Reruns<F>,
) -> EditError {
let Failure {
error,
refusal,
advice,
} = failure;
let Some((refusals, outcomes)) = refusal else {
return advised(error, advice);
};
let Some(first) = refusals.first() else {
return advised(error, advice);
};
let Some(ops) = culprits(count, together, |ops| reruns.reproduces(ops, first)) else {
return advised(error, advice);
};
advised(naming(error, &ops, &outcomes), advice)
}
fn naming(mut error: EditError, ops: &[usize], outcomes: &[EditOutcome]) -> EditError {
let name = |i: usize| match outcomes.get(i) {
Some(EditOutcome {
id,
kind,
paragraph: Some(paragraph),
..
}) => format!("{id} ({kind} on {paragraph})"),
Some(EditOutcome { id, kind, .. }) => format!("{id} ({kind})"),
None => format!("/operations/{i}"),
};
let Some(&cause) = ops.first() else {
return error;
};
error.operation = Some(match outcomes.get(cause) {
Some(outcome) => outcome.id.clone(),
None => format!("/operations/{cause}"),
});
error.message = format!("{}; {} introduces it", error.message, name(cause));
if let [_, others @ ..] = ops
&& let Some((&last, rest)) = others.split_last()
{
let mut message = format!("{}, together with ", error.message);
for (n, &i) in rest.iter().enumerate() {
if n > 0 {
message.push_str(", ");
}
message.push_str(&name(i));
}
if !rest.is_empty() {
message.push_str(" and ");
}
message.push_str(&name(last));
error.message = message;
}
error
}
fn culprits(
count: usize,
together: bool,
mut reproduces: impl FnMut(&[usize]) -> bool,
) -> Option<Vec<usize>> {
if count == 0 || reproduces(&[]) {
return None;
}
let all: Vec<usize> = (0..count).collect();
let (mut lo, mut hi) = (0, count);
while hi - lo > 1 {
let mid = lo + (hi - lo) / 2;
if reproduces(&all[..mid]) {
hi = mid;
} else {
lo = mid;
}
}
let first = hi - 1;
let mut needed = vec![first];
if !together {
return Some(needed);
}
let with = |pool: &[usize], needed: &[usize]| {
let mut ops: Vec<usize> = pool.iter().chain(needed).copied().collect();
ops.sort_unstable();
ops
};
let mut pool = all[..first].to_vec();
while !reproduces(&with(&[], &needed)) {
let (mut lo, mut hi) = (0, pool.len());
while hi - lo > 1 {
let mid = lo + (hi - lo) / 2;
if reproduces(&with(&pool[..mid], &needed)) {
hi = mid;
} else {
lo = mid;
}
}
let Some(&next) = pool.get(hi.wrapping_sub(1)) else {
break;
};
needed.push(next);
pool.truncate(hi - 1);
}
needed[1..].sort_unstable();
Some(needed)
}
fn prepare_once(
source: &[u8],
plan: &EditPlan,
options: &PrepareOptions,
known: &SourceChecks,
) -> Result<PreparedEdit, Box<Failure>> {
#[cfg(test)]
tests::PREPARATIONS.with(|count| count.set(count.get() + 1));
check_update_fields(plan)?;
let lowered = super::visible::lower(source, plan, options.syntax)?;
let plan = lowered.as_ref().unwrap_or(plan);
let mut tx = Transaction::start_quoting(source, plan, options.syntax)?;
tx.resolve()?;
tracked::check(&tx)?;
let emit = tx.emits_directly()?;
let compared_beside = !emit && plan.update_fields && tx.edits_beside_a_range_marker();
tx.apply()?;
check_dom_realized(&tx, options.exact_text)?;
let result = produce(&mut tx, emit, options.role)?;
let effect = primary_effect(&tx, &result)?;
let (primary, primary_bytes) = match effect {
Effect::Unchanged => (source.to_vec(), PrimaryBytes::Source),
_ => match options.role {
PrimaryRole::Clean => (result.clean.clone(), PrimaryBytes::Candidate),
PrimaryRole::Redline => (result.redline.clone(), PrimaryBytes::Candidate),
},
};
let mut checks = screen_candidates(source, &result, known)
.and_then(|checks| {
if compared_beside {
check_rejected_ranges(source, &result.redline)?;
}
Ok(checks)
})
.map_err(|refusals| {
Box::new(Failure {
error: refusals[0].error.clone(),
refusal: Some((refusals, tx.outcomes.clone())),
advice: compared_beside.then_some(
"update_fields compares an edit beside a bookmark, comment or permission range, so drop update_fields and refresh the fields with `jubarte convert FILE --update-fields -o OUT.docx` afterwards",
),
})
})?;
checks.operation_postconditions = true;
if options.require_change && effect == Effect::Unchanged {
return Err(err(
codes::NO_CHANGES_REQUESTED,
None,
"the primary transaction has no document effect; --require-change was given",
)
.into());
}
Ok(PreparedEdit {
result,
source_sha256: source_sha256(source),
request_sha256: source_sha256(plan.to_json().as_bytes()),
primary_sha256: source_sha256(&primary),
primary,
primary_bytes,
primary_effect: effect,
checks,
correspondence: None,
skipped: Vec::new(),
})
}
#[cfg(feature = "render")]
fn fields_updated(report: &super::EditReport) -> bool {
!report.fields.is_empty()
}
#[cfg(not(feature = "render"))]
fn fields_updated(_report: &super::EditReport) -> bool {
false
}
fn primary_effect(tx: &Transaction<'_>, result: &EditResult) -> Result<Effect, EditError> {
let report = &result.report;
if !report.resolved_revisions.is_empty()
|| report.revisions.total > 0
|| report.paragraphs.from != report.paragraphs.to
|| fields_updated(report)
|| report.comments_added > 0
|| tx.watermark.is_some()
|| tx.settings.is_some()
|| tx.notes_story.is_some()
{
return Ok(Effect::Changed);
}
for (op, resolved) in &tx.resolved {
if !matches!(resolved, Resolved::Text { .. })
|| !matches!(
tx.plan.operations[*op].kind,
OperationKind::Replace { .. }
| OperationKind::Insert { .. }
| OperationKind::Delete { .. }
| OperationKind::Rewrite { .. }
)
{
return Ok(Effect::Changed);
}
}
if expected_texts(tx, false)?.into_iter().any(|(p, e)| {
matches!(e.scope, ExpectedScope::Paragraph) && e.text != tx.projections[p].text
}) {
return Ok(Effect::Changed);
}
Ok(Effect::Unchanged)
}
fn check_rejected_ranges(source: &[u8], redline: &[u8]) -> Result<(), Refused> {
let rejected = crate::document_comparer::reject_revisions(redline).map_err(|e| {
vec![refused(
"redline",
"REJECT_FAILED",
"",
String::new(),
format!("the redline cannot be rejected: {e}"),
)]
})?;
let (before, after) = (range_markers(source), range_markers(&rejected));
let lost = before
.bookmarks
.difference(&after.bookmarks)
.map(|name| format!("bookmark {name}"))
.chain((after.comments < before.comments).then(|| "a comment's range".to_string()))
.chain((after.permissions < before.permissions).then(|| "a permission range".to_string()))
.collect::<Vec<_>>();
if lost.is_empty() {
return Ok(());
}
let lost = lost.join(", ");
Err(vec![refused(
"redline",
"RANGE_LOST",
"",
lost.clone(),
format!("Reject All of the redline drops {lost} the source holds"),
)])
}
#[derive(Default)]
struct RangeMarkers {
bookmarks: std::collections::BTreeSet<String>,
comments: usize,
permissions: usize,
}
fn range_markers(docx: &[u8]) -> RangeMarkers {
let mut markers = RangeMarkers::default();
let Ok(opened) = crate::inspect::Opened::open(docx) else {
return markers;
};
let mut parts = vec![opened.main.clone()];
parts.extend(opened.story_parts().into_iter().map(|(_, _, part)| part));
for part in parts {
let Ok((dom, _, root)) = crate::inspect::parse_part(&opened.pkg, &part) else {
continue;
};
for start in dom.descendants(root, Some(&W::bookmark_start())) {
if let Some(name) = dom.attribute(start, &W::name("name"))
&& name != "_GoBack"
{
markers.bookmarks.insert(name.to_string());
}
}
markers.comments += dom.descendants(root, Some(&W::comment_range_start())).len();
markers.permissions += dom.descendants(root, Some(&W::perm_start())).len();
}
markers
}
#[cfg(test)]
fn check_candidates(source: &[u8], result: &EditResult) -> Result<CandidateChecks, EditError> {
screen_candidates(source, result, &SourceChecks::default())
.map_err(|refusals| refusals[0].error.clone())
}
fn screen_candidates(
source: &[u8],
result: &EditResult,
known: &SourceChecks,
) -> Result<CandidateChecks, Refused> {
let baseline = known
.findings
.get_or_init(|| validate::validate(source).ok())
.as_ref();
let mut refusals = Vec::new();
let mut introduced = Vec::new();
let mut unresolved = Vec::new();
for (artifact, bytes) in [("clean copy", &result.clean), ("redline", &result.redline)] {
let findings = match validate::validate(bytes) {
Ok(findings) => findings,
Err(e) => {
refusals.push(refused(
artifact,
"UNREADABLE",
"",
String::new(),
format!("the serialized candidate cannot be validated: {e}"),
));
return Err(refusals);
}
};
if findings.iter().any(|f| f.code == "DUPLICATE_REVISION_ID")
&& let Some(source_ids) = known
.revision_ids
.get_or_init(|| validate::revision_id_occurrences(source).ok())
{
match check_revision_ids(source_ids, artifact, bytes) {
Ok(duplicates) => refusals.extend(duplicates),
Err(unreadable) => {
refusals.extend(unreadable);
return Err(refusals);
}
}
}
for finding in &findings {
if !unresolved
.iter()
.any(|f| finding_key(f) == finding_key(finding))
{
unresolved.push(finding.clone());
}
}
let Some(baseline) = baseline else {
continue;
};
let suspects: HashSet<String> = introduced_findings(baseline, &findings)
.into_iter()
.map(|finding| finding.code)
.collect();
let baseline = if suspects.is_empty() {
baseline.to_vec()
} else {
relocate_findings(source, bytes, baseline, &suspects)
};
for finding in introduced_findings(&baseline, &findings) {
if blocking(&finding) {
refusals.push(refused(
artifact,
&finding.code,
&finding.part,
finding.unnumbered(),
format!(
"the edit introduces {} in {}#{}: {}",
finding.code, finding.part, finding.location, finding.message
),
));
continue;
}
if !introduced
.iter()
.any(|f| finding_key(f) == finding_key(&finding))
{
introduced.push(finding);
}
}
}
if !refusals.is_empty() {
return Err(refusals);
}
Ok(CandidateChecks {
baseline: baseline.cloned().unwrap_or_default(),
introduced,
unresolved,
integrity: true,
operation_postconditions: false,
})
}
struct ParsedPart {
dom: crate::xmllinq::Dom,
root: crate::xmllinq::NodeId,
siblings: HashMap<(crate::xmllinq::NodeId, String, String), std::rc::Rc<Siblings>>,
}
struct Siblings {
nodes: Vec<crate::xmllinq::NodeId>,
texts: Vec<String>,
marks: Vec<Vec<(String, String)>>,
text_counts: HashMap<String, usize>,
}
impl ParsedPart {
fn siblings(
&mut self,
parent: crate::xmllinq::NodeId,
namespace: &str,
local: &str,
) -> std::rc::Rc<Siblings> {
let key = (parent, namespace.to_string(), local.to_string());
if let Some(found) = self.siblings.get(&key) {
return std::rc::Rc::clone(found);
}
let nodes = named_children(&self.dom, parent, namespace, local);
let texts: Vec<String> = nodes.iter().map(|&node| self.dom.value(node)).collect();
let marks = nodes
.iter()
.map(|&node| revision_marks(&self.dom, node))
.collect();
let mut text_counts = HashMap::new();
for text in &texts {
*text_counts.entry(text.clone()).or_insert(0) += 1;
}
let built = std::rc::Rc::new(Siblings {
nodes,
texts,
marks,
text_counts,
});
self.siblings.insert(key, std::rc::Rc::clone(&built));
built
}
}
fn relocate_findings(
source: &[u8],
candidate: &[u8],
baseline: &[Finding],
suspects: &HashSet<String>,
) -> Vec<Finding> {
let (Ok(source), Ok(candidate)) = (
crate::strict_translation::strict_to_transitional_docx(source),
crate::strict_translation::strict_to_transitional_docx(candidate),
) else {
return baseline.to_vec();
};
let Ok(source_pkg) = crate::opc::PartFs::open(&source) else {
return baseline.to_vec();
};
let Ok(candidate_pkg) = crate::opc::PartFs::open(&candidate) else {
return baseline.to_vec();
};
let mut source_parts = HashMap::new();
let mut candidate_parts = HashMap::new();
baseline
.iter()
.map(|finding| {
let mut finding = finding.clone();
if !suspects.contains(&finding.code) {
return finding;
}
if let Some(location) = moved_location(
&source_pkg,
&candidate_pkg,
&mut source_parts,
&mut candidate_parts,
&finding.part,
&finding.location,
) {
finding.r#ref.clone_from(&location);
finding.location = location;
}
finding
})
.collect()
}
fn ensure_part(
pkg: &crate::opc::PartFs,
cache: &mut HashMap<String, ParsedPart>,
part: &str,
) -> Option<()> {
if cache.contains_key(part) {
return Some(());
}
let (dom, _, root) = crate::inspect::parse_part(pkg, part).ok()?;
cache.insert(
part.to_string(),
ParsedPart {
dom,
root,
siblings: HashMap::new(),
},
);
Some(())
}
fn moved_location(
source_pkg: &crate::opc::PartFs,
candidate_pkg: &crate::opc::PartFs,
source_parts: &mut HashMap<String, ParsedPart>,
candidate_parts: &mut HashMap<String, ParsedPart>,
part: &str,
location: &str,
) -> Option<String> {
if !location.contains('[') {
return None;
}
ensure_part(source_pkg, source_parts, part)?;
ensure_part(candidate_pkg, candidate_parts, part)?;
let source = source_parts.get_mut(part)?;
let candidate = candidate_parts.get_mut(part)?;
let mut source_node = source.root;
let mut candidate_node = candidate.root;
let mut steps = Vec::new();
for step in location.split('/') {
let (namespace, local, index) = parse_step(step)?;
let source_siblings = source.siblings(source_node, &namespace, &local);
let candidate_siblings = candidate.siblings(candidate_node, &namespace, &local);
let source_child = *source_siblings.nodes.get(index)?;
let new_index = match_child(
&source.dom,
index,
&source_siblings,
&candidate.dom,
&candidate_siblings,
)?;
let candidate_child = candidate_siblings.nodes[new_index];
let name = step.split_once('[').map_or(step, |(name, _)| name);
steps.push(format!("{name}[{new_index}]"));
source_node = source_child;
candidate_node = candidate_child;
}
let relocated = steps.join("/");
(relocated != location).then_some(relocated)
}
fn parse_step(step: &str) -> Option<(String, String, usize)> {
let (name, rest) = step.strip_suffix(']')?.split_once('[')?;
let index = rest.parse().ok()?;
let (namespace, local) = match name.split_once(':') {
Some((prefix, local)) => (
crate::xmllinq::serialize::well_known_namespace(prefix)?.to_string(),
local.to_string(),
),
None => (String::new(), name.to_string()),
};
Some((namespace, local, index))
}
fn named_children(
dom: &crate::xmllinq::Dom,
parent: crate::xmllinq::NodeId,
namespace: &str,
local: &str,
) -> Vec<crate::xmllinq::NodeId> {
dom.elements(parent, None)
.into_iter()
.filter(|&element| {
dom.name(element).is_some_and(|name| {
name.local_name() == local && name.namespace_name() == namespace
})
})
.collect()
}
fn match_child(
source_dom: &crate::xmllinq::Dom,
index: usize,
source: &Siblings,
candidate_dom: &crate::xmllinq::Dom,
candidate: &Siblings,
) -> Option<usize> {
let node = source.nodes[index];
let para_id = crate::namespaces::W14::para_id();
if let Some(id) = source_dom.attribute(node, ¶_id)
&& let Some(found) = sole_index(&candidate.nodes, |sibling| {
candidate_dom.attribute(sibling, ¶_id) == Some(id)
})
&& sole_index(&source.nodes, |sibling| {
source_dom.attribute(sibling, ¶_id) == Some(id)
})
.is_some()
{
return Some(found);
}
if let Some(revision) = own_revision(source_dom, node)
&& let Some(found) = sole_index(&candidate.nodes, |sibling| {
own_revision(candidate_dom, sibling).as_ref() == Some(&revision)
})
{
return Some(found);
}
let marks = &source.marks[index];
if !marks.is_empty()
&& source.marks.iter().filter(|other| *other == marks).count() == 1
&& let Some(found) = unique_position(&candidate.marks, marks)
{
return Some(found);
}
let text = &source.texts[index];
if source.text_counts.get(text) == Some(&1) && candidate.text_counts.get(text) == Some(&1) {
return candidate.texts.iter().position(|other| other == text);
}
(source.nodes.len() == candidate.nodes.len()).then_some(index)
}
fn unique_position<T: PartialEq>(items: &[T], wanted: &T) -> Option<usize> {
let mut found = None;
for (at, item) in items.iter().enumerate() {
if item != wanted {
continue;
}
if found.is_some() {
return None;
}
found = Some(at);
}
found
}
fn sole_index(
nodes: &[crate::xmllinq::NodeId],
mut pred: impl FnMut(crate::xmllinq::NodeId) -> bool,
) -> Option<usize> {
let mut found = None;
for (at, &node) in nodes.iter().enumerate() {
if !pred(node) {
continue;
}
if found.is_some() {
return None;
}
found = Some(at);
}
found
}
fn own_revision(
dom: &crate::xmllinq::Dom,
node: crate::xmllinq::NodeId,
) -> Option<(String, String)> {
let id = dom.attribute(node, &W::id())?;
dom.attribute(node, &W::author())?;
Some((dom.name(node)?.local_name().to_string(), id.to_string()))
}
fn revision_marks(
dom: &crate::xmllinq::Dom,
node: crate::xmllinq::NodeId,
) -> Vec<(String, String)> {
let mut marks: Vec<(String, String)> = std::iter::once(node)
.chain(dom.descendants(node, None))
.filter_map(|element| own_revision(dom, element))
.collect();
marks.sort();
marks.dedup();
marks
}
fn check_revision_ids(
source: &HashMap<(String, String), usize>,
artifact: &'static str,
candidate: &[u8],
) -> Result<Vec<Refusal>, Refused> {
let occurrences = validate::revision_id_occurrences(candidate).map_err(|e| {
vec![refused(
artifact,
"UNREADABLE",
"",
String::new(),
format!("the serialized candidate cannot be read: {e}"),
)]
})?;
let mut introduced: Vec<_> = occurrences
.iter()
.filter(|&(key, &count)| count > 1 && count > source.get(key).copied().unwrap_or(0))
.collect();
introduced.sort();
Ok(introduced
.into_iter()
.map(|(entry, &count)| {
let (part, key) = entry;
let mut fields = key.splitn(3, ':');
let (kind, mark, id) = (
fields.next().unwrap_or_default(),
fields.next() == Some("true"),
fields.next().unwrap_or_default(),
);
let mark = if mark { " on a mark" } else { "" };
let had = source.get(entry).copied().unwrap_or(0);
let subject = if had > 0 {
format!("w:{kind}{mark} w:id {id}")
} else {
format!("w:{kind}{mark}")
};
refused(
artifact,
"DUPLICATE_REVISION_ID",
part,
subject,
format!(
"the edit introduces a duplicate revision id in the {artifact}: w:{kind}{mark} w:id {id} occurs {count} times in {part} (the source has {had})"
),
)
})
.collect())
}
struct ExpectedText {
op: String,
text: String,
ignore_symbols: bool,
scope: ExpectedScope,
}
#[derive(Clone, Copy)]
enum ExpectedScope {
Paragraph,
Control(usize),
}
fn expected_texts(
tx: &Transaction<'_>,
exact: bool,
) -> Result<Vec<(usize, ExpectedText)>, EditError> {
type Spans = (String, Vec<(usize, usize, String, usize, bool, bool)>);
let mut edits: BTreeMap<usize, Spans> = BTreeMap::new();
let mut merges = Vec::new();
let mut filled: HashSet<usize> = HashSet::new();
let mut fills = Vec::new();
for (op, resolved) in &tx.resolved {
match resolved {
Resolved::Text {
para,
start,
end,
replacement,
attach_before,
..
} => {
let entry = edits
.entry(*para)
.or_insert_with(|| (tx.outcomes[*op].id.clone(), Vec::new()));
entry.1.push((
*start,
*end,
replacement.clone(),
*op,
false,
*attach_before,
));
}
Resolved::Restore { para, at, text, .. } => {
let entry = edits
.entry(*para)
.or_insert_with(|| (tx.outcomes[*op].id.clone(), Vec::new()));
entry.1.push((*at, *at, text.clone(), *op, true, false));
}
Resolved::MergeParagraphs {
para,
next,
separator,
..
} => {
merges.push((*para, *next, separator.clone(), tx.outcomes[*op].id.clone()));
}
Resolved::FillControl {
control,
paragraphs,
value,
..
} => {
let text = match value {
controls::FillValue::Text(text) | controls::FillValue::Date { text, .. } => {
text.clone()
}
controls::FillValue::Checked { glyph, .. } => glyph.to_string(),
};
for para in paragraphs {
fills.push((*para, *control, text.clone(), tx.outcomes[*op].id.clone()));
}
filled.extend(paragraphs.iter().copied());
}
_ => {}
}
}
let mut expected = BTreeMap::new();
for (para, (op, mut spans)) in edits {
let rewrite = if exact { rewrite_text(tx, para) } else { None };
let rewrite_op = rewrite.as_ref().map(|&(op, _)| op);
let base = &tx.projections[para].text;
let empty = base.is_empty();
spans.sort_by_key(|&(start, end, _, op, restore, after)| {
(start, !restore, end, empty && after, op)
});
let mut text = String::with_capacity(base.len());
let mut own = String::with_capacity(base.len());
let mut at = 0;
let mut own_at = 0;
let mut previous_op = None;
for (start, end, replacement, span_op, ..) in spans {
let conflict = || {
let previous = previous_op.unwrap_or(span_op);
tx.conflict(
span_op,
&format!(
"{} and {} have text ranges that cannot be composed in p{para}",
tx.outcomes[previous].id, tx.outcomes[span_op].id
),
)
};
let gap = base.get(at..start).ok_or_else(conflict)?;
base.get(start..end).ok_or_else(conflict)?;
text.push_str(gap);
text.push_str(&replacement);
at = end;
if Some(span_op) == rewrite_op {
own.push_str(base.get(own_at..start).ok_or_else(conflict)?);
own.push_str(&replacement);
own_at = end;
}
previous_op = Some(span_op);
}
text.push_str(base.get(at..).ok_or_else(|| {
tx.conflict(
previous_op.unwrap_or(0),
"text range ends outside the paragraph",
)
})?);
own.push_str(base.get(own_at..).ok_or_else(|| {
tx.conflict(
rewrite_op.unwrap_or(0),
"rewrite range ends outside the paragraph",
)
})?);
if let Some((rewrite_op, requested)) = rewrite {
let entry = if own == requested {
ExpectedText {
op,
text,
ignore_symbols: true,
scope: ExpectedScope::Paragraph,
}
} else {
ExpectedText {
op: tx.outcomes[rewrite_op].id.clone(),
text: requested,
ignore_symbols: true,
scope: ExpectedScope::Paragraph,
}
};
expected.insert(para, entry);
continue;
}
expected.insert(
para,
ExpectedText {
op,
text,
ignore_symbols: false,
scope: ExpectedScope::Paragraph,
},
);
}
if exact {
for (i, op) in tx.plan.operations.iter().enumerate() {
let OperationKind::Rewrite {
paragraph, text, ..
} = &op.kind
else {
continue;
};
if comment_text_of(paragraph).is_some() {
continue;
}
let Some(para) = outcome_paragraph(tx, i) else {
continue;
};
expected.entry(para).or_insert_with(|| ExpectedText {
op: tx.outcomes[i].id.clone(),
text: text.clone(),
ignore_symbols: true,
scope: ExpectedScope::Paragraph,
});
}
}
for (para, next, separator, op) in merges {
let survivor = expected
.get(&next)
.map(|e| e.text.clone())
.unwrap_or_else(|| tx.projections[next].text.clone());
let first = expected
.get(¶)
.map(|e| e.text.clone())
.unwrap_or_else(|| tx.projections[para].text.clone());
expected.insert(
next,
ExpectedText {
op,
text: format!("{first}{separator}{survivor}"),
ignore_symbols: false,
scope: ExpectedScope::Paragraph,
},
);
expected.remove(¶);
}
for para in filled {
expected.remove(¶);
}
let mut out: Vec<(usize, ExpectedText)> = expected.into_iter().collect();
for (para, control, text, op) in fills {
out.push((
para,
ExpectedText {
op,
text,
ignore_symbols: false,
scope: ExpectedScope::Control(control),
},
));
}
out.sort_by_key(|(para, _)| *para);
Ok(out)
}
fn rewrite_text(tx: &Transaction<'_>, para: usize) -> Option<(usize, String)> {
tx.plan.operations.iter().enumerate().find_map(|(i, op)| {
let OperationKind::Rewrite {
paragraph, text, ..
} = &op.kind
else {
return None;
};
if comment_text_of(paragraph).is_some() {
return None;
}
(outcome_paragraph(tx, i) == Some(para)).then(|| (i, text.clone()))
})
}
fn outcome_paragraph(tx: &Transaction<'_>, op: usize) -> Option<usize> {
let id = tx.outcomes[op].paragraph.as_deref()?;
let (story, index) = crate::ids::paragraph_place(id)?;
let story = tx.stories.iter().position(|s| s.id == story)?;
tx.paragraph_story
.iter()
.position(|&(s, i)| s == story && i == index)
}
fn attached(tx: &Transaction<'_>, para: usize) -> bool {
let node = tx.paragraph_nodes[para];
let (story, _) = tx.paragraph_story[para];
let root = tx.stories[story].root;
node == root || tx.opened.dom.ancestors(node, None).contains(&root)
}
pub(super) fn check_dom_realized(tx: &Transaction<'_>, exact: bool) -> Result<(), EditError> {
for (para, expected) in expected_texts(tx, exact)? {
if !attached(tx, para) {
continue;
}
let realized = match expected.scope {
ExpectedScope::Paragraph => {
project_paragraph(&tx.opened.dom, tx.paragraph_nodes[para]).text
}
ExpectedScope::Control(control) => control_text(&tx.opened.dom, tx.controls[control]),
};
let (want, got) = if expected.ignore_symbols {
(
expected.text.replace('\u{FFFC}', ""),
realized.replace('\u{FFFC}', ""),
)
} else {
(expected.text.clone(), realized)
};
if want != got {
let mut e = err(
codes::POSTCONDITION_FAILED,
Some(&expected.op),
format!(
"the requested text was not realized in paragraph {}",
crate::ids::paragraph_id(
&tx.stories[tx.paragraph_story[para].0].id,
tx.paragraph_story[para].1,
),
),
);
e.outcomes = tx.outcomes.clone();
return Err(e);
}
}
Ok(())
}
fn control_text(dom: &crate::xmllinq::Dom, sdt: crate::xmllinq::NodeId) -> String {
dom.element(sdt, &crate::namespaces::W::sdt_content())
.map(|content| project_paragraph(dom, content).text)
.unwrap_or_default()
}
pub(super) fn check_serialized_stories(
tx: &Transaction<'_>,
clean: &[u8],
) -> Result<(), EditError> {
let post = body_paragraph_nodes(&tx.opened.dom, tx.opened.body);
let reopened = crate::inspect::paragraphs(clean)
.map_err(|e| err("POSTCONDITION_FAILED", None, e.to_string()))?;
if reopened.len() != post.len() {
return Err(err(
codes::POSTCONDITION_FAILED,
None,
format!(
"the serialized candidate holds {} body paragraphs, the prepared document {}",
reopened.len(),
post.len(),
),
));
}
for (node, paragraph) in post.iter().zip(&reopened) {
let expected = project_paragraph(&tx.opened.dom, *node).text;
if expected != paragraph.text {
return Err(err(
codes::POSTCONDITION_FAILED,
None,
format!(
"the serialized candidate's paragraph {} reads differently than prepared",
paragraph.id,
),
));
}
}
let written: Vec<usize> = tx
.written_stories
.iter()
.copied()
.filter(|&story| story != 0)
.collect();
if written.is_empty() {
return Ok(());
}
let reopened = crate::inspect::stories(clean)
.map_err(|e| err("POSTCONDITION_FAILED", None, e.to_string()))?;
for story in written {
let StoryPart { part, root, .. } = &tx.stories[story];
let post = crate::inspect::story_paragraph_nodes(&tx.opened.dom, *root);
let Some(reopened) = reopened.iter().find(|s| &s.part == part) else {
return Err(err(
codes::POSTCONDITION_FAILED,
None,
format!("the serialized candidate lost the {part} story"),
));
};
if reopened.paragraphs.len() != post.len() {
return Err(err(
codes::POSTCONDITION_FAILED,
None,
format!(
"the serialized candidate holds {} {part} paragraphs, the prepared document {}",
reopened.paragraphs.len(),
post.len(),
),
));
}
for (node, paragraph) in post.iter().zip(&reopened.paragraphs) {
let expected = project_paragraph(&tx.opened.dom, *node).text;
if expected != paragraph.text {
return Err(err(
codes::POSTCONDITION_FAILED,
None,
format!(
"the serialized candidate's paragraph {} reads differently than prepared",
paragraph.id,
),
));
}
}
}
Ok(())
}
fn check_redline_stories(
tx: &Transaction<'_>,
clean: &[u8],
redline: &[u8],
) -> Result<(), EditError> {
fn compare(
part: &str,
clean: &[crate::inspect::Paragraph],
redline: &[crate::inspect::Paragraph],
) -> Result<(), EditError> {
if clean.len() != redline.len() {
return Err(err(
codes::POSTCONDITION_FAILED,
None,
format!(
"the accepted redline's {part} paragraph {} differs: {} paragraphs, the clean candidate {}",
clean.len().min(redline.len()),
redline.len(),
clean.len(),
),
));
}
for (expected, actual) in clean.iter().zip(redline) {
if expected.text != actual.text {
return Err(err(
codes::POSTCONDITION_FAILED,
None,
format!(
"the accepted redline's {part} paragraph {} reads differently than the clean candidate",
expected.id,
),
));
}
}
Ok(())
}
let accept = |bytes: &[u8]| {
crate::document_comparer::accept_revisions(bytes)
.map_err(|e| err(codes::POSTCONDITION_FAILED, None, e.to_string()))
};
let clean = accept(clean)?;
let redline = accept(redline)?;
let paragraphs = |bytes: &[u8]| {
crate::inspect::paragraphs(bytes)
.map_err(|e| err(codes::POSTCONDITION_FAILED, None, e.to_string()))
};
compare(
&tx.opened.main,
¶graphs(&clean)?,
¶graphs(&redline)?,
)?;
if tx.written_stories.iter().all(|&story| story == 0) {
return Ok(());
}
let stories = |bytes: &[u8]| {
crate::inspect::stories(bytes)
.map_err(|e| err(codes::POSTCONDITION_FAILED, None, e.to_string()))
};
let clean = stories(&clean)?;
let redline = stories(&redline)?;
for &story in &tx.written_stories {
if story == 0 {
continue;
}
let part = &tx.stories[story].part;
let expected = clean.iter().find(|s| &s.part == part);
let actual = redline.iter().find(|s| &s.part == part);
let (Some(expected), Some(actual)) = (expected, actual) else {
return Err(err(
codes::POSTCONDITION_FAILED,
None,
format!("the accepted candidates lost the {part} story at paragraph 0"),
));
};
compare(part, &expected.paragraphs, &actual.paragraphs)?;
}
Ok(())
}
fn produce(
tx: &mut Transaction<'_>,
emit: bool,
role: PrimaryRole,
) -> Result<EditResult, EditError> {
let plan = tx.plan;
let (clean, marked) = tx.finish()?;
check_serialized_stories(tx, &clean)?;
if plan.existing_revisions == ExistingRevisions::Keep
|| (emit
&& (tx.edits_only_comments()
|| matches!(tx.commented_base()?, std::borrow::Cow::Borrowed(_))))
{
let result = tracked::result(tx, clean)?;
tx.check_redactions(&[&result.clean, &result.redline])?;
if role == PrimaryRole::Redline {
check_redline_stories(tx, &result.clean, &result.redline)?;
}
return Ok(result);
}
#[cfg(feature = "render")]
let (mut clean, mut marked) = (clean, marked);
#[cfg(feature = "render")]
let mut fields = Vec::new();
#[cfg(feature = "render")]
if plan.update_fields {
let refresh = |bytes: &[u8]| {
crate::fields::update_fields(bytes)
.map_err(|e| err("FIELDS_FAILED", None, e.to_string()))
};
let updated = refresh(&clean)?;
clean = updated.docx;
fields = updated.fields;
if let Some(bytes) = &marked {
marked = Some(refresh(bytes)?.docx);
}
}
let settings = WmlComparerSettings {
author_for_revisions: plan.author.clone(),
date_time_for_revisions: tx.date.clone(),
preserve_untouched: true,
..WmlComparerSettings::default()
};
let revised = marked.as_deref().unwrap_or(&clean);
let base = tx.commented_base()?;
let mut redline =
crate::document_comparer::compare_documents_for_edit(&base, revised, &settings)
.map_err(|e| err("COMPARE_FAILED", None, e.to_string()))?;
if tx
.resolved
.iter()
.any(|(_, r)| matches!(r, Resolved::PageSetup { .. }))
{
redline = sections::record_mid_changes(&redline, &base, &plan.author, &tx.date)?;
}
redline = super::ranges::restore(&redline, &base, revised, &plan.author)?;
if marked.is_some() {
let (rewritten, fallbacks) = whole::rewrite(
&redline,
(&base, revised),
&tx.whole_marks,
&plan.author,
&tx.date,
)?;
redline = rewritten;
for (op, reason) in fallbacks {
tx.outcomes[op].message = Some(format!("shown as a word-level diff: {reason}"));
}
}
if let Some(&(_, op)) = tx
.deletion_comments
.iter()
.find(|&&(id, _)| comment_holds_kept_text(&redline, id))
{
let keep = match tx.syntax {
super::PlanSyntax::Flags | super::PlanSyntax::Typed => "--existing-revisions keep",
super::PlanSyntax::V1 | super::PlanSyntax::V2 => r#""existing_revisions": "keep""#,
};
let message = match plan.operations[op].kind {
OperationKind::Delete { .. } => format!(
"the comparer deleted the same words elsewhere instead, so the comment would sit on text that stays; pass {keep} to write the deletion as given, or delete without a comment"
),
OperationKind::ReplyComment { .. } => format!(
"the comparer deleted an identical paragraph instead, so the reply would sit on text that stays; pass {keep} to write the deletion as given"
),
_ => "the comparer deleted an identical paragraph instead, so the comment would sit on text that stays; delete without a comment, or comment on a neighbouring paragraph".to_string(),
};
let mut error = tx.conflict(op, &message);
error.code = "UNSUPPORTED_STRUCTURE".into();
error.outcomes[op].code = Some(error.code.clone());
return Err(error);
}
if let Some((op, message)) = identical_paragraph_kept(tx, &redline)? {
let mut error = tx.conflict(op, &message);
error.code = "UNSUPPORTED_STRUCTURE".into();
error.outcomes[op].code = Some(error.code.clone());
return Err(error);
}
let lifted: Vec<u32> = tx
.deletion_comments
.iter()
.filter(|&&(_, op)| matches!(plan.operations[op].kind, OperationKind::Delete { .. }))
.map(|&(id, _)| id)
.collect();
redline = lift_deletion_comments(&redline, &lifted)?;
redline = enclose_deleted_paragraph_comment_references(&redline, &plan.author)?;
if let Some(request) = &tx.settings {
redline = crate::settings::apply_settings_to_docx(&redline, request)
.map_err(|m| err("PACKAGE_WRITE", None, m))?;
}
redline = super::replacement_order::typing_order(&redline)?;
tx.check_redactions(&[&clean, &redline])?;
let mut report = tx.report(true);
report.paragraphs.to = crate::inspect::paragraphs(&clean)
.map(|p| p.len())
.unwrap_or(report.paragraphs.from);
report.revisions = revision_counts(&redline);
#[cfg(feature = "render")]
{
report.fields = fields;
}
report.clean_removed_comments = crate::comment_settlement::threads_the_clean_copy_drops(
&clean,
&redline,
&report.removed_comments,
&tx.deleted_paragraph_ids(),
)?;
if role == PrimaryRole::Redline {
check_redline_stories(tx, &clean, &redline)?;
}
Ok(EditResult {
clean,
redline,
report,
})
}
fn identical_paragraph_kept(
tx: &Transaction<'_>,
redline: &[u8],
) -> Result<Option<(usize, String)>, EditError> {
let deletions: Vec<(usize, usize)> = tx
.resolved
.iter()
.filter_map(|(op, resolved)| match resolved {
Resolved::DeleteParagraph { para } => Some((*op, *para)),
_ => None,
})
.collect();
if deletions.is_empty() {
return Ok(None);
}
let opened = crate::inspect::Opened::open(redline)
.map_err(|error| err("PACKAGE_WRITE", None, error.to_string()))?;
let mut texts: HashMap<usize, Vec<String>> = HashMap::new();
for (op, para) in deletions {
let source = match tx.projections.get(para) {
Some(projection) if !projection.text.is_empty() => projection.text.as_str(),
_ => continue,
};
let Some(&(story, local)) = tx.paragraph_story.get(para) else {
continue;
};
if let std::collections::hash_map::Entry::Vacant(slot) = texts.entry(story) {
slot.insert(visible_paragraphs(&opened, tx, story)?);
}
let still_there = texts
.get(&story)
.and_then(|paragraphs| paragraphs.get(local))
.is_some_and(|text| text == source);
if !still_there {
continue;
}
let keep = match tx.syntax {
super::PlanSyntax::Flags | super::PlanSyntax::Typed => "--existing-revisions keep",
super::PlanSyntax::V1 | super::PlanSyntax::V2 => r#""existing_revisions": "keep""#,
};
return Ok(Some((
op,
format!(
"the comparer deleted an identical paragraph instead of the one named; pass {keep} to write the deletion as given"
),
)));
}
Ok(None)
}
fn visible_paragraphs(
opened: &crate::inspect::Opened,
tx: &Transaction<'_>,
story: usize,
) -> Result<Vec<String>, EditError> {
if story == 0 {
return Ok(body_paragraph_nodes(&opened.dom, opened.body)
.into_iter()
.map(|paragraph| project_paragraph(&opened.dom, paragraph).text)
.collect());
}
let Some(part) = tx.stories.get(story).map(|story| story.part.clone()) else {
return Ok(Vec::new());
};
if opened.pkg.part_bytes(&part).is_none() {
return Ok(Vec::new());
}
let (dom, _, root) = crate::inspect::parse_part(&opened.pkg, &part)
.map_err(|error| err("PACKAGE_WRITE", None, error.to_string()))?;
Ok(crate::inspect::story_paragraph_nodes(&dom, root)
.into_iter()
.map(|paragraph| project_paragraph(&dom, paragraph).text)
.collect())
}
fn err(code: &str, operation: Option<&str>, message: impl Into<String>) -> EditError {
super::err(code, operation, message)
}
pub fn prepare_v2(
source: &[u8],
plan: &super::v2::PlanV2,
options: &PrepareOptions,
) -> Result<PreparedEdit, EditError> {
let known = SourceChecks::default();
prepare_v2_once(source, plan, options, &known).map_err(|failure| {
let mut reruns = Reruns::new(|ops: &[usize]| {
let part = super::v2::PlanV2 {
operations: ops.iter().map(|&i| plan.operations[i].clone()).collect(),
..plan.clone()
};
prepare_v2_once(source, &part, options, &known)
.err()
.and_then(|f| f.refusal)
.map(|(refusals, _)| refusals)
})
.knowing(plan.operations.len(), &failure);
attributed(*failure, plan.operations.len(), false, &mut reruns)
})
}
fn prepare_v2_once(
source: &[u8],
plan: &super::v2::PlanV2,
options: &PrepareOptions,
known: &SourceChecks,
) -> Result<PreparedEdit, Box<Failure>> {
let syntax = if options.syntax == super::PlanSyntax::Typed {
super::PlanSyntax::Typed
} else {
super::PlanSyntax::V2
};
let applied = super::v2_apply::apply_quoting(source, plan, syntax)?;
let result = EditResult {
clean: applied.clean,
redline: applied.redline,
report: applied.report,
};
let primary = result.redline.clone();
let unchanged = primary == source;
let effect = if unchanged {
Effect::Unchanged
} else {
Effect::Changed
};
let mut checks = screen_candidates(source, &result, known).map_err(|refusals| {
Box::new(Failure {
error: refusals[0].error.clone(),
refusal: Some((refusals, Vec::new())),
advice: None,
})
})?;
checks.operation_postconditions = true;
if options.require_change && unchanged {
return Err(err(
codes::NO_CHANGES_REQUESTED,
None,
"the primary transaction has no document effect; --require-change was given",
)
.into());
}
Ok(PreparedEdit {
source_sha256: source_sha256(source),
request_sha256: source_sha256(
serde_json::to_string(plan)
.expect("plan serializes")
.as_bytes(),
),
primary_sha256: source_sha256(&primary),
primary,
primary_bytes: if unchanged {
PrimaryBytes::Source
} else {
PrimaryBytes::Candidate
},
primary_effect: effect,
result,
checks,
correspondence: Some(applied.correspondence),
skipped: Vec::new(),
})
}
#[cfg(test)]
#[cfg_attr(all(coverage_nightly, test), coverage(off))]
pub(crate) mod tests {
use super::super::{
EditReport, EditResult, ExistingRevisions, ParagraphDelta, deeper_boundary_fixture,
};
use super::*;
fn result_of(clean: Vec<u8>) -> EditResult {
EditResult {
clean: clean.clone(),
redline: clean,
report: EditReport {
schema_version: 1,
ok: true,
source_sha256: String::new(),
base_sha256: String::new(),
guarded: false,
author: "T".into(),
date: "2026-10-17T12:00:00Z".into(),
existing_revisions: ExistingRevisions::Refuse,
resolved_revisions: Default::default(),
removed_comments: Vec::new(),
clean_removed_comments: Vec::new(),
kept_comments: Vec::new(),
paragraph_ids: Vec::new(),
split_revisions: Vec::new(),
paragraphs: ParagraphDelta::default(),
operations: Vec::new(),
comments_added: 0,
revisions: Default::default(),
#[cfg(feature = "render")]
fields: Vec::new(),
skipped: 0,
notes: Vec::new(),
output_sha256: None,
},
}
}
#[test]
fn unvalidatable_source_never_claims_findings_introduced() {
let orphan = deeper_boundary_fixture::docx(
r#"<w:p><w:r><w:t>x</w:t></w:r><w:r><w:commentReference w:id="0"/></w:r></w:p>"#,
);
let findings = crate::validate::validate(&orphan).unwrap();
assert!(
findings.iter().any(|f| f.code == "COMMENT_ANCHOR_ORPHAN"),
"{findings:?}"
);
let checks = check_candidates(b"not a zip at all", &result_of(orphan)).unwrap();
assert!(checks.introduced.is_empty(), "{:?}", checks.introduced);
assert!(checks.baseline.is_empty());
}
fn revisions(records: &[(&str, u32)]) -> Vec<u8> {
let body: String = records
.iter()
.map(|&(kind, id)| match kind {
"customXmlInsRangeStart" => format!(
r#"<w:customXmlInsRangeStart w:id="{id}" w:author="A" w:date="2026-10-17T09:00:00Z"/><w:r><w:t>w</w:t></w:r><w:customXmlInsRangeEnd w:id="{id}"/>"#
),
"rPrChange" => format!(
r#"<w:r><w:rPr><w:b/><w:rPrChange w:id="{id}" w:author="A" w:date="2026-10-17T09:00:00Z"><w:rPr/></w:rPrChange></w:rPr><w:t>w</w:t></w:r>"#
),
_ => format!(
r#"<w:{kind} w:id="{id}" w:author="A" w:date="2026-10-17T09:00:00Z"><w:r><w:{t}>w</w:{t}></w:r></w:{kind}>"#,
t = if kind == "del" { "delText" } else { "t" }
),
})
.collect();
deeper_boundary_fixture::docx(&format!("<w:p>{body}</w:p>"))
}
fn gate(
source: &[(&str, u32)],
candidate: &[(&str, u32)],
) -> Result<CandidateChecks, EditError> {
check_candidates(&revisions(source), &result_of(revisions(candidate)))
}
fn refused_as_duplicate(result: Result<CandidateChecks, EditError>, record: &str, part: &str) {
let error = result.unwrap_err();
assert_eq!(error.code, "CANDIDATE_INVALID", "{error:?}");
assert!(
error
.message
.contains("introduces a duplicate revision id in the clean copy: ")
&& error.message.contains(&format!(": {record} occurs "))
&& error.message.contains(&format!(" times in {part} (")),
"{error:?}"
);
}
#[test]
fn a_candidate_may_not_introduce_a_duplicate_revision_id() {
refused_as_duplicate(
gate(&[("ins", 5), ("ins", 6)], &[("ins", 5), ("ins", 5)]),
"w:ins w:id 5",
"word/document.xml",
);
refused_as_duplicate(
gate(&[("rPrChange", 5)], &[("rPrChange", 5), ("rPrChange", 5)]),
"w:rPrChange w:id 5",
"word/document.xml",
);
}
#[test]
fn a_candidate_may_not_add_an_occurrence_to_an_inherited_duplicate() {
refused_as_duplicate(
gate(
&[("ins", 5), ("ins", 5)],
&[("ins", 5), ("ins", 5), ("ins", 5)],
),
"w:ins w:id 5",
"word/document.xml",
);
}
#[test]
fn healing_one_duplicate_revision_id_hides_no_new_one() {
refused_as_duplicate(
gate(
&[("ins", 5), ("ins", 5)],
&[("ins", 5), ("ins", 7), ("ins", 7)],
),
"w:ins w:id 7",
"word/document.xml",
);
}
#[test]
fn a_custom_xml_range_start_may_not_take_a_duplicate_id() {
assert!(
gate(
&[("customXmlInsRangeStart", 5)],
&[("customXmlInsRangeStart", 5)]
)
.is_ok()
);
refused_as_duplicate(
gate(
&[("customXmlInsRangeStart", 5), ("customXmlInsRangeStart", 6)],
&[("customXmlInsRangeStart", 5), ("customXmlInsRangeStart", 5)],
),
"w:customXmlInsRangeEnd w:id 5",
"word/document.xml",
);
}
fn strict(bytes: &[u8]) -> Vec<u8> {
let mut pkg = crate::opc::PartFs::open(bytes).unwrap();
let xml = pkg.part_string("word/document.xml").unwrap().replace(
"http://schemas.openxmlformats.org/wordprocessingml/2006/main",
"http://purl.oclc.org/ooxml/wordprocessingml/main",
);
pkg.set_part("word/document.xml", xml.into_bytes());
pkg.to_zip().unwrap()
}
#[test]
fn a_strict_sources_duplicates_are_inherited() {
let source = strict(&revisions(&[("ins", 5), ("ins", 5)]));
let candidate = revisions(&[("ins", 5), ("ins", 5)]);
let result = check_candidates(&source, &result_of(candidate));
assert!(result.is_ok(), "{result:?}");
}
fn main_named(bytes: &[u8], name: &str) -> Vec<u8> {
const OFFICE_DOCUMENT: &str =
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
let mut pkg = crate::opc::PartFs::open(bytes).unwrap();
let xml = pkg.part_bytes("word/document.xml").unwrap().to_vec();
pkg.remove_part("word/document.xml");
pkg.set_part(name, xml);
pkg.add_content_type_override(
&format!("/{name}"),
"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml",
);
pkg.remove_package_relationships_by_type(OFFICE_DOCUMENT);
pkg.add_package_relationship(OFFICE_DOCUMENT, name);
pkg.to_zip().unwrap()
}
#[test]
fn a_main_part_without_an_xml_extension_is_gated_too() {
let source = main_named(&revisions(&[("ins", 5), ("ins", 6)]), "word/main.dat");
let candidate = main_named(&revisions(&[("ins", 5), ("ins", 5)]), "word/main.dat");
refused_as_duplicate(
check_candidates(&source, &result_of(candidate)),
"w:ins w:id 5",
"word/main.dat",
);
}
#[test]
fn a_strict_main_part_without_an_xml_extension_is_read_as_transitional() {
let source = main_named(
&strict(&revisions(&[("ins", 5), ("ins", 5)])),
"word/main.dat",
);
let candidate = main_named(&revisions(&[("ins", 5), ("ins", 5)]), "word/main.dat");
let result = check_candidates(&source, &result_of(candidate));
assert!(result.is_ok(), "{result:?}");
}
fn prepared(source: &[u8], operation: &str) -> Result<PreparedEdit, EditError> {
let json = format!(
r#"{{"schema_version":1,"author":"Bob","date":"2026-10-17T12:00:00Z","existing_revisions":"keep","operations":[{operation}]}}"#
);
let plan = EditPlan::from_json(&json).unwrap();
prepare(source, &plan, &PrepareOptions::default())
}
#[test]
fn a_paragraph_marks_revision_id_survives_paragraph_edits() {
let source = deeper_boundary_fixture::docx(
r#"<w:p><w:pPr><w:rPr><w:ins w:id="7" w:author="Ann" w:date="2026-09-01T00:00:00Z"/></w:rPr></w:pPr><w:r><w:t xml:space="preserve">abcd efgh</w:t></w:r></w:p><w:p><w:r><w:t>next</w:t></w:r></w:p>"#,
);
for operation in [
r#"{"kind":"insert_paragraph","paragraph":{"index":1},"runs":[{"text":"b"}],"like":{"index":0}}"#,
r#"{"kind":"insert","paragraph":"p0","after":"abcd","text":"\nnew para"}"#,
r#"{"kind":"replace","paragraph":"p0","find":"cd","replacement":"X"}"#,
] {
let result = prepared(&source, operation);
assert!(result.is_ok(), "{operation}: {:?}", result.err());
}
}
fn culprits_of(
count: usize,
together: bool,
causes: &[&[usize]],
) -> (Option<Vec<usize>>, usize) {
let mut reruns = 0;
let found = culprits(count, together, |ops| {
reruns += 1;
causes
.iter()
.any(|cause| cause.iter().all(|i| ops.contains(i)))
});
(found, reruns)
}
#[test]
fn culprits_finds_one_operation_in_logarithmic_reruns() {
for at in 0..64 {
let (found, reruns) = culprits_of(64, true, &[&[at]]);
assert_eq!(found, Some(vec![at]));
assert!(reruns <= 8, "{at}: {reruns}");
}
assert_eq!(culprits_of(1, true, &[&[0]]).0, Some(vec![0]));
}
#[test]
fn culprits_names_the_operations_a_refusal_needs() {
assert_eq!(culprits_of(10, true, &[&[2, 7]]).0, Some(vec![7, 2]));
assert_eq!(culprits_of(10, true, &[&[5, 1, 8]]).0, Some(vec![8, 1, 5]));
assert_eq!(culprits_of(2, true, &[&[0, 1]]).0, Some(vec![1, 0]));
assert_eq!(culprits_of(10, true, &[&[6], &[3]]).0, Some(vec![3]));
assert_eq!(culprits_of(10, false, &[&[2, 7]]).0, Some(vec![7]));
}
#[test]
fn culprits_seeks_each_further_operation_before_the_last_found() {
let (found, reruns) = culprits_of(64, true, &[&[0, 1, 2, 3, 63]]);
assert_eq!(found, Some(vec![63, 0, 1, 2, 3]));
assert!(reruns <= 21, "{reruns}");
let (found, reruns) = culprits_of(64, true, &[&[1, 3, 63]]);
assert_eq!(found, Some(vec![63, 1, 3]));
assert!(reruns <= 18, "{reruns}");
}
#[test]
fn a_refusal_without_operations_names_none() {
assert_eq!(culprits_of(5, true, &[&[]]).0, None);
assert_eq!(culprits_of(0, true, &[]).0, None);
}
#[test]
fn attribution_leaves_other_failures_alone() {
let error = err("OVERLAPPING_EDITS", Some("op-2"), "two edits");
let mut never = Reruns::new(|_: &[usize]| -> Option<Refused> {
panic!("no rerun for a failure the candidate checks did not raise")
});
let kept = attributed(*Box::<Failure>::from(error.clone()), 3, true, &mut never);
assert_eq!(kept, error);
let refusal = refused(
"redline",
"BOOKMARK_CROSSES_REVISION",
"word/document.xml",
"Clause".into(),
"the edit introduces it".into(),
);
let failure = Box::new(Failure {
error: refusal.error.clone(),
refusal: Some((vec![refusal.clone()], Vec::new())),
advice: Some("drop update_fields"),
});
let mut always = Reruns::new(|_: &[usize]| Some(vec![refusal.clone()]));
let kept = attributed(*failure, 3, true, &mut always);
assert_eq!(kept.operation, None);
assert_eq!(kept.message, "the edit introduces it; drop update_fields");
}
#[test]
fn named_operations_come_before_the_advice() {
let refusal = refused(
"redline",
"RANGE_LOST",
"",
"bookmark B".into(),
"Reject All drops bookmark B".into(),
);
let outcome = |id: &str, kind: &str, paragraph: Option<&str>| EditOutcome {
id: id.into(),
kind: kind.into(),
status: String::new(),
paragraph: paragraph.map(str::to_string),
matches: 0,
context: None,
comment_id: None,
anchor_given: None,
anchor_read_as: None,
near: None,
candidates: Vec::new(),
code: None,
message: None,
};
let failure = Box::new(Failure {
error: refusal.error.clone(),
refusal: Some((
vec![refusal.clone()],
vec![
outcome("op-1", "replace", Some("p0")),
outcome("op-2", "insert", Some("p4")),
outcome("op-3", "page_setup", None),
],
)),
advice: Some("drop update_fields"),
});
let mut reruns = Reruns::new(|ops: &[usize]| {
(ops.contains(&0) && ops.contains(&2)).then(|| vec![refusal.clone()])
});
let named = attributed(*failure, 3, true, &mut reruns);
assert_eq!(named.operation.as_deref(), Some("op-3"));
assert_eq!(
named.message,
"Reject All drops bookmark B; op-3 (page_setup) introduces it, together with op-1 (replace on p0); drop update_fields"
);
}
#[test]
fn a_rerun_reproduces_the_defect_not_its_code() {
let defect = |subject: &str| {
refused(
"redline",
"DUPLICATE_REVISION_ID",
"word/document.xml",
subject.into(),
format!("{subject} repeats"),
)
};
let mut runs = 0;
let mut reruns = Reruns::new(|ops: &[usize]| {
runs += 1;
let mut met = Vec::new();
if ops.contains(&0) {
met.push(defect("w:ins w:id 9"));
}
if ops.contains(&1) {
met.push(defect("w:ins w:id 7"));
}
(!met.is_empty()).then_some(met)
});
let seven = defect("w:ins w:id 7");
assert!(!reruns.reproduces(&[0], &seven));
assert!(reruns.reproduces(&[1], &seven));
assert!(reruns.reproduces(&[0, 1], &seven));
assert!(!reruns.reproduces(&[0], &seven));
drop(reruns);
assert_eq!(runs, 3);
}
#[test]
fn a_run_marks_revision_id_split_in_two_is_refused() {
let source = deeper_boundary_fixture::docx(
r#"<w:p><w:r><w:rPr><w:ins w:id="7" w:author="Ann" w:date="2026-09-01T00:00:00Z"/></w:rPr><w:t>abcd</w:t></w:r></w:p>"#,
);
let error = prepared(
&source,
r#"{"kind":"replace","paragraph":"p0","find":"bc","replacement":"X"}"#,
)
.err()
.unwrap();
assert_eq!(error.code, "CANDIDATE_INVALID", "{error:?}");
assert!(
error.message.contains("w:ins on a mark w:id 7"),
"{error:?}"
);
}
#[test]
fn a_duplicate_only_the_redline_carries_is_refused_by_its_artifact() {
let source = revisions(&[("ins", 5), ("ins", 6)]);
let mut result = result_of(revisions(&[("ins", 5), ("ins", 6)]));
result.redline = revisions(&[("ins", 5), ("ins", 5)]);
let error = check_candidates(&source, &result).unwrap_err();
assert_eq!(error.code, "CANDIDATE_INVALID", "{error:?}");
assert_eq!(
error.message,
"the edit introduces a duplicate revision id in the redline: w:ins w:id 5 occurs 2 times in word/document.xml (the source has 1)"
);
}
#[test]
fn an_unreadable_source_skips_the_duplicate_gate() {
let candidate = revisions(&[("ins", 5), ("ins", 5)]);
let result = check_candidates(b"not a zip at all", &result_of(candidate));
assert!(result.is_ok(), "{result:?}");
}
#[test]
fn inherited_duplicates_and_ids_shared_across_kinds_publish() {
let inherited = [("ins", 5), ("ins", 5)];
assert!(gate(&inherited, &inherited).is_ok());
assert!(gate(&inherited, &[("ins", 5)]).is_ok());
let shared = [("ins", 5), ("del", 5), ("rPrChange", 5)];
assert!(gate(&[("ins", 5)], &shared).is_ok());
}
#[test]
fn verification_reports_only_completed_candidate_checks() {
let skipped = CandidateChecks::default().verification();
assert_eq!(skipped.integrity, "skipped");
assert_eq!(skipped.operation_postconditions, "skipped");
let source = deeper_boundary_fixture::docx("<w:p><w:r><w:t>old</w:t></w:r></w:p>");
let plan = EditPlan::from_json(
r#"{"schema_version":1,"author":"T","date":"2026-10-17T12:00:00Z","operations":[]}"#,
)
.unwrap();
let checked = prepare(&source, &plan, &PrepareOptions::default())
.unwrap()
.checks
.verification();
assert_eq!(checked.integrity, "passed");
assert_eq!(checked.operation_postconditions, "passed");
}
#[test]
fn redline_only_defects_name_the_story_and_paragraph() {
use deeper_boundary_fixture::{Part, R_NS, W_NS, docx_with_sect, para};
let kinds = ["header", "footer", "footnotes", "endnotes"];
let names = [
"word/header1.xml",
"word/footer1.xml",
"word/footnotes.xml",
"word/endnotes.xml",
];
let roots = ["hdr", "ftr", "footnotes", "endnotes"];
let xmls: Vec<_> = roots
.iter()
.map(|root| {
let text = para("old");
let content = match *root {
"footnotes" => format!(r#"<w:footnote w:id="1">{text}</w:footnote>"#),
"endnotes" => format!(r#"<w:endnote w:id="1">{text}</w:endnote>"#),
_ => text,
};
format!(r#"<w:{root} xmlns:w="{W_NS}">{content}</w:{root}>"#)
})
.collect();
let rels: Vec<_> = kinds.iter().map(|k| format!("{R_NS}/{k}")).collect();
let types: Vec<_> = kinds
.iter()
.map(|k| {
format!("application/vnd.openxmlformats-officedocument.wordprocessingml.{k}+xml")
})
.collect();
let parts: Vec<_> = (0..kinds.len())
.map(|i| Part {
name: names[i],
content_type: &types[i],
rel_type: &rels[i],
xml: &xmls[i],
})
.collect();
let source = docx_with_sect(
&(para("old")
+ r#"<w:p><w:r><w:footnoteReference w:id="1"/><w:endnoteReference w:id="1"/></w:r></w:p>"#),
&parts,
r#"<w:headerReference w:type="default" r:id="rIdX0"/><w:footerReference w:type="default" r:id="rIdX1"/>"#,
);
let mut operations = vec![
serde_json::json!({"kind":"replace", "paragraph":"p0", "find":"old", "replacement":"new"}),
];
for story in crate::inspect::stories(&source).unwrap() {
operations.push(serde_json::json!({"kind":"replace", "paragraph":story.paragraphs[0].id, "find":"old", "replacement":"new"}));
}
let plan = EditPlan::from_json(&serde_json::json!({
"schema_version":1, "author":"T", "date":"2026-10-17T12:00:00Z", "operations":operations,
}).to_string()).unwrap();
let mut tx = Transaction::start(&source, &plan).unwrap();
tx.resolve().unwrap();
tx.apply().unwrap();
let (clean, _) = tx.finish().unwrap();
assert!(check_redline_stories(&tx, &clean, &clean).is_ok());
let mut failures = Vec::new();
for part in std::iter::once("word/document.xml").chain(names) {
for defect in ["text", "count", "missing"] {
if part == "word/document.xml" && defect == "missing" {
continue;
}
let mut pkg = crate::opc::PartFs::open(&clean).unwrap();
let xml = pkg.part_string(part).unwrap();
if defect == "missing" {
pkg.remove_related_part(
"word/document.xml",
&format!("rIdX{}", names.iter().position(|p| *p == part).unwrap()),
);
} else {
let broken = if defect == "text" {
xml.replace("new", "WRONG")
} else {
xml.replace("</w:p>", "</w:p><w:p/>")
};
pkg.set_part(part, broken.into_bytes());
}
let redline = pkg.to_zip().unwrap();
match check_redline_stories(&tx, &clean, &redline) {
Err(e)
if e.code == codes::POSTCONDITION_FAILED
&& e.message.contains(part)
&& e.message.contains("paragraph") => {}
outcome => failures.push(format!("{part} {defect}: {outcome:?}")),
}
}
}
assert!(failures.is_empty(), "{}", failures.join("\n"));
}
thread_local! {
pub(crate) static PREPARATIONS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}
#[test]
fn skip_failed_attributes_every_defect_of_a_refusal_at_once() {
let n = 16;
let body: String = (0..n)
.map(|i| {
format!(
r#"<w:p><w:r><w:rPr><w:ins w:id="{}" w:author="Ann" w:date="2026-09-01T00:00:00Z"/></w:rPr><w:t>abcd</w:t></w:r></w:p>"#,
100 + i
)
})
.collect();
let source = deeper_boundary_fixture::docx(&body);
let operations: Vec<String> = (0..n)
.map(|i| {
format!(r#"{{"kind":"replace","paragraph":"p{i}","find":"bc","replacement":"X"}}"#)
})
.collect();
let plan = EditPlan::from_json(&format!(
r#"{{"schema_version":1,"author":"Bob","date":"2026-10-17T12:00:00Z","existing_revisions":"keep","operations":[{}]}}"#,
operations.join(",")
))
.unwrap();
PREPARATIONS.with(|count| count.set(0));
let options = PrepareOptions {
skip_failed: true,
..PrepareOptions::default()
};
let error = prepare(&source, &plan, &options).unwrap_err();
assert_eq!(error.code, "CANDIDATE_INVALID", "{error:?}");
let failed = error
.outcomes
.iter()
.filter(|o| o.status == "failed")
.count();
assert_eq!(failed, n, "{error:?}");
let preparations = PREPARATIONS.with(std::cell::Cell::get);
assert!(
preparations <= n + 1,
"{preparations} preparations for {n} operations"
);
}
#[test]
fn skip_failed_bisects_a_lone_defect() {
let n = 16;
let culprit = 11;
let body: String = (0..n)
.map(|i| {
let mark = if i == culprit {
r#"<w:rPr><w:ins w:id="100" w:author="Ann" w:date="2026-09-01T00:00:00Z"/></w:rPr>"#
} else {
""
};
format!(r#"<w:p><w:r>{mark}<w:t>abcd</w:t></w:r></w:p>"#)
})
.collect();
let source = deeper_boundary_fixture::docx(&body);
let operations: Vec<String> = (0..n)
.map(|i| {
format!(r#"{{"kind":"replace","paragraph":"p{i}","find":"bc","replacement":"X"}}"#)
})
.collect();
let plan = EditPlan::from_json(&format!(
r#"{{"schema_version":1,"author":"Bob","date":"2026-10-17T12:00:00Z","existing_revisions":"keep","operations":[{}]}}"#,
operations.join(",")
))
.unwrap();
PREPARATIONS.with(|count| count.set(0));
let options = PrepareOptions {
skip_failed: true,
..PrepareOptions::default()
};
let prepared = prepare(&source, &plan, &options).unwrap();
let skipped: Vec<usize> = prepared.skipped.iter().map(|s| s.index).collect();
assert_eq!(skipped, [culprit]);
assert!(
prepared.skipped[0]
.message
.contains("op-12 (replace on p11) introduces it"),
"{:?}",
prepared.skipped
);
let preparations = PREPARATIONS.with(std::cell::Cell::get);
assert!(preparations <= 8, "{preparations} preparations");
}
thread_local! {
static SERIALIZED_CANDIDATE: std::cell::RefCell<Option<Vec<u8>>> = const { std::cell::RefCell::new(None) };
}
pub(crate) fn serialized_candidate(bytes: Vec<u8>) -> Vec<u8> {
SERIALIZED_CANDIDATE.with(|candidate| candidate.borrow().clone().unwrap_or(bytes))
}
#[test]
fn review_11_prepare_v2_checks_serialized_text_like_v1() {
struct Reset;
impl Drop for Reset {
fn drop(&mut self) {
SERIALIZED_CANDIDATE.with(|c| *c.borrow_mut() = None);
}
}
let source = deeper_boundary_fixture::docx("<w:p><w:r><w:t>old</w:t></w:r></w:p>");
let legacy = EditPlan::from_json(r#"{"schema_version":1,"author":"T","date":"2026-10-17T12:00:00Z","existing_revisions":"keep","operations":[{"kind":"replace","paragraph":"p0","find":"old","replacement":"new"}]}"#).unwrap();
let view = super::super::for_edit::for_edit(
&source,
&super::super::for_edit::ForEditRequest {
all: true,
..Default::default()
},
)
.unwrap();
let target = &view.paragraphs[0].target;
let value = serde_json::json!({
"schema_version": 2, "source": {"sha256": source_sha256(&source)},
"author": "T", "date": "2026-10-17T12:00:00Z",
"policy": {"track": "off", "existing_revisions": "keep", "text_view":"final"},
"resolve_revisions":{"accept":[],"reject":[]}, "scope": {"kind":"all"},
"operations": [{"kind":"replace", "target": {"short_id":target.short_id,"long_id":target.long_id}, "find":"old", "text":"new"}]
});
let plan = super::super::v2::PlanV2::from_json(&value.to_string()).unwrap();
SERIALIZED_CANDIDATE.with(|c| {
*c.borrow_mut() = Some(deeper_boundary_fixture::docx(
"<w:p><w:r><w:t>wrong</w:t></w:r></w:p>",
));
});
let _reset = Reset;
let v1 = prepare(&source, &legacy, &PrepareOptions::default());
assert!(v1.is_err(), "v1 must detect the serializer disagreement");
let expected = v1.unwrap_err();
assert_eq!(expected.code, "POSTCONDITION_FAILED");
for track in [super::super::v2::Track::Off, super::super::v2::Track::On] {
let mut plan = plan.clone();
plan.policy.track = track;
let actual = prepare_v2(&source, &plan, &PrepareOptions::default());
assert!(
actual.is_err(),
"prepare_v2 must refuse the same serialized-text disagreement as prepare"
);
assert_eq!(actual.unwrap_err().code, expected.code);
}
}
}
#[cfg(test)]
#[cfg_attr(all(coverage_nightly, test), coverage(off))]
mod blame_branch_tests {
use super::*;
fn refusal(artifact: &'static str, code: &str, part: &str, subject: &str) -> Refusal {
Refusal {
artifact,
code: code.into(),
part: part.into(),
subject: subject.into(),
error: err("CANDIDATE_INVALID", None, "bad"),
}
}
#[test]
fn the_same_defect_matches_on_every_field_but_the_error() {
let base = refusal("redline", "X", "word/document.xml", "r1");
assert!(base.same(&refusal("redline", "X", "word/document.xml", "r1")));
for other in [
refusal("clean", "X", "word/document.xml", "r1"),
refusal("redline", "Y", "word/document.xml", "r1"),
refusal("redline", "X", "word/header1.xml", "r1"),
refusal("redline", "X", "word/document.xml", "r2"),
] {
assert!(!base.same(&other), "{other:?}");
}
}
fn outcome(id: &str, paragraph: Option<&str>) -> EditOutcome {
EditOutcome {
id: id.into(),
kind: "replace".into(),
status: "ok".into(),
paragraph: paragraph.map(Into::into),
matches: 1,
context: None,
comment_id: None,
anchor_given: None,
anchor_read_as: None,
near: None,
candidates: Vec::new(),
code: None,
message: None,
}
}
#[test]
fn naming_lists_every_operation_that_brings_the_defect() {
let outcomes = [
outcome("e1", Some("p0")),
outcome("e2", None),
outcome("e3", None),
];
let error = || err("CANDIDATE_INVALID", None, "broken");
assert_eq!(naming(error(), &[], &outcomes).message, "broken");
let one = naming(error(), &[0], &outcomes);
assert_eq!(one.operation.as_deref(), Some("e1"));
assert_eq!(one.message, "broken; e1 (replace on p0) introduces it");
let two = naming(error(), &[1, 0], &outcomes);
assert_eq!(
two.message,
"broken; e2 (replace) introduces it, together with e1 (replace on p0)"
);
let many = naming(error(), &[0, 1, 2, 7], &outcomes);
assert_eq!(
many.message,
"broken; e1 (replace on p0) introduces it, together with e2 (replace), e3 (replace) and /operations/7"
);
let unknown = naming(error(), &[9], &outcomes);
assert_eq!(unknown.operation.as_deref(), Some("/operations/9"));
}
}
#[cfg(test)]
#[cfg_attr(all(coverage_nightly, test), coverage(off))]
mod marker_branch_tests {
use super::super::deeper_boundary_fixture::docx;
use super::*;
#[test]
fn range_markers_skip_go_back_and_nameless_bookmarks() {
let source = docx(
r#"<w:p><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/><w:bookmarkStart w:id="1"/><w:bookmarkEnd w:id="1"/><w:bookmarkStart w:id="2" w:name="Kept"/><w:bookmarkEnd w:id="2"/><w:permStart w:id="3" w:edGrp="everyone"/><w:r><w:t>Text</w:t></w:r><w:permEnd w:id="3"/></w:p>"#,
);
let markers = range_markers(&source);
assert_eq!(
markers.bookmarks.into_iter().collect::<Vec<_>>(),
vec!["Kept"]
);
assert_eq!(markers.permissions, 1);
assert_eq!(markers.comments, 0);
let none = range_markers(b"not a package");
assert!(none.bookmarks.is_empty() && none.permissions == 0);
}
#[test]
fn a_location_without_a_step_moves_nowhere() {
let source = docx("<w:p><w:r><w:t>Text</w:t></w:r></w:p>");
let pkg = crate::opc::PartFs::open(&source).unwrap();
let (mut a, mut b) = (HashMap::new(), HashMap::new());
assert_eq!(
moved_location(
&pkg,
&pkg,
&mut a,
&mut b,
"word/document.xml",
"/w:document"
),
None
);
assert!(a.is_empty() && b.is_empty());
}
}