noosphere_core/data/
changelog.rs

1use anyhow::Result;
2use serde::{Deserialize, Serialize};
3use std::default::Default;
4
5#[cfg(doc)]
6use crate::data::{Did, VersionedMapIpld};
7
8/// A [ChangelogIpld] records a series of changes that represent the delta of a
9/// given [VersionedMapIpld] from its immediate ancestor
10#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
11pub struct ChangelogIpld<Op> {
12    /// The [Did] of the author of the change
13    pub did: Option<String>,
14    /// The changes that were made to the associated [VersionedMapIpld]
15    pub changes: Vec<Op>,
16}
17
18impl<Op> ChangelogIpld<Op> {
19    /// Returns true if the [ChangelogIpld] represents zero changes
20    pub fn is_empty(&self) -> bool {
21        self.changes.len() == 0
22    }
23
24    /// Adds a single change to the [ChangelogIpld]
25    pub fn push(&mut self, op: Op) -> Result<()> {
26        self.changes.push(op);
27        Ok(())
28    }
29
30    /// Initializes a [ChangelogIpld] for the author with the given [Did]
31    pub fn mark(&self, did: &str) -> Self {
32        ChangelogIpld {
33            did: Some(did.to_string()),
34            changes: Vec::new(),
35        }
36    }
37}
38
39impl<Op> Default for ChangelogIpld<Op> {
40    fn default() -> Self {
41        Self {
42            did: None,
43            changes: Vec::new(),
44        }
45    }
46}