use std::fmt;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct NodeId(u64);
impl NodeId {
pub const fn index(self) -> u64 {
self.0
}
pub const fn from_index(index: u64) -> Self {
Self(index)
}
}
impl fmt::Display for NodeId {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "{}", self.0)
}
}
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct HistoryNode<S> {
pub id: NodeId,
pub parent: Option<NodeId>,
pub children: Vec<NodeId>,
pub label: String,
pub state: S,
pub group: Option<u64>,
pub timestamp: f64,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct GroupId(u64);
impl GroupId {
pub const fn index(self) -> u64 {
self.0
}
}
impl fmt::Display for GroupId {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "{}", self.0)
}
}
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum HistoryError {
#[error("unknown history node {0}")]
UnknownNode(NodeId),
#[error("the history is empty")]
Empty,
#[error("group {0} is already open")]
GroupAlreadyOpen(GroupId),
#[error("no group is open")]
NoGroup,
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct History<S> {
nodes: Vec<HistoryNode<S>>,
current: NodeId,
next_id: u64,
next_group: u64,
active_group: Option<GroupId>,
#[cfg_attr(feature = "serde", serde(skip))]
redo_to: Option<NodeId>,
}
impl<S> History<S> {
pub fn new(initial: S) -> Self {
Self {
nodes: vec![HistoryNode {
id: NodeId(0),
parent: None,
children: Vec::new(),
label: "Initial".to_owned(),
state: initial,
group: None,
timestamp: 0.0,
}],
current: NodeId(0),
next_id: 1,
next_group: 1,
active_group: None,
redo_to: None,
}
}
pub fn current(&self) -> NodeId {
self.current
}
#[allow(clippy::indexing_slicing)]
pub fn state(&self) -> &S {
&self.nodes[self.current.0 as usize].state
}
#[allow(clippy::indexing_slicing)]
pub fn current_node(&self) -> &HistoryNode<S> {
&self.nodes[self.current.0 as usize]
}
pub fn node(&self, id: NodeId) -> Option<&HistoryNode<S>> {
self.nodes.iter().find(|node| node.id == id)
}
pub fn nodes(&self) -> &[HistoryNode<S>] {
&self.nodes
}
pub fn len(&self) -> usize {
self.nodes.len()
}
pub fn is_empty(&self) -> bool {
self.nodes.len() <= 1
}
#[allow(clippy::indexing_slicing)]
pub fn can_undo(&self) -> bool {
self.nodes[self.current.0 as usize].parent.is_some()
}
#[allow(clippy::indexing_slicing)]
pub fn can_redo(&self) -> bool {
!self.nodes[self.current.0 as usize].children.is_empty()
}
pub fn group(&self) -> Option<GroupId> {
self.active_group
}
pub fn begin_group(&mut self, _label: &str) -> Result<GroupId, HistoryError> {
if let Some(group) = self.active_group {
return Err(HistoryError::GroupAlreadyOpen(group));
}
let group = GroupId(self.next_group);
self.next_group += 1;
self.active_group = Some(group);
Ok(group)
}
pub fn end_group(&mut self) -> Result<(), HistoryError> {
if self.active_group.take().is_none() {
return Err(HistoryError::NoGroup);
}
Ok(())
}
pub fn path(&self) -> Vec<NodeId> {
let mut path = Vec::new();
let mut node = Some(self.current);
while let Some(id) = node {
path.push(id);
node = self.node(id).and_then(|node| node.parent);
}
path.reverse();
path
}
#[allow(clippy::indexing_slicing)]
pub fn undo(&mut self) -> Option<&S> {
let parent = self.nodes[self.current.0 as usize].parent?;
self.redo_to = Some(self.current);
self.active_group = None;
self.current = parent;
Some(self.state())
}
#[allow(clippy::indexing_slicing)]
pub fn redo(&mut self) -> Option<&S> {
let remembered = self
.redo_to
.filter(|id| self.nodes[self.current.0 as usize].children.contains(id));
let child = match remembered {
Some(id) => id,
None => *self.nodes[self.current.0 as usize].children.last()?,
};
self.redo_to = None;
self.active_group = None;
self.current = child;
Some(self.state())
}
pub fn jump_to(&mut self, id: NodeId) -> Result<&S, HistoryError> {
if self.node(id).is_none() {
return Err(HistoryError::UnknownNode(id));
}
self.redo_to = None;
self.active_group = None;
self.current = id;
Ok(self.state())
}
}
impl<S: Clone + PartialEq> History<S> {
#[allow(clippy::indexing_slicing)]
pub fn record(&mut self, label: impl Into<String>, state: S) -> NodeId {
self.redo_to = None;
let label = label.into();
if let Some(group) = self.active_group
&& self.current != NodeId(0)
&& self.current_node().group == Some(group.index())
{
let node = &mut self.nodes[self.current.0 as usize];
node.state = state;
node.label = label;
return node.id;
}
if self.state() == &state {
return self.current;
}
self.record_with_group(label, state, self.active_group.map(GroupId::index), 0.0)
}
pub fn record_at(&mut self, label: impl Into<String>, state: S, timestamp: f64) -> NodeId {
self.record_with_group(label.into(), state, self.active_group.map(GroupId::index), timestamp)
}
#[allow(clippy::indexing_slicing)]
fn record_with_group(&mut self, label: String, state: S, group: Option<u64>, timestamp: f64) -> NodeId {
let id = NodeId(self.next_id);
self.next_id += 1;
self.nodes.push(HistoryNode {
id,
parent: Some(self.current),
children: Vec::new(),
label,
state,
group,
timestamp,
});
self.nodes[self.current.0 as usize].children.push(id);
self.current = id;
id
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn undo_and_branch_keep_the_abandoned_path() {
let mut history = History::new("a".to_owned());
let second = history.record("b", "ab".to_owned());
let third = history.record("c", "abc".to_owned());
assert!(history.can_undo());
assert!(!history.can_redo());
history.undo();
assert_eq!(history.state(), "ab");
let branch = history.record("!", "a!".to_owned());
assert_eq!(history.node(third).map(|node| node.label.as_str()), Some("c"));
assert_eq!(history.node(second).map(|node| node.children.len()), Some(2));
assert_eq!(history.node(branch).map(|node| node.state.as_str()), Some("a!"));
assert_eq!(history.len(), 4);
}
#[test]
fn redo_follows_the_last_child_and_jump_time_travels() {
let mut history = History::new(0);
history.record("one", 1);
let two = history.record("two", 2);
history.undo();
history.undo();
assert_eq!(history.state(), &0);
history.redo();
assert_eq!(history.state(), &1);
history.jump_to(two).expect("node exists");
assert_eq!(history.state(), &2);
assert!(history.jump_to(NodeId(99)).is_err());
}
#[test]
fn undo_then_redo_returns_to_the_undone_node() {
let mut history = History::new(0);
let branch = history.record("a", 1);
history.undo();
assert_eq!(history.redo(), Some(&1));
assert_eq!(history.current(), branch);
}
#[test]
fn a_group_coalesces_typing() {
let mut history = History::new(String::new());
history.begin_group("typing").expect("group");
let first = history.record("t", "t".to_owned());
let second = history.record("typing", "typing".to_owned());
assert_eq!(first, second, "the open group updates the same node");
history.end_group().expect("end");
history.record("!", "typing!".to_owned());
assert_eq!(history.len(), 3);
assert!(history.end_group().is_err());
}
#[test]
fn no_op_records_do_not_add_nodes() {
let mut history = History::new(1);
history.record("same", 1);
assert_eq!(history.len(), 1);
}
#[cfg(feature = "serde")]
#[test]
fn the_undo_tree_survives_json() {
let mut history = History::new("a".to_owned());
history.record("b", "ab".to_owned());
history.record("c", "abc".to_owned());
history.undo();
history.record("!", "a!".to_owned());
let json = serde_json::to_string(&history).expect("serialize");
let back: History<String> = serde_json::from_str(&json).expect("deserialize");
assert_eq!(back.state(), "a!");
assert_eq!(back.len(), history.len());
assert_eq!(back.current(), history.current());
}
}