Skip to main content

objects/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    Signature(StateSignature),
36}
37
38#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
39pub struct StateAttachment {
40    pub state_id: StateId,
41    pub body: StateAttachmentBody,
42    pub attribution: Attribution,
43    pub created_at: DateTime<Utc>,
44    pub supersedes: Option<StateAttachmentId>,
45}
46
47impl StateAttachment {
48    pub fn id(&self) -> StateAttachmentId {
49        let bytes = rmp_serde::to_vec_named(self).expect("state attachment encoding is infallible");
50        StateAttachmentId::from_hash(ContentHash::compute_typed("state-attachment", &bytes))
51    }
52}