Skip to content

Commit

Permalink
Just track when an edit occurs and not when created
Browse files Browse the repository at this point in the history
  • Loading branch information
evenorog committed Sep 22, 2023
1 parent b00a52f commit 7441f03
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 114 deletions.
2 changes: 1 addition & 1 deletion src/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::fmt::{self, Display, Formatter};
///
/// Not part of the API and can change at any time.
#[doc(hidden)]
#[derive(Clone, Copy, Debug)]
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct Add(pub char);

impl crate::Edit for Add {
Expand Down
25 changes: 10 additions & 15 deletions src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,30 @@ use std::time::SystemTime;
pub(crate) struct Entry<E> {
pub edit: E,
#[cfg(feature = "std")]
pub created_at: SystemTime,
#[cfg(feature = "std")]
pub updated_at: SystemTime,
pub edit_at: SystemTime,
}

impl<E: Edit> Entry<E> {
pub fn edit(&mut self, target: &mut E::Target) -> E::Output {
#[cfg(feature = "std")]
{
self.edit_at = SystemTime::now();
}
self.edit.edit(target)
}

pub fn undo(&mut self, target: &mut E::Target) -> E::Output {
#[cfg(feature = "std")]
{
self.updated_at = SystemTime::now();
self.edit_at = SystemTime::now();
}
self.edit.undo(target)
}

pub fn redo(&mut self, target: &mut E::Target) -> E::Output {
#[cfg(feature = "std")]
{
self.updated_at = SystemTime::now();
self.edit_at = SystemTime::now();
}
self.edit.redo(target)
}
Expand All @@ -45,7 +47,7 @@ impl<E: Edit> Entry<E> {
Merged::Yes => {
#[cfg(feature = "std")]
{
self.updated_at = other.updated_at;
self.edit_at = other.edit_at;
}
Merged::Yes
}
Expand All @@ -56,20 +58,13 @@ impl<E: Edit> Entry<E> {
}

impl<E> From<E> for Entry<E> {
#[cfg(feature = "std")]
fn from(edit: E) -> Self {
let now = SystemTime::now();
Entry {
edit,
created_at: now,
updated_at: now,
#[cfg(feature = "std")]
edit_at: SystemTime::UNIX_EPOCH,
}
}

#[cfg(not(feature = "std"))]
fn from(edit: E) -> Self {
Entry { edit }
}
}

impl<E: Display> Display for Entry<E> {
Expand Down
17 changes: 1 addition & 16 deletions src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use serde::{Deserialize, Serialize};
/// [`History`] maintains an undo tree containing every edit made to the target.
///
/// See [this](https://github.com/evenorog/undo/blob/master/examples/history.rs)
/// for an interactive example of the history tree.
/// example for an interactive view of the history tree.
///
/// # Examples
/// ```
Expand Down Expand Up @@ -385,21 +385,6 @@ impl<E> Branch<E> {
mod tests {
use crate::*;

struct Add(char);

impl Edit for Add {
type Target = String;
type Output = ();

fn edit(&mut self, s: &mut String) {
s.push(self.0);
}

fn undo(&mut self, s: &mut String) {
self.0 = s.pop().unwrap();
}
}

#[test]
fn go_to() {
// m
Expand Down
4 changes: 2 additions & 2 deletions src/history/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ impl<E: fmt::Display, S> Display<'_, E, S> {
if let Some(entry) = entry {
if self.format.detailed {
let st_fmt = self.st_fmt;
let updated_at = st_fmt(now, entry.updated_at);
self.format.elapsed(f, updated_at)?;
let string = st_fmt(now, entry.edit_at);
self.format.elapsed(f, string)?;
}
}

Expand Down
78 changes: 0 additions & 78 deletions src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,74 +386,6 @@ mod tests {
use alloc::string::String;
use alloc::vec::Vec;

enum Op {
Add(Add),
Del(Del),
}

impl Edit for Op {
type Target = String;
type Output = ();

fn edit(&mut self, s: &mut String) {
match self {
Op::Add(add) => add.edit(s),
Op::Del(del) => del.edit(s),
}
}

fn undo(&mut self, s: &mut String) {
match self {
Op::Add(add) => add.undo(s),
Op::Del(del) => del.undo(s),
}
}

fn merge(&mut self, edit: Self) -> Merged<Self>
where
Self: Sized,
{
match (self, edit) {
(Op::Add(_), Op::Del(_)) => Merged::Annul,
(Op::Del(Del(Some(a))), Op::Add(Add(b))) if a == &b => Merged::Annul,
(_, edit) => Merged::No(edit),
}
}
}

#[derive(Debug, PartialEq)]
struct Add(char);

impl Edit for Add {
type Target = String;
type Output = ();

fn edit(&mut self, s: &mut String) {
s.push(self.0);
}

fn undo(&mut self, s: &mut String) {
self.0 = s.pop().unwrap();
}
}

#[derive(Default)]
struct Del(Option<char>);

impl Edit for Del {
type Target = String;
type Output = ();

fn edit(&mut self, s: &mut String) {
self.0 = s.pop();
}

fn undo(&mut self, s: &mut String) {
let ch = self.0.unwrap();
s.push(ch);
}
}

#[test]
fn go_to() {
let mut target = String::new();
Expand Down Expand Up @@ -486,16 +418,6 @@ mod tests {
assert_eq!(record.index(), 3);
}

#[test]
fn annul() {
let mut target = String::new();
let mut record = Record::new();
record.edit(&mut target, Op::Add(Add('a')));
record.edit(&mut target, Op::Del(Del::default()));
record.edit(&mut target, Op::Add(Add('b')));
assert_eq!(record.len(), 1);
}

#[test]
fn edits() {
let mut target = String::new();
Expand Down
4 changes: 2 additions & 2 deletions src/record/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ impl<E: fmt::Display, S> Display<'_, E, S> {
if let Some(entry) = entry {
if self.format.detailed {
let st_fmt = self.st_fmt;
let updated_at = st_fmt(now, entry.updated_at);
self.format.elapsed(f, updated_at)?;
let string = st_fmt(now, entry.edit_at);
self.format.elapsed(f, string)?;
}
}

Expand Down

0 comments on commit 7441f03

Please sign in to comment.