p2panda_rs/document/
document_view_hash.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3use std::fmt::Display;
4
5use crate::document::DocumentViewId;
6use crate::hash::{Hash, HashId};
7
8/// Contains a hash over the sorted graph tips constituting this view id.
9///
10/// Use this as a unique identifier for a document if you need a value with a limited size. The
11/// document view id itself grows with the number of graph tips that the document has, which may
12/// not be desirable for an identifier.
13///
14/// Keep in mind that when you refer to document views with this hash value it will not be possible
15/// to recover the document view id from it.
16#[derive(Clone, Debug, PartialEq, Eq)]
17pub struct DocumentViewHash(Hash);
18
19impl DocumentViewHash {
20    /// Creates a new instance of `DocumentViewHash`.
21    pub fn new(hash: Hash) -> Self {
22        Self(hash)
23    }
24
25    /// Returns string representation of the document view hash as `&str`.
26    pub fn as_str(&self) -> &str {
27        self.0.as_str()
28    }
29}
30
31impl Display for DocumentViewHash {
32    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        write!(f, "{}", self.as_str())
34    }
35}
36
37impl From<Hash> for DocumentViewHash {
38    fn from(hash: Hash) -> Self {
39        Self::new(hash)
40    }
41}
42
43impl From<&DocumentViewId> for DocumentViewHash {
44    fn from(document_view_id: &DocumentViewId) -> Self {
45        let graph_tip_bytes: Vec<u8> = document_view_id
46            .iter()
47            .flat_map(|graph_tip| graph_tip.to_bytes())
48            .collect();
49
50        Self::new(Hash::new_from_bytes(&graph_tip_bytes))
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use rstest::rstest;
57
58    use crate::document::DocumentViewId;
59    use crate::hash::Hash;
60    use crate::operation::OperationId;
61    use crate::test_utils::fixtures::{random_hash, random_operation_id};
62
63    use super::DocumentViewHash;
64
65    #[rstest]
66    fn equality_after_conversion(
67        #[from(random_operation_id)] operation_id_1: OperationId,
68        #[from(random_operation_id)] operation_id_2: OperationId,
69    ) {
70        let view_id_1 = DocumentViewId::new(&[operation_id_1.clone(), operation_id_2.clone()]);
71        let view_hash_1 = DocumentViewHash::from(&view_id_1);
72        let view_id_2 = DocumentViewId::new(&[operation_id_2, operation_id_1]);
73        let view_hash_2 = DocumentViewHash::from(&view_id_2);
74        assert_eq!(view_hash_1, view_hash_2);
75    }
76
77    #[rstest]
78    fn string_representation(#[from(random_hash)] hash: Hash) {
79        let document_view_hash = DocumentViewHash::new(hash.clone());
80        assert_eq!(hash.as_str(), document_view_hash.as_str());
81        assert_eq!(hash.as_str(), &document_view_hash.to_string());
82        assert_eq!(
83            format!("{}", document_view_hash),
84            document_view_hash.as_str()
85        )
86    }
87}