Skip to main content

khive_types/
header.rs

1//! Common header for substrate records.
2
3use crate::{Id128, Namespace, Timestamp};
4
5/// Fields shared by every substrate record (Note, Entity, Event).
6#[derive(Clone, Debug)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8pub struct Header {
9    /// Unique identifier for this record.
10    pub id: Id128,
11    /// Namespace that owns and isolates this record.
12    pub namespace: Namespace,
13    /// Wall-clock time when this record was first created.
14    pub created_at: Timestamp,
15    /// Wall-clock time of the most recent mutation.
16    pub updated_at: Timestamp,
17}
18
19impl Header {
20    /// Construct a new header with `created_at` and `updated_at` both set to `now`.
21    pub fn new(id: Id128, namespace: Namespace, now: Timestamp) -> Self {
22        Self {
23            id,
24            namespace,
25            created_at: now,
26            updated_at: now,
27        }
28    }
29
30    /// Advance `updated_at` to `now`, preserving `created_at`.
31    pub fn touch(&mut self, now: Timestamp) {
32        self.updated_at = now;
33    }
34}