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