use crate::holochain_json_api::{error::JsonError, json::JsonString};
use multihash::{encode, Hash};
use rust_base58::ToBase58;
use std::fmt;
#[derive(
PartialOrd, PartialEq, Eq, Ord, Clone, Debug, Serialize, Deserialize, DefaultJson, Default, Hash,
)]
pub struct HashString(String);
impl fmt::Display for HashString {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<String> for HashString {
fn from(s: String) -> HashString {
HashString(s)
}
}
impl From<HashString> for String {
fn from(h: HashString) -> String {
h.0
}
}
impl<'a> From<&'a str> for HashString {
fn from(s: &str) -> HashString {
HashString::from(s.to_string())
}
}
impl HashString {
pub fn new() -> HashString {
HashString("".to_string())
}
pub fn encode_from_bytes(bytes: &[u8], hash_type: Hash) -> HashString {
HashString::from(encode(hash_type, bytes).unwrap().to_base58())
}
pub fn encode_from_str(s: &str, hash_type: Hash) -> HashString {
HashString::encode_from_bytes(s.as_bytes(), hash_type)
}
pub fn encode_from_json_string(json_string: JsonString, hash_type: Hash) -> HashString {
HashString::encode_from_str(&String::from(json_string), hash_type)
}
}
#[cfg(test)]
pub mod tests {
use super::*;
use crate::{cas::content::AddressableContent, eav::eavi::test_entry_a};
use multihash::Hash;
pub fn test_hash() -> HashString {
test_entry_a().address()
}
#[test]
fn from_string_test() {
assert_eq!(HashString::new(), HashString::from("".to_string()),);
assert_eq!(
test_hash(),
HashString::from(test_entry_a().address().to_string()),
);
}
#[test]
fn from_str_test() {
assert_eq!(HashString::new(), HashString::from(""));
assert_eq!(test_hash(), HashString::from(test_entry_a().address()),);
}
#[test]
fn bytes_to_b58_known_golang() {
assert_eq!(
HashString::encode_from_bytes(b"test data", Hash::SHA2256).to_string(),
"QmY8Mzg9F69e5P9AoQPYat655HEhc1TVGs11tmfNSzkqh2"
)
}
#[test]
fn str_to_b58_hash_known_golang() {
assert_eq!(
HashString::encode_from_str("test data", Hash::SHA2256).to_string(),
"QmY8Mzg9F69e5P9AoQPYat655HEhc1TVGs11tmfNSzkqh2"
);
}
#[test]
fn can_serialize_to_b58_hash() {
#[derive(Serialize, Deserialize, Debug, DefaultJson)]
struct Foo {
foo: u8,
};
assert_eq!(
"Qme7Bu4NVYMtpsRtb7e4yyhcbE1zdB9PsrKTdosaqF3Bu3",
HashString::encode_from_json_string(JsonString::from(Foo { foo: 5 }), Hash::SHA2256)
.to_string(),
);
}
}