use serde::{Deserialize, Serialize};
use std::hash::Hash;
use xor_name::XorName;
use super::AddressParseError;
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub struct ChunkAddress(XorName);
impl ChunkAddress {
pub fn new(xor_name: XorName) -> Self {
Self(xor_name)
}
pub fn xorname(&self) -> &XorName {
&self.0
}
pub fn to_hex(&self) -> String {
hex::encode(self.0)
}
pub fn from_hex(hex: &str) -> Result<Self, AddressParseError> {
let bytes = hex::decode(hex)?;
let xor = XorName(
bytes
.try_into()
.map_err(|_| AddressParseError::InvalidLength)?,
);
Ok(Self(xor))
}
}
impl std::fmt::Display for ChunkAddress {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", &self.to_hex())
}
}
impl std::fmt::Debug for ChunkAddress {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", &self.to_hex())
}
}