~dkellner/chronofold

f5d93a9e94399228278c06396258d212448a9ef9 — Dominik Kellner 9 months ago b086ae9
Add `Session::insert` to insert changes without references
2 files changed, 10 insertions(+), 0 deletions(-)

M src/internal.rs
M src/session.rs
M src/internal.rs => src/internal.rs +1 -0
@@ 15,6 15,7 @@ impl<A: Author, T> Chronofold<A, T> {
    ) -> Option<LogIndex> {
        match (reference, change) {
            (None, Change::Root) => None,
            (None, Change::Insert(_)) => None,
            (_, Change::Root) => {
                // Roots cannot reference other entries.
                // XXX: Should we cover this by the type system?

M src/session.rs => src/session.rs +9 -0
@@ 113,6 113,15 @@ impl<'a, A: Author, T> Session<'a, A, T> {
            .apply_change(Timestamp(new_index, self.author), None, Change::Root)
    }

    pub fn insert(&mut self, value: T) -> LogIndex {
        let new_index = LogIndex(self.chronofold.log.len());
        self.chronofold.apply_change(
            Timestamp(new_index, self.author),
            None,
            Change::Insert(value),
        )
    }

    fn apply_change(&mut self, reference: LogIndex, change: Change<T>) -> LogIndex {
        self.apply_changes(reference, Some(change)).unwrap()
    }