Skip to main content

heddle_object_model/object/
state_attachment.rs

1// SPDX-License-Identifier: Apache-2.0
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5
6use super::{Attribution, ContentHash, StateId, StateSignature};
7
8#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Ord, PartialOrd, Serialize, Deserialize)]
9#[serde(transparent)]
10pub struct StateAttachmentId(ContentHash);
11
12impl StateAttachmentId {
13    pub fn from_hash(hash: ContentHash) -> Self {
14        Self(hash)
15    }
16
17    pub fn as_hash(&self) -> &ContentHash {
18        &self.0
19    }
20}
21
22impl std::fmt::Display for StateAttachmentId {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        write!(f, "ha-{}", self.0.short())
25    }
26}
27
28#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
29pub enum StateAttachmentBody {
30    Context(ContentHash),
31    RiskSignals(ContentHash),
32    ReviewSignatures(ContentHash),
33    Discussions(ContentHash),
34    StructuredConflicts(ContentHash),
35    /// Content hash of the state's `SemanticIndexRoot` blob (heddle#1067).
36    SemanticIndex(ContentHash),
37    Signature(StateSignature),
38}
39
40/// The kind of a [`StateAttachmentBody`], with the payload projected away.
41///
42/// Kind is a pure function of the record: [`StateAttachmentBody::kind`] maps a
43/// body to its kind with no I/O and no ambiguity. This is the primitive that
44/// currency (last-attachment-of-a-kind) and supersession (same-kind guard) are
45/// expressed in terms of, and that the wire layer threads through
46/// `wire::ObjectId` (heddle#1080, Fable §B(1)).
47#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
48pub enum StateAttachmentKind {
49    Context,
50    RiskSignals,
51    ReviewSignatures,
52    Discussions,
53    StructuredConflicts,
54    SemanticIndex,
55    Signature,
56}
57
58impl StateAttachmentBody {
59    /// The [`StateAttachmentKind`] of this body — a pure projection that
60    /// discards the payload. Exhaustive by construction: adding a body variant
61    /// forces a matching kind arm here.
62    pub fn kind(&self) -> StateAttachmentKind {
63        match self {
64            StateAttachmentBody::Context(_) => StateAttachmentKind::Context,
65            StateAttachmentBody::RiskSignals(_) => StateAttachmentKind::RiskSignals,
66            StateAttachmentBody::ReviewSignatures(_) => StateAttachmentKind::ReviewSignatures,
67            StateAttachmentBody::Discussions(_) => StateAttachmentKind::Discussions,
68            StateAttachmentBody::StructuredConflicts(_) => StateAttachmentKind::StructuredConflicts,
69            StateAttachmentBody::SemanticIndex(_) => StateAttachmentKind::SemanticIndex,
70            StateAttachmentBody::Signature(_) => StateAttachmentKind::Signature,
71        }
72    }
73}
74
75#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
76pub struct StateAttachment {
77    pub state_id: StateId,
78    pub body: StateAttachmentBody,
79    pub attribution: Attribution,
80    pub created_at: DateTime<Utc>,
81    pub supersedes: Option<StateAttachmentId>,
82}
83
84impl StateAttachment {
85    /// Encode the canonical named-field msgpack representation shared by
86    /// packs and object transfer.
87    pub fn encode_current_msgpack(&self) -> crate::error::Result<Vec<u8>> {
88        Ok(rmp_serde::to_vec_named(self)?)
89    }
90
91    /// Decode the canonical named-field msgpack representation.
92    pub fn decode_current_msgpack(bytes: &[u8]) -> crate::error::Result<Self> {
93        Ok(rmp_serde::from_slice(bytes)?)
94    }
95
96    pub fn id(&self) -> StateAttachmentId {
97        let bytes = rmp_serde::to_vec_named(self).expect("state attachment encoding is infallible");
98        StateAttachmentId::from_hash(ContentHash::compute_typed("state-attachment", &bytes))
99    }
100}
101