1use crate::{
4 predicate::{PredicateDecodeError, PredicateEncodeError},
5 ContentAddress, PredicateAddress, Signature,
6};
7use core::{fmt, str};
8
9impl fmt::LowerHex for ContentAddress {
10 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
11 for byte in self.0 {
12 write!(f, "{byte:02x}")?;
13 }
14 Ok(())
15 }
16}
17
18impl fmt::LowerHex for Signature {
19 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
20 for byte in self.0 {
21 write!(f, "{byte:02x}")?;
22 }
23 write!(f, "{:02x}", self.1)?;
24 Ok(())
25 }
26}
27
28impl fmt::UpperHex for ContentAddress {
29 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
30 for byte in self.0 {
31 write!(f, "{byte:02X}")?;
32 }
33 Ok(())
34 }
35}
36
37impl fmt::UpperHex for Signature {
38 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39 for byte in self.0 {
40 write!(f, "{byte:02X}")?;
41 }
42 write!(f, "{:02X}", self.1)?;
43 Ok(())
44 }
45}
46
47impl fmt::Debug for ContentAddress {
48 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
49 write!(f, "{}", self)
50 }
51}
52
53impl fmt::Display for ContentAddress {
54 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
55 hex::encode_upper(self.0).fmt(f)
56 }
57}
58
59impl fmt::Display for PredicateAddress {
60 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
61 write!(f, "{}:{}", self.contract, self.predicate)
62 }
63}
64
65impl fmt::Display for Signature {
66 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
67 let bytes: [u8; 65] = self.clone().into();
68 hex::encode_upper(bytes).fmt(f)
69 }
70}
71
72impl fmt::Display for PredicateDecodeError {
73 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
74 write!(
75 f,
76 "{}",
77 match self {
78 PredicateDecodeError::BytesTooShort => "bytes too short",
79 }
80 )
81 }
82}
83
84impl fmt::Display for PredicateEncodeError {
85 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
86 write!(
87 f,
88 "{}",
89 match self {
90 PredicateEncodeError::TooManyNodes => "too many nodes",
91 PredicateEncodeError::TooManyEdges => "too many edges",
92 }
93 )
94 }
95}
96
97impl str::FromStr for ContentAddress {
98 type Err = hex::FromHexError;
99 fn from_str(s: &str) -> Result<Self, Self::Err> {
100 let vec = hex::decode(s)?;
101 let bytes: [u8; 32] = vec
102 .try_into()
103 .map_err(|_| hex::FromHexError::InvalidStringLength)?;
104 Ok(bytes.into())
105 }
106}
107
108impl str::FromStr for Signature {
109 type Err = hex::FromHexError;
110 fn from_str(s: &str) -> Result<Self, Self::Err> {
111 let vec = hex::decode(s)?;
112 let bytes: [u8; 65] = vec
113 .try_into()
114 .map_err(|_| hex::FromHexError::InvalidStringLength)?;
115 Ok(bytes.into())
116 }
117}