Skip to content

Commit

Permalink
Expose Entry struct in api
Browse files Browse the repository at this point in the history
  • Loading branch information
evenorog committed Sep 28, 2023
1 parent 17fcba1 commit a7b0985
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 33 deletions.
27 changes: 20 additions & 7 deletions src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,51 @@ use std::time::SystemTime;
/// Wrapper around an [`Edit`] command that contains additional metadata.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug)]
pub(crate) struct Entry<E> {
pub edit: E,
pub struct Entry<E> {
edit: E,
#[cfg(feature = "std")]
pub edit_at: SystemTime,
edit_at: SystemTime,
}

impl<E> Entry<E> {
/// Returns the edit command.
pub fn get(&self) -> &E {
&self.edit
}

/// Returns the last time the edit was applied.
#[cfg(feature = "std")]
pub(crate) fn edit_at(&self) -> SystemTime {
self.edit_at
}
}

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

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

pub fn merge(&mut self, other: Self) -> Merged<Self>
pub(crate) fn merge(&mut self, other: Self) -> Merged<Self>
where
Self: Sized,
{
Expand Down
21 changes: 11 additions & 10 deletions src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ pub use queue::Queue;
use crate::socket::Slot;
use crate::{At, Edit, Entry, Event, Record};
use alloc::collections::{BTreeMap, VecDeque};
use alloc::string::{String, ToString};
use alloc::string::String;
use alloc::vec::Vec;
use core::fmt;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -152,13 +153,13 @@ impl<E, S> History<E, S> {
/// Returns the edit at the index in the current root branch.
///
/// Use [History::get_branch] if you want to get edits from other branches.
pub fn get_edit(&self, index: usize) -> Option<&E> {
self.record.get_edit(index)
pub fn entry(&self, index: usize) -> Option<&Entry<E>> {
self.record.entry(index)
}

/// Returns an iterator over the edits in the current root branch.
pub fn edits(&self) -> impl Iterator<Item = &E> {
self.record.edits()
pub fn entries(&self) -> impl Iterator<Item = &Entry<E>> {
self.record.entries()
}

/// Returns the branch with the given id.
Expand Down Expand Up @@ -364,7 +365,7 @@ impl<E: Edit, S: Slot> History<E, S> {
}
}

impl<E: ToString, S> History<E, S> {
impl<E: fmt::Display, S> History<E, S> {
/// Returns the string of the edit which will be undone
/// in the next call to [`History::undo`].
pub fn undo_string(&self) -> Option<String> {
Expand Down Expand Up @@ -428,12 +429,12 @@ impl<E> Branch<E> {
}

/// Returns the edit at the index.
pub fn get_edit(&self, index: usize) -> Option<&E> {
self.entries.get(index).map(|e| &e.edit)
pub fn get_entry(&self, index: usize) -> Option<&Entry<E>> {
self.entries.get(index)
}

/// Returns an iterator over the edits in the branch.
pub fn edits(&self) -> impl Iterator<Item = &E> {
self.entries.iter().map(|e| &e.edit)
pub fn entries(&self) -> impl Iterator<Item = &Entry<E>> {
self.entries.iter()
}
}
2 changes: 1 addition & 1 deletion src/history/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl<E: fmt::Display, S> Display<'_, E, S> {
if let Some(entry) = entry {
if self.format.detailed {
let st_fmt = self.st_fmt;
let string = st_fmt(now, entry.edit_at);
let string = st_fmt(now, entry.edit_at());
self.format.elapsed(f, string)?;
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,23 @@ pub mod record;
#[cfg(feature = "alloc")]
mod socket;

#[cfg(feature = "alloc")]
use entry::Entry;
#[cfg(feature = "alloc")]
use format::Format;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[doc(hidden)]
#[cfg(feature = "alloc")]
pub use add::Add;
#[cfg(feature = "alloc")]
pub use entry::Entry;
#[cfg(feature = "alloc")]
pub use history::History;
#[cfg(feature = "alloc")]
pub use record::Record;
#[cfg(feature = "alloc")]
pub use socket::{Event, Slot};

#[cfg(feature = "alloc")]
use format::Format;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Base functionality for all edit commands.
pub trait Edit {
/// The target type.
Expand Down
13 changes: 7 additions & 6 deletions src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{Edit, Entry, Event, Merged};
use alloc::collections::VecDeque;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::fmt;
use core::num::NonZeroUsize;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -152,13 +153,13 @@ impl<E, S> Record<E, S> {
}

/// Returns the edit at the index.
pub fn get_edit(&self, index: usize) -> Option<&E> {
self.entries.get(index).map(|e| &e.edit)
pub fn entry(&self, index: usize) -> Option<&Entry<E>> {
self.entries.get(index)
}

/// Returns an iterator over the edits.
pub fn edits(&self) -> impl Iterator<Item = &E> {
self.entries.iter().map(|e| &e.edit)
pub fn entries(&self) -> impl Iterator<Item = &Entry<E>> {
self.entries.iter()
}

/// Returns a queue.
Expand Down Expand Up @@ -365,7 +366,7 @@ impl<E: Edit, S: Slot> Record<E, S> {
}
}

impl<E: ToString, S> Record<E, S> {
impl<E: fmt::Display, S> Record<E, S> {
/// Returns the string of the edit which will be undone
/// in the next call to [`Record::undo`].
pub fn undo_string(&self) -> Option<String> {
Expand All @@ -379,7 +380,7 @@ impl<E: ToString, S> Record<E, S> {
}

fn string_at(&self, i: usize) -> Option<String> {
self.entries.get(i).map(|e| e.edit.to_string())
self.entries.get(i).map(|e| e.to_string())
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/record/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl<E: fmt::Display, S> Display<'_, E, S> {
if let Some(entry) = entry {
if self.format.detailed {
let st_fmt = self.st_fmt;
let string = st_fmt(now, entry.edit_at);
let string = st_fmt(now, entry.edit_at());
self.format.elapsed(f, string)?;
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn edits() {
let mut record = Record::new();
record.edit(&mut target, A);
record.edit(&mut target, B);
let collected = record.edits().collect::<Vec<_>>();
let collected = record.entries().map(|e| e.get()).collect::<Vec<_>>();
assert_eq!(&collected[..], &[&A, &B][..]);
}

Expand Down

0 comments on commit a7b0985

Please sign in to comment.