From a7b0985789ea8bbbc1795dce04922817508f33ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Even=20Olsson=20Rogstadkj=C3=A6rnet?= Date: Thu, 28 Sep 2023 21:47:20 +0200 Subject: [PATCH] Expose Entry struct in api --- src/entry.rs | 27 ++++++++++++++++++++------- src/history.rs | 21 +++++++++++---------- src/history/display.rs | 2 +- src/lib.rs | 14 +++++++------- src/record.rs | 13 +++++++------ src/record/display.rs | 2 +- tests/record.rs | 2 +- 7 files changed, 48 insertions(+), 33 deletions(-) diff --git a/src/entry.rs b/src/entry.rs index 3a45e06..a4c5d8f 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -8,14 +8,27 @@ 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 { - pub edit: E, +pub struct Entry { + edit: E, #[cfg(feature = "std")] - pub edit_at: SystemTime, + edit_at: SystemTime, +} + +impl Entry { + /// 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 Entry { - 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(); @@ -23,7 +36,7 @@ impl Entry { 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(); @@ -31,7 +44,7 @@ impl Entry { 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(); @@ -39,7 +52,7 @@ impl Entry { self.edit.redo(target) } - pub fn merge(&mut self, other: Self) -> Merged + pub(crate) fn merge(&mut self, other: Self) -> Merged where Self: Sized, { diff --git a/src/history.rs b/src/history.rs index 5dd2e0d..8714402 100644 --- a/src/history.rs +++ b/src/history.rs @@ -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}; @@ -152,13 +153,13 @@ impl History { /// 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> { + self.record.entry(index) } /// Returns an iterator over the edits in the current root branch. - pub fn edits(&self) -> impl Iterator { - self.record.edits() + pub fn entries(&self) -> impl Iterator> { + self.record.entries() } /// Returns the branch with the given id. @@ -364,7 +365,7 @@ impl History { } } -impl History { +impl History { /// Returns the string of the edit which will be undone /// in the next call to [`History::undo`]. pub fn undo_string(&self) -> Option { @@ -428,12 +429,12 @@ impl Branch { } /// 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> { + self.entries.get(index) } /// Returns an iterator over the edits in the branch. - pub fn edits(&self) -> impl Iterator { - self.entries.iter().map(|e| &e.edit) + pub fn entries(&self) -> impl Iterator> { + self.entries.iter() } } diff --git a/src/history/display.rs b/src/history/display.rs index cb6df0e..6d7de3b 100644 --- a/src/history/display.rs +++ b/src/history/display.rs @@ -69,7 +69,7 @@ impl 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)?; } } diff --git a/src/lib.rs b/src/lib.rs index f38a6f3..5538589 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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. diff --git a/src/record.rs b/src/record.rs index cb87648..670c8e9 100644 --- a/src/record.rs +++ b/src/record.rs @@ -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}; @@ -152,13 +153,13 @@ impl Record { } /// 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> { + self.entries.get(index) } /// Returns an iterator over the edits. - pub fn edits(&self) -> impl Iterator { - self.entries.iter().map(|e| &e.edit) + pub fn entries(&self) -> impl Iterator> { + self.entries.iter() } /// Returns a queue. @@ -365,7 +366,7 @@ impl Record { } } -impl Record { +impl Record { /// Returns the string of the edit which will be undone /// in the next call to [`Record::undo`]. pub fn undo_string(&self) -> Option { @@ -379,7 +380,7 @@ impl Record { } fn string_at(&self, i: usize) -> Option { - self.entries.get(i).map(|e| e.edit.to_string()) + self.entries.get(i).map(|e| e.to_string()) } } diff --git a/src/record/display.rs b/src/record/display.rs index 7e8b902..8d5d41f 100644 --- a/src/record/display.rs +++ b/src/record/display.rs @@ -67,7 +67,7 @@ impl 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)?; } } diff --git a/tests/record.rs b/tests/record.rs index d5a8bcd..3dbe728 100644 --- a/tests/record.rs +++ b/tests/record.rs @@ -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::>(); + let collected = record.entries().map(|e| e.get()).collect::>(); assert_eq!(&collected[..], &[&A, &B][..]); }