use std::fmt::Display;
use std::hash::Hash as StdHash;
use bamboo_rs_core_ed25519_yasmf::ED25519_SIGNATURE_SIZE;
use serde::{Deserialize, Serialize};
use crate::entry::traits::AsEncodedEntry;
use crate::hash::Hash;
use crate::serde::{deserialize_hex, serialize_hex};
pub const SIGNATURE_SIZE: usize = ED25519_SIGNATURE_SIZE;
#[derive(Clone, Debug, PartialEq, Eq, StdHash, Serialize, Deserialize)]
pub struct EncodedEntry(
#[serde(serialize_with = "serialize_hex", deserialize_with = "deserialize_hex")] Vec<u8>,
);
impl EncodedEntry {
pub fn from_bytes(bytes: &[u8]) -> Self {
Self(bytes.to_owned())
}
}
impl AsEncodedEntry for EncodedEntry {
fn hash(&self) -> Hash {
Hash::new_from_bytes(&self.0)
}
fn into_bytes(&self) -> Vec<u8> {
self.0.clone()
}
fn size(&self) -> u64 {
self.0.len() as u64
}
}
impl Display for EncodedEntry {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", hex::encode(&self.0))
}
}
#[cfg(any(feature = "test-utils", test))]
impl EncodedEntry {
pub fn new(bytes: &[u8]) -> EncodedEntry {
Self(bytes.to_owned())
}
pub fn from_hex(value: &str) -> EncodedEntry {
let bytes = hex::decode(value).expect("invalid hexadecimal value");
Self(bytes)
}
}