p2panda_rs/entry/
encoded_entry.rs1use std::fmt::Display;
4use std::hash::Hash as StdHash;
5
6use bamboo_rs_core_ed25519_yasmf::ED25519_SIGNATURE_SIZE;
7use serde::{Deserialize, Serialize};
8
9use crate::entry::traits::AsEncodedEntry;
10use crate::hash::Hash;
11use crate::serde::{deserialize_hex, serialize_hex};
12
13pub const SIGNATURE_SIZE: usize = ED25519_SIGNATURE_SIZE;
15
16#[derive(Clone, Debug, PartialEq, Eq, StdHash, Serialize, Deserialize)]
25pub struct EncodedEntry(
26 #[serde(serialize_with = "serialize_hex", deserialize_with = "deserialize_hex")] Vec<u8>,
27);
28
29impl EncodedEntry {
30 pub fn from_bytes(bytes: &[u8]) -> Self {
35 Self(bytes.to_owned())
36 }
37}
38
39impl AsEncodedEntry for EncodedEntry {
40 fn hash(&self) -> Hash {
42 Hash::new_from_bytes(&self.0)
43 }
44
45 fn into_bytes(&self) -> Vec<u8> {
47 self.0.clone()
48 }
49
50 fn size(&self) -> u64 {
52 self.0.len() as u64
53 }
54}
55
56impl Display for EncodedEntry {
57 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58 write!(f, "{}", hex::encode(&self.0))
59 }
60}
61
62#[cfg(any(feature = "test-utils", test))]
63impl EncodedEntry {
64 pub fn new(bytes: &[u8]) -> EncodedEntry {
66 Self(bytes.to_owned())
67 }
68
69 pub fn from_hex(value: &str) -> EncodedEntry {
71 let bytes = hex::decode(value).expect("invalid hexadecimal value");
72 Self(bytes)
73 }
74}