tentacle_multiaddr/
onion_addr.rs1use std::{borrow::Cow, fmt};
2
3use data_encoding::BASE32;
4
5#[derive(Clone)]
7pub struct Onion3Addr<'a>(Cow<'a, [u8; 35]>, u16);
8
9impl Onion3Addr<'_> {
10 pub fn hash(&self) -> &[u8; 35] {
12 self.0.as_ref()
13 }
14
15 pub fn port(&self) -> u16 {
17 self.1
18 }
19
20 pub fn acquire<'b>(self) -> Onion3Addr<'b> {
22 Onion3Addr(Cow::Owned(self.0.into_owned()), self.1)
23 }
24
25 pub fn hash_string(&self) -> String {
26 let s = BASE32.encode(self.hash());
27 s.to_lowercase()
28 }
29}
30
31impl PartialEq for Onion3Addr<'_> {
32 fn eq(&self, other: &Self) -> bool {
33 self.1 == other.1 && self.0[..] == other.0[..]
34 }
35}
36
37impl Eq for Onion3Addr<'_> {}
38
39impl From<([u8; 35], u16)> for Onion3Addr<'_> {
40 fn from(parts: ([u8; 35], u16)) -> Self {
41 Self(Cow::Owned(parts.0), parts.1)
42 }
43}
44
45impl<'a> From<(&'a [u8; 35], u16)> for Onion3Addr<'a> {
46 fn from(parts: (&'a [u8; 35], u16)) -> Self {
47 Self(Cow::Borrowed(parts.0), parts.1)
48 }
49}
50
51impl fmt::Debug for Onion3Addr<'_> {
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
53 f.debug_tuple("Onion3Addr")
54 .field(&format!("{:02x?}", &self.0[..]))
55 .field(&self.1)
56 .finish()
57 }
58}