use serde::{Deserialize, Serialize};
use super::{
EditPlan, ExistingRevisions, HalfPoints, Operation, OperationKind, ResolveRevisions, RunFormat,
RunSpec, Selector, Side,
};
use crate::changes::ChangeFilter;
use crate::ids::{IdError, Location, ShortId};
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct FlagOp {
pub at: String,
pub anchor: Option<String>,
pub occurrence: Option<usize>,
pub content: Option<String>,
pub delete: bool,
pub resolve: bool,
pub accept: bool,
pub reject: bool,
pub strike: bool,
pub restore: bool,
pub visible: bool,
pub reply: Option<String>,
pub before: bool,
pub comment: bool,
pub styles: Vec<String>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Verb {
Edit,
Add,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FlagPlan {
pub plan: EditPlan,
pub notes: Vec<String>,
}
pub fn plan_from_flags(
verb: Verb,
ops: &[FlagOp],
author: &str,
date: Option<&str>,
existing: Option<ExistingRevisions>,
source: &[u8],
) -> Result<FlagPlan, String> {
if ops.is_empty() {
return Err("no operation: give -p WHERE with its flags, or --plan".into());
}
let mut operations = Vec::with_capacity(ops.len());
let mut notes = Vec::new();
let (mut accept, mut reject) = (Vec::new(), Vec::new());
let mut listed: Option<Vec<crate::changes::Change>> = None;
for op in ops {
if op.occurrence.is_some() && op.anchor.is_none() {
return Err(format!("-p {}: --occurrence requires --anchor", op.at));
}
if op.occurrence == Some(0) {
return Err(format!("-p {}: --occurrence is 1-based", op.at));
}
match revision_group(verb, op)? {
Group::Settle { accept: true, ids } => accept.extend(ids),
Group::Settle { accept: false, ids } => reject.extend(ids),
Group::Answer { how, ids, comment } => {
let revision = || crate::changes::composite_id(&ids);
let kinds = match how {
Answer::Strike => vec![OperationKind::Strike {
revision: revision(),
comment,
}],
Answer::Restore => vec![OperationKind::Restore {
revision: revision(),
comment,
}],
Answer::ByKind => {
let listed = listed.get_or_insert_with(|| {
crate::changes::list_changes(source).unwrap_or_default()
});
super::visible::reject_kinds(listed, &ids, comment)
}
};
operations.extend(kinds.into_iter().map(|kind| Operation {
id: None,
group: None,
kind,
}));
}
Group::Operation => {
let (kind, note) = flag_operation(verb, op)?;
operations.push(Operation {
id: None,
group: None,
kind,
});
notes.extend(note.map(|n| format!("op-{}: {n}", operations.len())));
}
}
}
let side = |ids: Vec<String>| (!ids.is_empty()).then(|| ChangeFilter::ids(ids));
let resolve_revisions = (!accept.is_empty() || !reject.is_empty()).then(|| ResolveRevisions {
accept: side(accept),
reject: side(reject),
reject_visible: false,
});
let existing = existing.unwrap_or_else(|| {
if crate::changes::list_changes(source).is_ok_and(|c| !c.is_empty()) {
ExistingRevisions::Keep
} else {
ExistingRevisions::Refuse
}
});
Ok(FlagPlan {
plan: EditPlan {
schema_version: crate::inspect::SCHEMA_VERSION,
source_sha256: None,
author: author.to_string(),
date: date.map(str::to_string),
initials: None,
resolve_revisions,
removed_anchor_comments: Default::default(),
existing_revisions: existing,
operations,
update_fields: false,
rewrite_others_comments: false,
others: super::Others::Allow,
},
notes,
})
}
fn styles(tokens: &[String]) -> Result<(Option<RunFormat>, Option<String>), String> {
let mut format = RunFormat::default();
let mut touched = false;
let mut paragraph: Option<String> = None;
for token in tokens {
let (key, value) = token
.split_once('=')
.map_or((token.as_str(), None), |(k, v)| (k, Some(v)));
match (key, value) {
("bold", None) => format.bold = Some(true),
("italic", None) => format.italic = Some(true),
("underline", None) => format.underline = Some(true),
("strike", None) => format.strike = Some(true),
("caps", None) => format.caps = Some(true),
("highlight", Some(v)) => format.highlight = Some(v.to_string()),
("font", Some(v)) => format.font = Some(v.to_string()),
("color", Some(v)) => format.color = Some(v.to_string()),
("size", Some(v)) => {
let points: f64 = v
.parse()
.ok()
.filter(|p: &f64| p.is_finite() && *p > 0.0 && *p <= 1638.0)
.ok_or_else(|| format!("--style size={v}: points between 0 and 1638"))?;
let half = (points * 2.0).round();
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "checked above: 0 < half <= 3276"
)]
let half = half as u32;
format.size_pt = Some(HalfPoints(half));
}
(_, Some(_)) => return Err(format!("--style {token}: unknown key")),
("", None) => return Err("--style needs a value".into()),
(name, None) => {
if paragraph.replace(name.to_string()).is_some() {
return Err("--style: one paragraph style per operation".into());
}
continue;
}
}
touched = true;
}
Ok((touched.then_some(format), paragraph))
}
fn content_text(content: &str) -> (String, Option<&'static str>) {
let text = crate::markdown::unescape_markdown(content);
let marks = crate::markdown::plain_anchor(content).trim_start() != text.trim_start();
(
text,
marks.then_some("content keeps its Markdown marks as text; use --style for formatting"),
)
}
fn comment_number(at: &str) -> Option<u32> {
match crate::ids::parse_location(at) {
Ok(crate::ids::Location::Comment(id)) => Some(id),
_ => None,
}
}
fn expired(at: &str, keep: impl Fn(&ShortId) -> bool) -> Option<String> {
match crate::ids::parse_location(at) {
Err(e) if e.expected == crate::ids::LOCATION_NOT_YET => Some(format!("-p {e}")),
Err(e) if e.hint.is_some() => {
let hint = crate::ids::hint_where(e.hint.clone(), keep);
Some(format!("-p {}", IdError { hint, ..e }))
}
_ => None,
}
}
enum Group {
Operation,
Settle { accept: bool, ids: Vec<String> },
Answer {
how: Answer,
ids: Vec<String>,
comment: Option<String>,
},
}
#[derive(Clone, Copy)]
enum Answer {
Strike,
Restore,
ByKind,
}
fn revision_group(verb: Verb, op: &FlagOp) -> Result<Group, String> {
let at = op.at.trim();
let verbs: Vec<&str> = [
("accept", op.accept),
("reject", op.reject),
("strike", op.strike),
("restore", op.restore),
]
.into_iter()
.filter_map(|(name, set)| set.then_some(name))
.collect();
if !verbs.is_empty()
&& let Some(refused) = expired(at, |id| matches!(id, ShortId::Change { .. }))
{
return Err(refused);
}
let bad = |m: &str| Err(format!("-p {at}: {m}"));
let ids = match crate::ids::parse_location(at) {
Ok(Location::Changes(ids)) => Some(ids),
_ => None,
};
if op.reply.is_some() {
if !verbs.is_empty() || op.visible {
return bad("--reply takes no other flag");
}
return Ok(Group::Operation);
}
if verb == Verb::Add && ids.is_some() {
return bad(
"add resolves no revision; use edit -p r12 --accept, --reject, --strike or --restore",
);
}
if op.visible && !op.reject {
return bad("--visible goes with --reject");
}
let Some(ids) = ids else {
return match verbs.first() {
None => Ok(Group::Operation),
Some(&("accept" | "reject")) => bad("--accept and --reject take a revision (-p r12)"),
Some(name) => bad(&format!("--{name} takes a revision (-p r12)")),
};
};
if let [first, second, ..] = verbs[..] {
return bad(&format!("--{first} and --{second} exclude each other"));
}
let others = op.anchor.is_some()
|| op.delete
|| op.resolve
|| op.before
|| op.comment
|| !op.styles.is_empty();
if others || verbs.is_empty() {
return bad("a revision takes --accept, --reject, --strike or --restore");
}
let how = if op.strike {
Some(Answer::Strike)
} else if op.restore {
Some(Answer::Restore)
} else {
op.visible.then_some(Answer::ByKind)
};
if op.content.is_some() && how.is_none() {
return bad(
"--content on a revision is the comment of --strike, --restore or --reject --visible",
);
}
let ids = ids.iter().map(crate::ids::ChangeId::short).collect();
Ok(match how {
Some(how) => Group::Answer {
how,
ids,
comment: op.content.clone(),
},
None => Group::Settle {
accept: op.accept,
ids,
},
})
}
fn flag_operation(verb: Verb, op: &FlagOp) -> Result<(OperationKind, Option<String>), String> {
use OperationKind as K;
let at = op.at.trim();
let bad = |m: &str| Err(format!("-p {at}: {m}"));
if at.is_empty() {
return Err("-p needs a location".into());
}
let names_comment = op.reply.is_some() || op.resolve;
let comment_takes = op.anchor.is_none()
&& op.styles.is_empty()
&& !op.before
&& !op.comment
&& !(verb == Verb::Add && (op.delete || op.resolve));
let keep = |id: &ShortId| match id {
ShortId::Comment(_) => comment_takes,
ShortId::Change { .. } => false,
_ => !names_comment,
};
if let Some(refused) = expired(at, keep) {
return Err(refused);
}
if let Some(reply) = &op.reply {
let Some(comment_id) = comment_number(at) else {
return bad("--reply is for a comment (-p c5)");
};
if op.anchor.is_some()
|| op.content.is_some()
|| op.delete
|| op.resolve
|| op.before
|| op.comment
|| !op.styles.is_empty()
{
return bad("--reply takes no other flag");
}
let (text, note) = content_text(reply);
let note = note.map(|_| {
"the reply keeps its Markdown marks as text; replies take no formatting".to_string()
});
return Ok((K::ReplyComment { comment_id, text }, note));
}
if op.delete && (op.content.is_some() || op.resolve) {
return bad("--delete excludes --content and --resolve");
}
if op.resolve && (op.content.is_some() || op.anchor.is_some() || !op.styles.is_empty()) {
return bad("--resolve takes no --anchor, --content or --style");
}
if op.before && (op.anchor.is_some() || op.comment) {
return bad("--before is for a new paragraph, not a comment");
}
let paragraph = || Selector::Name(at.to_string());
let (run_format, paragraph_style) = styles(&op.styles).map_err(|m| format!("-p {at}: {m}"))?;
let (content, note) = match &op.content {
Some(c) => {
let (text, note) = content_text(c);
(Some(text), note.map(str::to_string))
}
None => (None, None),
};
let comment_id = comment_number(at);
let mut kind = match (verb, comment_id, &op.anchor, content) {
(Verb::Add, _, _, _) if op.delete || op.resolve => {
return bad("add takes no --delete or --resolve");
}
(_, Some(_), Some(_), _) => return bad("a comment takes no --anchor"),
(_, Some(_), _, _) if !op.styles.is_empty() => return bad("a comment takes no --style"),
(Verb::Add, Some(id), None, Some(text)) if !op.before && !op.comment && !op.delete => {
K::ReplyComment {
comment_id: id,
text,
}
}
(Verb::Add, Some(_), None, _) => return bad("a reply takes --content only"),
(Verb::Edit, Some(id), None, Some(text)) => K::EditComment {
comment_id: id,
text,
},
(Verb::Edit, Some(id), None, None) if op.delete => K::DeleteComment { comment_id: id },
(Verb::Edit, Some(id), None, None) if op.resolve => K::ResolveComment {
comment_id: id,
done: true,
},
(Verb::Edit, Some(_), None, None) => {
return bad("a comment takes --content, --delete or --resolve");
}
(Verb::Add, None, _, Some(_))
if (op.anchor.is_some() || op.comment) && !op.styles.is_empty() =>
{
return bad("a comment takes no --style");
}
(Verb::Add, None, Some(anchor), Some(text)) => K::Comment {
paragraph: paragraph(),
find: Some(anchor.clone()),
text,
through: None,
occurrence: None,
},
(Verb::Add, None, None, Some(text)) if op.comment => K::Comment {
paragraph: paragraph(),
find: None,
text,
through: None,
occurrence: None,
},
(Verb::Add, None, None, Some(text)) => {
let format = run_format.unwrap_or_default();
if format.strike.is_some()
|| format.caps.is_some()
|| format.font.is_some()
|| format.size_pt.is_some()
|| format.color.is_some()
{
return bad(
"a new paragraph takes --style bold, italic, underline, highlight=COLOR or a paragraph style",
);
}
K::InsertParagraph {
paragraph: paragraph(),
position: if op.before { Side::Before } else { Side::After },
runs: vec![RunSpec {
text,
bold: format.bold,
italic: format.italic,
underline: format.underline,
highlight: format.highlight,
}],
like: None,
style: paragraph_style,
comment: None,
}
}
(Verb::Add, None, _, None) => return bad("add needs --content"),
(Verb::Edit, None, _, _) if op.resolve => {
return bad("--resolve is for a comment (-p c5)");
}
(Verb::Edit, None, _, _) if op.before || op.comment => {
return bad("--before and --comment belong to add");
}
(Verb::Edit, None, Some(anchor), Some(text)) => {
if paragraph_style.is_some() {
return bad("a paragraph style takes no --anchor");
}
inline_edit(paragraph(), anchor, text, run_format)
.map_err(|m| format!("-p {at}: {m}"))?
}
(Verb::Edit, None, Some(_), None) if op.delete && !op.styles.is_empty() => {
return bad("--delete takes no --style");
}
(Verb::Edit, None, Some(anchor), None) if op.delete => K::Delete {
paragraph: paragraph(),
find: anchor.clone(),
occurrence: None,
comment: None,
},
(Verb::Edit, None, Some(anchor), None) => match (run_format, paragraph_style) {
(Some(format), None) => K::FormatRun {
paragraph: paragraph(),
find: anchor.clone(),
format,
occurrence: None,
},
(_, Some(_)) => return bad("a paragraph style takes no --anchor"),
(None, None) => return bad("--anchor needs --content, --delete or --style"),
},
(Verb::Edit, None, None, Some(text))
if paragraph_style.is_none() && run_format.is_none() =>
{
K::Rewrite {
paragraph: paragraph(),
text,
comment: None,
}
}
(Verb::Edit, None, None, Some(_)) => {
return bad(
"a rewrite takes no --style: format with --anchor, or set the paragraph style in a second -p",
);
}
(Verb::Edit, None, None, None) if op.delete => {
if !op.styles.is_empty() {
return bad("--delete takes no --style");
}
K::DeleteParagraph {
paragraph: paragraph(),
comment: None,
}
}
(Verb::Edit, None, None, None) => match (run_format, paragraph_style) {
(None, Some(style)) => K::FormatParagraph {
paragraph: paragraph(),
style: Some(style),
alignment: None,
line_spacing: None,
space_before: None,
space_after: None,
},
(Some(_), _) => return bad("run formatting needs --anchor (the text to format)"),
(None, None) => return bad("edit needs --content, --delete, --resolve or --style"),
},
};
match &mut kind {
K::Replace { occurrence, .. }
| K::Insert { occurrence, .. }
| K::Delete { occurrence, .. }
| K::FormatRun { occurrence, .. }
| K::Comment { occurrence, .. } => *occurrence = op.occurrence,
_ => {}
}
Ok((kind, note))
}
fn inline_edit(
paragraph: Selector,
anchor: &str,
text: String,
format: Option<RunFormat>,
) -> Result<OperationKind, String> {
use OperationKind as K;
let plain = crate::markdown::plain_anchor(anchor);
if text == anchor || text == plain {
return match format {
Some(format) => Ok(K::FormatRun {
paragraph,
find: anchor.to_string(),
format,
occurrence: None,
}),
None => Err("--content equals the anchor; nothing to change".into()),
};
}
let after = text.strip_prefix(anchor).or_else(|| {
text.strip_prefix(plain.as_str())
.filter(|_| !plain.is_empty())
});
if let Some(added) = after.filter(|a| !a.is_empty()) {
return Ok(K::Insert {
paragraph,
after: Some(anchor.to_string()),
before: None,
position: None,
text: added.to_string(),
format,
comment: None,
occurrence: None,
});
}
let before = text.strip_suffix(anchor).or_else(|| {
text.strip_suffix(plain.as_str())
.filter(|_| !plain.is_empty())
});
if let Some(added) = before.filter(|a| !a.is_empty()) {
return Ok(K::Insert {
paragraph,
after: None,
before: Some(anchor.to_string()),
position: None,
text: added.to_string(),
format,
comment: None,
occurrence: None,
});
}
Ok(K::Replace {
paragraph,
find: anchor.to_string(),
replacement: text,
format,
comment: None,
whole: false,
occurrence: None,
})
}
pub fn editing_mode_conflict(plan: &EditPlan, source: &[u8]) -> Option<String> {
if plan.existing_revisions == ExistingRevisions::Keep
&& crate::edit::revisions_left(source, plan)
{
return Some(
"--editing-mode needs a document without tracked changes; it has some, so pass --existing-revisions accept or reject".into(),
);
}
let (parent, paragraph) = crate::edit::replies_on_deleted_paragraphs(source, plan)
.into_iter()
.next()?;
let paragraph = paragraph.unwrap_or_else(|| "its paragraph".into());
Some(format!(
"--editing-mode deletes {paragraph} outright, and c{parent}'s thread with it, so the reply to c{parent} would join nothing; drop --editing-mode to track the deletion and keep the reply on the struck text, or delete without replying"
))
}
pub fn editing_mode_conflict_json(plan: &str, source: &[u8]) -> Option<String> {
EditPlan::from_json(plan)
.ok()
.and_then(|plan| editing_mode_conflict(&plan, source))
}
pub(crate) fn record_into(plan: Option<EditPlan>, more: EditPlan) -> Result<EditPlan, String> {
let Some(mut plan) = plan else {
return Ok(more);
};
if let (Some(pinned), Some(source)) = (&plan.source_sha256, &more.source_sha256)
&& pinned != source
{
return Err(format!(
"the plan was recorded against another document (sha256 {pinned}); this one is {source}"
));
}
if plan.author.is_empty() {
return Err("the plan names no author, so it cannot be applied; start a new plan".into());
}
if plan.author != more.author {
return Err(format!(
"the plan's author is {:?}; pass --author {:?}, or start a new plan",
plan.author, plan.author
));
}
if plan.date != more.date {
return Err(match &plan.date {
Some(date) => {
format!("the plan's date is {date}; pass --datetime {date}, or start a new plan")
}
None => {
"the plan takes its date when it runs; drop --datetime, or start a new plan".into()
}
});
}
if plan.existing_revisions != more.existing_revisions {
return Err(format!(
"the plan's existing_revisions is {mode}; pass --existing-revisions {mode} to match it, or start a new plan",
mode = existing_name(plan.existing_revisions),
));
}
let before = plan.operations.len();
let added = before + 1..=before + more.operations.len();
if let Some(id) = plan
.operations
.iter()
.filter_map(|op| op.id.as_deref())
.find(|id| added.clone().any(|n| *id == format!("op-{n}")))
{
return Err(format!(
"the plan names an operation {id}, the id a recorded operation takes; rename it, or start a new plan"
));
}
if let Some(more) = more.resolve_revisions {
let resolve = plan.resolve_revisions.get_or_insert_with(Default::default);
if let Some(ids) = more.reject.as_ref().and_then(|r| r.ids.as_ref())
&& resolve.reject_visible
&& !more.reject_visible
{
return Err(format!(
"the plan's reject is visible, and a recorded --reject is silent: record -p {} --reject --visible, or start a new plan",
ids.join(" -p ")
));
}
for side in [&mut resolve.accept, &mut resolve.reject] {
if let Some(normal) = side.as_ref().and_then(|f| f.normalized().ok()) {
*side = Some(normal);
}
}
join_ids(&mut resolve.accept, more.accept, "accept")?;
join_ids(&mut resolve.reject, more.reject, "reject")?;
let (accept, reject) = (filter_ids(&resolve.accept), filter_ids(&resolve.reject));
if let Some(both) = accept.iter().find(|id| reject.contains(id)) {
return Err(format!("{both} is both accepted and rejected"));
}
}
plan.operations.extend(more.operations);
plan.rewrite_others_comments |= more.rewrite_others_comments;
if more.others.keeps() {
plan.others = super::Others::Keep;
}
plan.source_sha256 = plan.source_sha256.or(more.source_sha256);
Ok(plan)
}
fn existing_name(mode: ExistingRevisions) -> &'static str {
match mode {
ExistingRevisions::Keep => "keep",
ExistingRevisions::Accept => "accept",
ExistingRevisions::Reject => "reject",
ExistingRevisions::Refuse => "refuse",
}
}
fn join_ids(
filter: &mut Option<ChangeFilter>,
more: Option<ChangeFilter>,
side: &str,
) -> Result<(), String> {
let Some(more) = more.and_then(|m| m.ids) else {
return Ok(());
};
if let Some(held) = filter.as_ref()
&& (held.authors.is_some() || held.kinds.is_some() || held.ids.is_none())
{
return Err(format!(
"the plan's {side} selects revisions by author or kind, or all of them; a recorded revision id would narrow it, so start a new plan"
));
}
let ids = filter
.get_or_insert_with(Default::default)
.ids
.get_or_insert_with(Vec::new);
for id in more {
if !ids.contains(&id) {
ids.push(id);
}
}
Ok(())
}
fn filter_ids(filter: &Option<ChangeFilter>) -> &[String] {
filter
.as_ref()
.and_then(|f| f.ids.as_deref())
.unwrap_or_default()
}
pub(crate) fn renumber_notes(notes: Vec<String>, before: usize) -> Vec<String> {
notes
.into_iter()
.map(|note| {
let parsed = note
.strip_prefix("op-")
.and_then(|rest| rest.split_once(':'))
.and_then(|(n, rest)| Some((n.parse::<usize>().ok()?, rest)));
match parsed {
Some((n, rest)) => format!("op-{}:{rest}", n + before),
None => note,
}
})
.collect()
}
pub fn plan_from_flags_json(
verb: &str,
operations: &str,
author: &str,
date: Option<&str>,
existing: &str,
source: &[u8],
) -> Result<(String, Vec<String>), String> {
let verb = match verb {
"edit" => Verb::Edit,
"add" => Verb::Add,
other => return Err(format!("unknown verb {other:?}: edit or add")),
};
let ops: Vec<FlagOp> =
serde_json::from_str(operations).map_err(|e| format!("operations: {e}"))?;
let existing = match existing {
"auto" => None,
"keep" => Some(ExistingRevisions::Keep),
"accept" => Some(ExistingRevisions::Accept),
"reject" => Some(ExistingRevisions::Reject),
"refuse" => Some(ExistingRevisions::Refuse),
other => {
return Err(format!(
"existing revisions {other:?}: auto, keep, accept, reject or refuse"
));
}
};
let built = plan_from_flags(verb, &ops, author, date, existing, source)?;
let json = serde_json::to_string(&built.plan).map_err(|e| e.to_string())?;
Ok((json, built.notes))
}
#[cfg(test)]
mod tests {
use super::*;
fn op(at: &str) -> FlagOp {
FlagOp {
at: at.into(),
..FlagOp::default()
}
}
fn kind(verb: Verb, op: &FlagOp) -> Result<OperationKind, String> {
flag_operation(verb, op).map(|(k, _)| k)
}
fn recorded(verb: Verb, ops: &[FlagOp], author: &str, sha: &str) -> EditPlan {
let mut plan = plan_from_flags(verb, ops, author, None, Some(ExistingRevisions::Keep), b"")
.unwrap()
.plan;
plan.source_sha256 = Some(sha.into());
plan
}
fn revision(at: &str, accept: bool) -> FlagOp {
FlagOp {
accept,
reject: !accept,
..op(at)
}
}
fn rewrite(at: &str, content: &str) -> FlagOp {
FlagOp {
content: Some(content.into()),
..op(at)
}
}
#[test]
fn recording_appends_operations_and_joins_revision_ids() {
let first = recorded(
Verb::Edit,
&[rewrite("p1", "One."), revision("r2", true)],
"Ann",
"aa",
);
let plan = record_into(None, first.clone()).unwrap();
assert_eq!(plan, first);
let second = recorded(
Verb::Add,
&[FlagOp {
comment: true,
..rewrite("p3", "Why?")
}],
"Ann",
"aa",
);
let third = recorded(
Verb::Edit,
&[revision("r2+5", true), revision("header1.r1", false)],
"Ann",
"aa",
);
let plan = record_into(Some(record_into(Some(plan), second).unwrap()), third).unwrap();
assert_eq!(plan.operations.len(), 2);
assert!(matches!(
plan.operations[1].kind,
OperationKind::Comment { .. }
));
let resolve = plan.resolve_revisions.unwrap();
assert_eq!(resolve.accept.unwrap().ids.unwrap(), ["r2", "r5"]);
assert_eq!(resolve.reject.unwrap().ids.unwrap(), ["header1.r1"]);
assert_eq!(plan.source_sha256.as_deref(), Some("aa"));
}
#[test]
fn recording_refuses_what_the_plan_names_differently() {
let base = || recorded(Verb::Edit, &[rewrite("p1", "One.")], "Ann", "aa");
let refused = |more: EditPlan| record_into(Some(base()), more).unwrap_err();
assert!(
refused(recorded(Verb::Edit, &[rewrite("p2", "Two.")], "Ann", "bb"))
.starts_with("the plan was recorded against another document (sha256 aa)")
);
assert_eq!(
refused(recorded(Verb::Edit, &[rewrite("p2", "Two.")], "Bob", "aa")),
r#"the plan's author is "Ann"; pass --author "Ann", or start a new plan"#
);
let mut dated = recorded(Verb::Edit, &[rewrite("p2", "Two.")], "Ann", "aa");
dated.date = Some("2026-10-12T00:00:00Z".into());
assert_eq!(
refused(dated),
"the plan takes its date when it runs; drop --datetime, or start a new plan"
);
let mut refusing = recorded(Verb::Edit, &[rewrite("p2", "Two.")], "Ann", "aa");
refusing.existing_revisions = ExistingRevisions::Refuse;
assert_eq!(
refused(refusing),
"the plan's existing_revisions is keep; pass --existing-revisions keep to match it, or start a new plan"
);
let accepted = recorded(Verb::Edit, &[revision("r2", true)], "Ann", "aa");
let plan = record_into(Some(base()), accepted).unwrap();
assert_eq!(
record_into(
Some(plan),
recorded(Verb::Edit, &[revision("r2", false)], "Ann", "aa")
)
.unwrap_err(),
"r2 is both accepted and rejected"
);
}
#[test]
fn recording_pins_an_unpinned_plan_and_keeps_hand_written_meaning() {
let mut unpinned = recorded(Verb::Edit, &[rewrite("p1", "One.")], "Ann", "aa");
unpinned.source_sha256 = None;
let more = recorded(Verb::Edit, &[rewrite("p2", "Two.")], "Ann", "bb");
let plan = record_into(Some(unpinned), more).unwrap();
assert_eq!(plan.source_sha256.as_deref(), Some("bb"));
for held in [
ChangeFilter {
authors: Some(vec!["Ann".into()]),
..ChangeFilter::default()
},
ChangeFilter::default(),
] {
let mut plan = recorded(Verb::Edit, &[rewrite("p1", "One.")], "Ann", "aa");
plan.resolve_revisions = Some(ResolveRevisions {
accept: Some(held),
reject: None,
reject_visible: false,
});
let accept = recorded(Verb::Edit, &[revision("r9", true)], "Ann", "aa");
assert!(
record_into(Some(plan), accept)
.unwrap_err()
.starts_with("the plan's accept selects revisions by author or kind, or all")
);
}
let mut named = recorded(Verb::Edit, &[rewrite("p1", "One.")], "Ann", "aa");
named.operations[0].id = Some("op-2".into());
let more = recorded(Verb::Edit, &[rewrite("p2", "Two.")], "Ann", "aa");
assert_eq!(
record_into(Some(named), more).unwrap_err(),
"the plan names an operation op-2, the id a recorded operation takes; rename it, or start a new plan"
);
}
#[test]
fn a_silent_reject_is_not_recorded_into_a_visible_one() {
let mut plan = recorded(Verb::Edit, &[rewrite("p1", "One.")], "Ann", "aa");
plan.resolve_revisions = Some(ResolveRevisions {
accept: None,
reject: Some(ChangeFilter::ids(["r4"])),
reject_visible: true,
});
let reject = recorded(Verb::Edit, &[revision("r5", false)], "Ann", "aa");
assert_eq!(
record_into(Some(plan.clone()), reject).unwrap_err(),
"the plan's reject is visible, and a recorded --reject is silent: record -p r5 --reject --visible, or start a new plan"
);
let accept = recorded(Verb::Edit, &[revision("r6", true)], "Ann", "aa");
let merged = record_into(Some(plan), accept).unwrap();
let resolve = merged.resolve_revisions.unwrap();
assert!(resolve.reject_visible);
assert_eq!(resolve.accept.unwrap().ids.unwrap(), ["r6"]);
}
#[test]
fn recording_reads_ids_and_names_as_the_plan_spells_them() {
let mut short = recorded(Verb::Edit, &[rewrite("p1", "One.")], "Ann", "aa");
short.resolve_revisions = Some(ResolveRevisions {
accept: Some(ChangeFilter::ids(["r1"])),
reject: None,
reject_visible: false,
});
let again = recorded(Verb::Edit, &[revision("r1", true)], "Ann", "aa");
let plan = record_into(Some(short.clone()), again).unwrap();
let accept = plan.resolve_revisions.unwrap().accept.unwrap().ids.unwrap();
assert_eq!(accept, ["r1"]);
let reject = recorded(Verb::Edit, &[revision("r1", false)], "Ann", "aa");
assert_eq!(
record_into(Some(short), reject).unwrap_err(),
"r1 is both accepted and rejected"
);
let mut padded = recorded(Verb::Edit, &[rewrite("p1", "One.")], "Ann", "aa");
padded.operations[0].id = Some("op-02".into());
let more = recorded(Verb::Edit, &[rewrite("p2", "Two.")], "Ann", "aa");
assert!(record_into(Some(padded), more).is_ok());
let mut nobody = recorded(Verb::Edit, &[rewrite("p1", "One.")], "", "aa");
nobody.author = String::new();
let more = recorded(Verb::Edit, &[rewrite("p2", "Two.")], "", "aa");
assert!(
record_into(Some(nobody), more)
.unwrap_err()
.starts_with("the plan names no author")
);
}
#[test]
fn notes_count_on_from_the_operations_the_plan_held() {
assert_eq!(
renumber_notes(vec!["op-1: kept as text".into(), "other".into()], 3),
["op-4: kept as text", "other"]
);
}
#[test]
fn delete_refuses_a_style_with_an_anchor_too() {
let mut delete = op("p4");
delete.anchor = Some("Fees".into());
delete.delete = true;
delete.styles = vec!["bold".into()];
assert_eq!(
kind(Verb::Edit, &delete).unwrap_err(),
"-p p4: --delete takes no --style"
);
delete.styles = vec!["Heading2".into()];
assert_eq!(
kind(Verb::Edit, &delete).unwrap_err(),
"-p p4: --delete takes no --style"
);
}
#[test]
fn add_refuses_what_it_cannot_write() {
let mut new = op("p1");
new.content = Some("Pay on time.".into());
for style in ["strike", "caps", "font=Calibri", "size=11", "color=FF0000"] {
new.styles = vec![style.into()];
let e = kind(Verb::Add, &new).unwrap_err();
assert!(e.contains("a new paragraph takes --style"), "{style}: {e}");
}
new.styles = vec!["bold".into(), "highlight=yellow".into(), "Heading2".into()];
assert!(kind(Verb::Add, &new).is_ok());
let mut comment = op("p1");
comment.anchor = Some("Pay".into());
comment.content = Some("Why?".into());
comment.styles = vec!["bold".into()];
assert!(
kind(Verb::Add, &comment)
.unwrap_err()
.contains("a comment takes no --style")
);
let mut delete = op("c0");
delete.delete = true;
assert_eq!(
kind(Verb::Add, &delete).unwrap_err(),
"-p c0: add takes no --delete or --resolve"
);
}
#[test]
fn the_json_entry_point_reads_what_the_cli_schema_serializes() {
let ops = r#"[{"at":"p0","anchor":null,"content":"**Fees**","delete":false,"resolve":false,"before":false,"comment":false,"styles":[]}]"#;
let (plan, notes) = plan_from_flags_json(
"edit",
ops,
"Ann",
Some("2026-10-01T09:00:00Z"),
"keep",
b"",
)
.unwrap();
let plan = EditPlan::from_json(&plan).unwrap();
assert_eq!(plan.author, "Ann");
assert_eq!(plan.existing_revisions, ExistingRevisions::Keep);
assert_eq!(plan.operations.len(), 1);
assert_eq!(
notes,
["op-1: content keeps its Markdown marks as text; use --style for formatting"]
);
for (verb, existing) in [("move", "keep"), ("edit", "sometimes")] {
assert!(plan_from_flags_json(verb, ops, "Ann", None, existing, b"").is_err());
}
assert!(plan_from_flags_json("edit", "{", "Ann", None, "keep", b"").is_err());
}
#[test]
fn an_anchor_kept_at_either_end_is_an_insertion() {
let k = inline_edit(
Selector::Name("p1".into()),
"thirty",
"thirty (30)".into(),
None,
)
.unwrap();
assert!(
matches!(k, OperationKind::Insert { after: Some(ref a), ref text, .. } if a == "thirty" && text == " (30)")
);
let k = inline_edit(
Selector::Name("p2".into()),
"Late",
"Note: Late".into(),
None,
)
.unwrap();
assert!(
matches!(k, OperationKind::Insert { before: Some(ref b), ref text, .. } if b == "Late" && text == "Note: ")
);
let k = inline_edit(
Selector::Name("p0".into()),
"# Fees",
"Fees * Costs".into(),
None,
)
.unwrap();
assert!(
matches!(k, OperationKind::Insert { after: Some(ref a), ref text, .. } if a == "# Fees" && text == " * Costs")
);
let k = inline_edit(Selector::Name("p1".into()), "thirty", "sixty".into(), None).unwrap();
assert!(
matches!(k, OperationKind::Replace { ref replacement, .. } if replacement == "sixty")
);
assert!(inline_edit(Selector::Name("p1".into()), "# x", "x".into(), None).is_err());
}
#[test]
fn content_notes_every_mark_the_view_would_read() {
for noted in [
"**bold**",
"{++x++}",
"{~~a~>b~~}",
"# Title",
"_x_",
"<u>u</u>",
] {
assert!(content_text(noted).1.is_some(), "{noted}");
}
for plain in [
"\\*\\*x\\*\\*",
"*italics?",
"2 * 3",
"snake__case",
"plain",
" lead",
"trail ",
"two\nlines",
"a = b",
"x - y",
] {
assert!(content_text(plain).1.is_none(), "{plain}");
}
}
#[test]
fn content_equal_to_the_anchor_with_a_style_formats_the_run() {
let bold = RunFormat {
bold: Some(true),
..RunFormat::default()
};
for content in ["thirty", "**thirty**"] {
let k = inline_edit(
Selector::Name("p1".into()),
"**thirty**",
content.into(),
Some(bold.clone()),
)
.unwrap();
assert!(
matches!(k, OperationKind::FormatRun { ref find, ref format, .. } if find == "**thirty**" && format.bold == Some(true)),
"{content}"
);
}
}
#[test]
fn each_flag_combination_maps_to_one_kind() {
let with = |f: &dyn Fn(&mut FlagOp)| {
let mut o = op("p1");
f(&mut o);
o
};
assert!(matches!(
kind(Verb::Edit, &with(&|o| o.content = Some("New text.".into()))),
Ok(OperationKind::Rewrite { .. })
));
assert!(matches!(
kind(Verb::Edit, &with(&|o| o.delete = true)),
Ok(OperationKind::DeleteParagraph { .. })
));
assert!(matches!(
kind(
Verb::Edit,
&with(&|o| {
o.anchor = Some("a".into());
o.delete = true;
})
),
Ok(OperationKind::Delete { .. })
));
assert!(matches!(
kind(
Verb::Edit,
&with(&|o| {
o.anchor = Some("a".into());
o.styles = vec!["bold".into(), "size=10.5".into()];
})
),
Ok(OperationKind::FormatRun {
format: RunFormat {
bold: Some(true),
size_pt: Some(HalfPoints(21)),
..
},
..
})
));
assert!(matches!(
kind(Verb::Edit, &with(&|o| o.styles = vec!["Heading2".into()])),
Ok(OperationKind::FormatParagraph { style: Some(_), .. })
));
assert!(matches!(
kind(
Verb::Add,
&with(&|o| {
o.content = Some("New".into());
o.before = true;
o.styles = vec!["Heading2".into(), "bold".into()];
})
),
Ok(OperationKind::InsertParagraph {
position: Side::Before,
style: Some(_),
..
})
));
assert!(matches!(
kind(
Verb::Add,
&with(&|o| {
o.anchor = Some("a".into());
o.content = Some("Why?".into());
})
),
Ok(OperationKind::Comment { find: Some(_), .. })
));
assert!(matches!(
kind(
Verb::Add,
&with(&|o| {
o.comment = true;
o.content = Some("Why?".into());
})
),
Ok(OperationKind::Comment { find: None, .. })
));
let c5 = |f: &dyn Fn(&mut FlagOp)| {
let mut o = op("c5");
f(&mut o);
o
};
assert!(matches!(
kind(Verb::Add, &c5(&|o| o.content = Some("Yes.".into()))),
Ok(OperationKind::ReplyComment { comment_id: 5, .. })
));
assert!(matches!(
kind(Verb::Edit, &c5(&|o| o.content = Some("Yes.".into()))),
Ok(OperationKind::EditComment { comment_id: 5, .. })
));
assert!(matches!(
kind(Verb::Edit, &c5(&|o| o.delete = true)),
Ok(OperationKind::DeleteComment { comment_id: 5 })
));
assert!(matches!(
kind(Verb::Edit, &c5(&|o| o.resolve = true)),
Ok(OperationKind::ResolveComment {
comment_id: 5,
done: true
})
));
}
#[test]
fn contradictory_flags_are_usage_errors() {
let cases: Vec<(Verb, FlagOp)> = vec![
(Verb::Edit, op("p1")),
(
Verb::Edit,
FlagOp {
delete: true,
content: Some("x".into()),
..op("p1")
},
),
(
Verb::Edit,
FlagOp {
resolve: true,
..op("p1")
},
),
(
Verb::Edit,
FlagOp {
anchor: Some("a".into()),
content: Some("b".into()),
..op("c0")
},
),
(
Verb::Edit,
FlagOp {
content: Some("x".into()),
styles: vec!["bold".into()],
..op("p1")
},
),
(
Verb::Edit,
FlagOp {
styles: vec!["size=big".into()],
anchor: Some("a".into()),
..op("p1")
},
),
(
Verb::Edit,
FlagOp {
styles: vec!["A".into(), "B".into()],
..op("p1")
},
),
(Verb::Add, op("p1")),
(
Verb::Add,
FlagOp {
anchor: Some("x".into()),
content: Some("y".into()),
before: true,
..op("p1")
},
),
(
Verb::Add,
FlagOp {
content: Some("y".into()),
before: true,
..op("c0")
},
),
(
Verb::Add,
FlagOp {
content: Some("y".into()),
delete: true,
..op("p1")
},
),
(
Verb::Edit,
FlagOp {
anchor: Some("same".into()),
content: Some("same".into()),
..op("p1")
},
),
];
for (verb, o) in cases {
assert!(flag_operation(verb, &o).is_err(), "{verb:?} {o:?}");
}
assert!(plan_from_flags(Verb::Edit, &[], "A", None, None, b"").is_err());
}
#[test]
fn content_escapes_resolve_and_kept_marks_get_a_note() {
let (text, note) = content_text("Fees \\* Costs");
assert_eq!((text.as_str(), note), ("Fees * Costs", None));
let (text, note) = content_text("**Fees**");
assert_eq!(text, "**Fees**");
assert!(note.is_some());
let (text, note) = content_text(" Fees");
assert_eq!((text.as_str(), note), (" Fees", None));
let plan = plan_from_flags(
Verb::Edit,
&[FlagOp {
content: Some("**Fees**".into()),
..op("p0")
}],
"Redline",
Some("2026-10-01T09:00:00Z"),
Some(ExistingRevisions::Refuse),
b"",
)
.unwrap();
assert_eq!(
plan.notes,
["op-1: content keeps its Markdown marks as text; use --style for formatting"]
);
assert_eq!(plan.plan.author, "Redline");
}
fn with(at: &str, set: impl Fn(&mut FlagOp)) -> FlagOp {
let mut o = op(at);
set(&mut o);
o
}
#[test]
fn revisions_go_to_resolve_revisions() {
let ops = [
with("r1", |o| o.accept = true),
with("p0", |o| o.content = Some("**x**".into())),
with("header1.r2", |o| o.reject = true),
with("r3+4", |o| o.accept = true),
];
let built = plan_from_flags(Verb::Edit, &ops, "A", None, None, b"").unwrap();
assert_eq!(
built.plan.resolve_revisions,
Some(ResolveRevisions {
accept: Some(ChangeFilter::ids(["r1", "r3", "r4"])),
reject: Some(ChangeFilter::ids(["header1.r2"])),
reject_visible: false,
})
);
assert_eq!(built.plan.operations.len(), 1);
assert_eq!(
built.notes,
["op-1: content keeps its Markdown marks as text; use --style for formatting"]
);
let only = plan_from_flags(
Verb::Edit,
&[with("r7", |o| o.reject = true)],
"A",
None,
None,
b"",
)
.unwrap();
assert!(only.plan.operations.is_empty());
assert_eq!(
only.plan.resolve_revisions.unwrap().reject,
Some(ChangeFilter::ids(["r7"]))
);
}
#[test]
fn strike_and_restore_groups_are_operations() {
let built = plan_from_flags(
Verb::Edit,
&[
with("r4", |o| {
o.strike = true;
o.content = Some("No.".into());
}),
with("header1.r2", |o| o.restore = true),
],
"A",
None,
None,
b"",
)
.unwrap();
assert!(built.plan.resolve_revisions.is_none());
let kinds: Vec<OperationKind> = built.plan.operations.into_iter().map(|o| o.kind).collect();
assert_eq!(
kinds,
[
OperationKind::Strike {
revision: "r4".into(),
comment: Some("No.".into()),
},
OperationKind::Restore {
revision: "header1.r2".into(),
comment: None,
},
]
);
}
#[test]
fn strike_restore_and_visible_refuse_what_they_cannot_mean() {
for (verb, group, want) in [
(
Verb::Edit,
with("r4", |o| {
o.strike = true;
o.restore = true;
}),
"-p r4: --strike and --restore exclude each other",
),
(
Verb::Edit,
with("r4", |o| {
o.accept = true;
o.strike = true;
}),
"-p r4: --accept and --strike exclude each other",
),
(
Verb::Edit,
with("r4", |o| o.visible = true),
"-p r4: --visible goes with --reject",
),
(
Verb::Edit,
with("p0", |o| o.strike = true),
"-p p0: --strike takes a revision (-p r12)",
),
(
Verb::Edit,
with("r4", |o| {
o.strike = true;
o.anchor = Some("x".into());
}),
"-p r4: a revision takes --accept, --reject, --strike or --restore",
),
(
Verb::Edit,
with("r4", |o| {
o.reject = true;
o.content = Some("No.".into());
}),
"-p r4: --content on a revision is the comment of --strike, --restore or --reject --visible",
),
(
Verb::Add,
with("r4", |o| o.strike = true),
"-p r4: add resolves no revision; use edit -p r12 --accept, --reject, --strike or --restore",
),
] {
let got = plan_from_flags(verb, &[group], "A", None, None, b"").map(|_| ());
assert_eq!(got, Err(want.to_string()));
}
}
#[test]
fn reply_answers_a_comment_on_edit_and_add() {
let want = kind(Verb::Add, &with("c5", |o| o.content = Some("Yes.".into()))).unwrap();
for verb in [Verb::Edit, Verb::Add] {
assert_eq!(
kind(verb, &with("c5", |o| o.reply = Some("Yes.".into()))),
Ok(want.clone()),
"{verb:?}"
);
let refused = kind(verb, &with("#c5", |o| o.reply = Some("Yes.".into())));
assert!(
refused
.as_ref()
.is_err_and(|m| m.ends_with("the settled spelling is c5")),
"{refused:?}"
);
}
}
#[test]
fn revision_and_reply_flags_pair_only_with_their_kind() {
let refusal =
|verb: Verb, o: FlagOp| plan_from_flags(verb, &[o], "A", None, None, b"").unwrap_err();
let cases: Vec<(FlagOp, &str)> = vec![
(
with("p1", |o| o.accept = true),
"-p p1: --accept and --reject take a revision (-p r12)",
),
(
with("c5", |o| o.reject = true),
"-p c5: --accept and --reject take a revision (-p r12)",
),
(
with("r0", |o| o.content = Some("x".into())),
"-p r0: a revision takes --accept, --reject, --strike or --restore",
),
(
with("r0", |_| {}),
"-p r0: a revision takes --accept, --reject, --strike or --restore",
),
(
with("r0", |o| {
o.accept = true;
o.reject = true;
}),
"-p r0: --accept and --reject exclude each other",
),
(
with("r0", |o| {
o.accept = true;
o.delete = true;
}),
"-p r0: a revision takes --accept, --reject, --strike or --restore",
),
(
with("p1", |o| o.reply = Some("x".into())),
"-p p1: --reply is for a comment (-p c5)",
),
(
with("r1", |o| o.reply = Some("x".into())),
"-p r1: --reply is for a comment (-p c5)",
),
(
with("c5", |o| {
o.reply = Some("x".into());
o.content = Some("y".into());
}),
"-p c5: --reply takes no other flag",
),
(
with("c5", |o| {
o.reply = Some("x".into());
o.resolve = true;
}),
"-p c5: --reply takes no other flag",
),
(
with("c5", |o| {
o.reply = Some("x".into());
o.comment = true;
}),
"-p c5: --reply takes no other flag",
),
(
with("c5", |o| {
o.reply = Some("x".into());
o.accept = true;
}),
"-p c5: --reply takes no other flag",
),
(
with("c5", |o| {
o.reply = Some("x".into());
o.reject = true;
}),
"-p c5: --reply takes no other flag",
),
];
for (o, message) in cases {
assert_eq!(refusal(Verb::Edit, o), message);
}
assert_eq!(
refusal(
Verb::Add,
with("c5", |o| {
o.reply = Some("x".into());
o.before = true;
})
),
"-p c5: --reply takes no other flag"
);
for o in [
with("r1", |o| o.accept = true),
with("r1", |o| o.content = Some("x".into())),
] {
assert_eq!(
refusal(Verb::Add, o.clone()),
format!(
"-p {}: add resolves no revision; use edit -p r12 --accept, --reject, --strike or --restore",
o.at
)
);
}
assert_eq!(
refusal(Verb::Add, with("r1", |o| o.reply = Some("x".into()))),
"-p r1: --reply is for a comment (-p c5)"
);
assert_eq!(
refusal(Verb::Add, with("c5", |o| o.accept = true)),
"-p c5: --accept and --reject take a revision (-p r12)"
);
}
}
#[cfg(test)]
#[cfg_attr(all(coverage_nightly, test), coverage(off))]
mod flag_branch_tests {
use super::*;
use crate::changes::ChangeKind;
fn op(at: &str) -> FlagOp {
FlagOp {
at: at.into(),
..FlagOp::default()
}
}
fn with(at: &str, set: impl Fn(&mut FlagOp)) -> FlagOp {
let mut op = op(at);
set(&mut op);
op
}
fn refused(verb: Verb, op: FlagOp) -> String {
plan_from_flags(verb, &[op], "A", None, Some(ExistingRevisions::Keep), b"")
.expect_err("refused")
}
fn planned(verb: Verb, op: FlagOp) -> OperationKind {
let mut plan = plan_from_flags(verb, &[op], "A", None, Some(ExistingRevisions::Keep), b"")
.expect("planned")
.plan;
assert_eq!(plan.operations.len(), 1);
plan.operations.remove(0).kind
}
#[test]
fn occurrence_needs_an_anchor_and_counts_from_one() {
let e = refused(Verb::Edit, with("p1", |o| o.occurrence = Some(2)));
assert_eq!(e, "-p p1: --occurrence requires --anchor");
let e = refused(
Verb::Edit,
with("p1", |o| {
o.occurrence = Some(0);
o.anchor = Some("x".into());
o.delete = true;
}),
);
assert_eq!(e, "-p p1: --occurrence is 1-based");
}
#[test]
fn style_sizes_outside_the_word_range_are_refused() {
for size in ["0", "-3", "1639", "NaN", "inf", "big"] {
let e = refused(
Verb::Edit,
with("p1", |o| {
o.anchor = Some("x".into());
o.styles = vec![format!("size={size}")];
}),
);
assert_eq!(
e,
format!("-p p1: --style size={size}: points between 0 and 1638")
);
}
let kind = planned(
Verb::Edit,
with("p1", |o| {
o.anchor = Some("x".into());
o.styles = vec!["size=10.5".into()];
}),
);
assert!(
matches!(kind, OperationKind::FormatRun { ref format, .. } if format.size_pt == Some(HalfPoints(21)))
);
}
#[test]
fn a_reply_takes_no_revision_flag_or_paragraph_flag() {
let e = refused(
Verb::Edit,
with("c1", |o| {
o.reply = Some("ok".into());
o.visible = true;
}),
);
assert_eq!(e, "-p c1: --reply takes no other flag");
for set in [
(|o: &mut FlagOp| o.delete = true) as fn(&mut FlagOp),
|o| o.styles = vec!["bold".into()],
|o| o.content = Some("x".into()),
] {
let e = refused(
Verb::Edit,
with("c1", |o| {
o.reply = Some("ok".into());
set(o);
}),
);
assert_eq!(e, "-p c1: --reply takes no other flag");
}
}
#[test]
fn a_revision_group_takes_only_its_verb() {
for set in [
(|o: &mut FlagOp| o.resolve = true) as fn(&mut FlagOp),
|o| o.before = true,
|o| o.comment = true,
|o| o.styles = vec!["bold".into()],
] {
let e = refused(
Verb::Edit,
with("r1", |o| {
o.accept = true;
set(o);
}),
);
assert_eq!(
e,
"-p r1: a revision takes --accept, --reject, --strike or --restore"
);
}
}
#[test]
fn paragraph_flags_that_contradict_each_other_are_refused() {
let e = refused(Verb::Edit, op(" "));
assert_eq!(e, "-p needs a location");
let e = refused(
Verb::Edit,
with("p1", |o| {
o.delete = true;
o.resolve = true;
}),
);
assert_eq!(e, "-p p1: --delete excludes --content and --resolve");
for set in [
(|o: &mut FlagOp| o.content = Some("x".into())) as fn(&mut FlagOp),
|o| o.anchor = Some("x".into()),
|o| o.styles = vec!["bold".into()],
] {
let e = refused(
Verb::Edit,
with("p1", |o| {
o.resolve = true;
set(o);
}),
);
assert_eq!(
e,
"-p p1: --resolve takes no --anchor, --content or --style"
);
}
let e = refused(
Verb::Add,
with("p1", |o| {
o.before = true;
o.comment = true;
o.content = Some("x".into());
}),
);
assert_eq!(e, "-p p1: --before is for a new paragraph, not a comment");
for set in [(|o: &mut FlagOp| o.before = true) as fn(&mut FlagOp), |o| {
o.comment = true;
}] {
let e = refused(
Verb::Edit,
with("p1", |o| {
o.content = Some("x".into());
set(o);
}),
);
assert_eq!(e, "-p p1: --before and --comment belong to add");
}
}
#[test]
fn comment_groups_map_to_their_comment_operation() {
for set in [(|o: &mut FlagOp| o.before = true) as fn(&mut FlagOp), |o| {
o.comment = true;
}] {
let e = flag_operation(
Verb::Add,
&with("c1", |o| {
o.content = Some("x".into());
set(o);
}),
)
.expect_err("refused");
assert!(e.starts_with("-p c1"), "{e}");
}
let kind = planned(Verb::Edit, with("c4", |o| o.resolve = true));
assert!(matches!(
kind,
OperationKind::ResolveComment {
comment_id: 4,
done: true
}
));
}
#[test]
fn paragraph_styles_and_run_formats_fit_only_their_operations() {
let e = refused(
Verb::Edit,
with("p1", |o| {
o.anchor = Some("x".into());
o.content = Some("y".into());
o.styles = vec!["Heading1".into()];
}),
);
assert_eq!(e, "-p p1: a paragraph style takes no --anchor");
for style in ["Heading1", "bold"] {
let e = refused(
Verb::Edit,
with("p1", |o| {
o.content = Some("y".into());
o.styles = vec![style.into()];
}),
);
assert!(e.starts_with("-p p1: a rewrite takes no --style"), "{e}");
}
let e = refused(
Verb::Edit,
with("p1", |o| {
o.delete = true;
o.styles = vec!["Heading1".into()];
}),
);
assert_eq!(e, "-p p1: --delete takes no --style");
}
fn plan(reject: Option<ChangeFilter>, visible: bool) -> EditPlan {
let mut plan = plan_from_flags(
Verb::Edit,
&[with("p1", |o| o.content = Some("x".into()))],
"A",
None,
Some(ExistingRevisions::Keep),
b"",
)
.unwrap()
.plan;
plan.resolve_revisions = Some(ResolveRevisions {
accept: None,
reject,
reject_visible: visible,
});
plan
}
#[test]
fn recording_joins_visible_rejects_and_the_others_guard() {
let ids = |v: &[&str]| {
Some(ChangeFilter::ids(
v.iter().map(|s| s.to_string()).collect::<Vec<String>>(),
))
};
let merged =
record_into(Some(plan(ids(&["r1"]), true)), plan(ids(&["r2"]), true)).expect("joined");
let reject = merged.resolve_revisions.unwrap().reject.unwrap();
assert_eq!(reject.ids.unwrap(), ["r1", "r2"]);
let mut more = plan(None, false);
more.others = super::super::Others::Keep;
let merged = record_into(Some(plan(None, false)), more).unwrap();
assert_eq!(merged.others, super::super::Others::Keep);
for held in [
ChangeFilter {
ids: Some(vec!["r1".into()]),
authors: Some(vec!["Bo".into()]),
kinds: None,
},
ChangeFilter {
ids: Some(vec!["r1".into()]),
authors: None,
kinds: Some(vec![ChangeKind::Insertion]),
},
ChangeFilter::default(),
] {
let e = record_into(Some(plan(Some(held), false)), plan(ids(&["r2"]), false))
.expect_err("narrowing");
assert!(e.starts_with("the plan's reject selects revisions"), "{e}");
}
}
}