ant_protocol/storage/address/
chunk.rs1use serde::{Deserialize, Serialize};
10use std::hash::Hash;
11use xor_name::XorName;
12
13use super::AddressParseError;
14
15#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
19pub struct ChunkAddress(XorName);
20
21impl ChunkAddress {
22 pub fn new(xor_name: XorName) -> Self {
24 Self(xor_name)
25 }
26
27 pub fn xorname(&self) -> &XorName {
29 &self.0
30 }
31
32 pub fn to_hex(&self) -> String {
34 hex::encode(self.0)
35 }
36
37 pub fn from_hex(hex: &str) -> Result<Self, AddressParseError> {
39 let bytes = hex::decode(hex)?;
40 let xor = XorName(
41 bytes
42 .try_into()
43 .map_err(|_| AddressParseError::InvalidLength)?,
44 );
45 Ok(Self(xor))
46 }
47}
48
49impl std::fmt::Display for ChunkAddress {
50 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51 write!(f, "{}", &self.to_hex())
52 }
53}
54
55impl std::fmt::Debug for ChunkAddress {
56 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57 write!(f, "{}", &self.to_hex())
58 }
59}