use crate::{
types::identifiers::{id::Id, ID_LEN},
Result,
};
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};
#[cfg(feature = "metrics")]
use prometheus_client::encoding::text::Encode;
#[repr(transparent)]
#[derive(Copy, Clone, Debug, Hash, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize)]
pub struct Address(Id);
impl Display for Address {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl Address {
pub fn expand_input(input: &Vec<u8>) -> Self {
warn!("Address::expand_input will change semantically in a future version!");
Self(Id::with_digest(input))
}
#[deprecated]
pub fn random() -> Self {
warn!("Generating a random Address is deprecated and will be removed in a future version of this crate!");
Self(Id::random())
}
pub fn from_string(s: &String) -> Self {
Self(Id::from_string(s))
}
pub fn try_from_bytes(buf: &[u8]) -> Result<Self> {
Id::try_from_bytes(buf).map(|id| Self(id))
}
pub fn from_bytes(buf: &[u8]) -> Self {
Self(Id::from_bytes(buf))
}
pub fn as_bytes(&self) -> &[u8] {
self.0.as_bytes()
}
}
impl From<[u8; ID_LEN]> for Address {
fn from(i: [u8; ID_LEN]) -> Self {
Self(i.into())
}
}
impl From<&[u8; ID_LEN]> for Address {
fn from(i: &[u8; ID_LEN]) -> Self {
Self(i.clone().into())
}
}
#[cfg(feature = "metrics")]
impl Encode for Address {
fn encode(&self, w: &mut dyn std::io::Write) -> std::io::Result<()> {
self.0.encode(w)
}
}