use std::mem;
use std::str::FromStr;
use {
crate::{
sanitize::Sanitize,
},
serde_derive::{Serialize,Deserialize},
borsh::{
BorshDeserialize, BorshSerialize,
},
borsh_derive::BorshSchema,
std::{
fmt,
},
thiserror::Error,
};
pub const HASH_BYTES: usize = 32;
const MAX_BASE58_LEN: usize = 44;
#[derive(
Serialize,
Deserialize,
BorshSerialize,
BorshDeserialize,
BorshSchema,
Clone,
Copy,
Default,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
)]
#[repr(transparent)]
pub struct Hash(pub(crate) [u8; HASH_BYTES]);
impl Hash {
pub fn new(hash_slice: &[u8]) -> Self {
Hash(<[u8; HASH_BYTES]>::try_from(hash_slice).unwrap())
}
pub fn to_bytes(self) -> [u8; HASH_BYTES] {
self.0
}
}
impl Sanitize for Hash {}
impl From<[u8; HASH_BYTES]> for Hash {
fn from(from: [u8; 32]) -> Self {
Self(from)
}
}
impl AsRef<[u8]> for Hash {
fn as_ref(&self) -> &[u8] {
&self.0[..]
}
}
impl fmt::Debug for Hash {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", bs58::encode(self.0).into_string())
}
}
impl fmt::Display for Hash {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", bs58::encode(self.0).into_string())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum ParseHashError {
#[error("string decoded to wrong size for hash")]
WrongSize,
#[error("failed to decoded string to hash")]
Invalid,
}
impl FromStr for Hash {
type Err = ParseHashError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.len() > MAX_BASE58_LEN {
return Err(ParseHashError::WrongSize);
}
let bytes = bs58::decode(s)
.into_vec()
.map_err(|_| ParseHashError::Invalid)?;
if bytes.len() != mem::size_of::<Hash>() {
Err(ParseHashError::WrongSize)
} else {
Ok(Hash::new(&bytes))
}
}
}