Remove redundant `apply_one` function
Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
parent
0b3a32627b
commit
24f1de980e
|
|
@ -228,90 +228,84 @@ impl Patch {
|
||||||
/// Apply a list of operations to the state.
|
/// Apply a list of operations to the state.
|
||||||
pub fn apply(&mut self, ops: impl IntoIterator<Item = Op>) -> Result<(), ApplyError> {
|
pub fn apply(&mut self, ops: impl IntoIterator<Item = Op>) -> Result<(), ApplyError> {
|
||||||
for op in ops {
|
for op in ops {
|
||||||
self.apply_one(op)?;
|
let id = op.id();
|
||||||
}
|
let author = Author::new(op.author);
|
||||||
Ok(())
|
let timestamp = op.timestamp;
|
||||||
}
|
|
||||||
|
|
||||||
/// Apply a single op to the state.
|
match op.action {
|
||||||
pub fn apply_one(&mut self, op: Op) -> Result<(), ApplyError> {
|
Action::Edit {
|
||||||
let id = op.id();
|
title,
|
||||||
let author = Author::new(op.author);
|
description,
|
||||||
let timestamp = op.timestamp;
|
target,
|
||||||
|
} => {
|
||||||
match op.action {
|
self.title.set(title, op.clock);
|
||||||
Action::Edit {
|
self.description.set(description, op.clock);
|
||||||
title,
|
self.target.set(target, op.clock);
|
||||||
description,
|
|
||||||
target,
|
|
||||||
} => {
|
|
||||||
self.title.set(title, op.clock);
|
|
||||||
self.description.set(description, op.clock);
|
|
||||||
self.target.set(target, op.clock);
|
|
||||||
}
|
|
||||||
Action::Tag { add, remove } => {
|
|
||||||
for tag in add {
|
|
||||||
self.tags.insert(tag, op.clock);
|
|
||||||
}
|
}
|
||||||
for tag in remove {
|
Action::Tag { add, remove } => {
|
||||||
self.tags.remove(tag, op.clock);
|
for tag in add {
|
||||||
|
self.tags.insert(tag, op.clock);
|
||||||
|
}
|
||||||
|
for tag in remove {
|
||||||
|
self.tags.remove(tag, op.clock);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
Action::Revision { base, oid } => {
|
||||||
Action::Revision { base, oid } => {
|
self.revisions.insert(
|
||||||
self.revisions.insert(
|
id,
|
||||||
id,
|
Redactable::Present(Revision::new(author, base, oid, timestamp)),
|
||||||
Redactable::Present(Revision::new(author, base, oid, timestamp)),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
Action::Redact { revision } => {
|
|
||||||
if let Some(revision) = self.revisions.get_mut(&revision) {
|
|
||||||
revision.merge(Redactable::Redacted);
|
|
||||||
} else {
|
|
||||||
return Err(ApplyError::Missing(revision));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Action::Review {
|
|
||||||
revision,
|
|
||||||
ref comment,
|
|
||||||
verdict,
|
|
||||||
ref inline,
|
|
||||||
} => {
|
|
||||||
if let Some(Redactable::Present(revision)) = self.revisions.get_mut(&revision) {
|
|
||||||
revision.reviews.insert(
|
|
||||||
op.author,
|
|
||||||
Review::new(verdict, comment.to_owned(), inline.to_owned(), timestamp),
|
|
||||||
);
|
);
|
||||||
} else {
|
|
||||||
return Err(ApplyError::Missing(revision));
|
|
||||||
}
|
}
|
||||||
}
|
Action::Redact { revision } => {
|
||||||
Action::Merge { revision, commit } => {
|
if let Some(revision) = self.revisions.get_mut(&revision) {
|
||||||
if let Some(Redactable::Present(revision)) = self.revisions.get_mut(&revision) {
|
revision.merge(Redactable::Redacted);
|
||||||
revision.merges.insert(
|
} else {
|
||||||
Merge {
|
return Err(ApplyError::Missing(revision));
|
||||||
node: op.author,
|
}
|
||||||
commit,
|
}
|
||||||
|
Action::Review {
|
||||||
|
revision,
|
||||||
|
ref comment,
|
||||||
|
verdict,
|
||||||
|
ref inline,
|
||||||
|
} => {
|
||||||
|
if let Some(Redactable::Present(revision)) = self.revisions.get_mut(&revision) {
|
||||||
|
revision.reviews.insert(
|
||||||
|
op.author,
|
||||||
|
Review::new(verdict, comment.to_owned(), inline.to_owned(), timestamp),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return Err(ApplyError::Missing(revision));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Action::Merge { revision, commit } => {
|
||||||
|
if let Some(Redactable::Present(revision)) = self.revisions.get_mut(&revision) {
|
||||||
|
revision.merges.insert(
|
||||||
|
Merge {
|
||||||
|
node: op.author,
|
||||||
|
commit,
|
||||||
|
timestamp,
|
||||||
|
}
|
||||||
|
.into(),
|
||||||
|
op.clock,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return Err(ApplyError::Missing(revision));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Action::Thread { revision, action } => {
|
||||||
|
// TODO(cloudhead): Make sure we can deal with redacted revisions which are added
|
||||||
|
// to out of order, like in the `Merge` case.
|
||||||
|
if let Some(Redactable::Present(revision)) = self.revisions.get_mut(&revision) {
|
||||||
|
revision.discussion.apply([cob::Op {
|
||||||
|
action,
|
||||||
|
author: op.author,
|
||||||
|
clock: op.clock,
|
||||||
timestamp,
|
timestamp,
|
||||||
}
|
}]);
|
||||||
.into(),
|
} else {
|
||||||
op.clock,
|
return Err(ApplyError::Missing(revision));
|
||||||
);
|
}
|
||||||
} else {
|
|
||||||
return Err(ApplyError::Missing(revision));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Action::Thread { revision, action } => {
|
|
||||||
// TODO(cloudhead): Make sure we can deal with redacted revisions which are added
|
|
||||||
// to out of order, like in the `Merge` case.
|
|
||||||
if let Some(Redactable::Present(revision)) = self.revisions.get_mut(&revision) {
|
|
||||||
revision.discussion.apply([cob::Op {
|
|
||||||
action,
|
|
||||||
author: op.author,
|
|
||||||
clock: op.clock,
|
|
||||||
timestamp,
|
|
||||||
}]);
|
|
||||||
} else {
|
|
||||||
return Err(ApplyError::Missing(revision));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue