@@ -13,10 +13,6 @@ use libp2p_comit::frame::Header;
13
13
use serde:: de:: Error ;
14
14
use std:: fmt;
15
15
16
- fn fail_serialize_unknown < D : fmt:: Debug > ( unknown : D ) -> serde_json:: Error {
17
- :: serde:: de:: Error :: custom ( format ! ( "serialization of {:?} is undefined." , unknown) )
18
- }
19
-
20
16
impl FromHeader for LedgerKind {
21
17
fn from_header ( mut header : Header ) -> Result < Self , serde_json:: Error > {
22
18
Ok ( match header. value :: < String > ( ) ?. as_str ( ) {
@@ -33,7 +29,12 @@ impl FromHeader for LedgerKind {
33
29
} ,
34
30
) ) ,
35
31
"ethereum" => LedgerKind :: Ethereum ( Ethereum :: new ( header. take_parameter ( "network" ) ?) ) ,
36
- other => LedgerKind :: Unknown ( other. to_string ( ) ) ,
32
+ unknown => {
33
+ return Err ( serde_json:: Error :: custom ( format ! (
34
+ "unknown ledger: {}" ,
35
+ unknown
36
+ ) ) )
37
+ }
37
38
} )
38
39
}
39
40
}
@@ -52,7 +53,6 @@ impl ToHeader for LedgerKind {
52
53
LedgerKind :: Ethereum ( ethereum) => {
53
54
Header :: with_str_value ( "ethereum" ) . with_parameter ( "network" , ethereum. chain_id ) ?
54
55
}
55
- unknown @ LedgerKind :: Unknown ( _) => return Err ( fail_serialize_unknown ( unknown) ) ,
56
56
} )
57
57
}
58
58
}
@@ -73,7 +73,12 @@ impl FromHeader for SwapProtocol {
73
73
fn from_header ( mut header : Header ) -> Result < Self , serde_json:: Error > {
74
74
Ok ( match header. value :: < String > ( ) ?. as_str ( ) {
75
75
"comit-rfc-003" => SwapProtocol :: Rfc003 ( header. take_parameter ( "hash_function" ) ?) ,
76
- other => SwapProtocol :: Unknown ( other. to_string ( ) ) ,
76
+ unknown => {
77
+ return Err ( serde_json:: Error :: custom ( format ! (
78
+ "unknown swap protocol: {}" ,
79
+ unknown
80
+ ) ) )
81
+ }
77
82
} )
78
83
}
79
84
}
@@ -83,7 +88,6 @@ impl ToHeader for SwapProtocol {
83
88
Ok ( match self {
84
89
SwapProtocol :: Rfc003 ( hash_function) => Header :: with_str_value ( "comit-rfc-003" )
85
90
. with_parameter ( "hash_function" , hash_function) ?,
86
- unknown @ SwapProtocol :: Unknown ( _) => return Err ( fail_serialize_unknown ( unknown) ) ,
87
91
} )
88
92
}
89
93
}
@@ -103,7 +107,12 @@ impl FromHeader for AssetKind {
103
107
header. take_parameter ( "address" ) ?,
104
108
header. take_parameter ( "quantity" ) ?,
105
109
) ) ,
106
- other => AssetKind :: Unknown ( other. to_string ( ) ) ,
110
+ unknown => {
111
+ return Err ( serde_json:: Error :: custom ( format ! (
112
+ "unknown asset: {}" ,
113
+ unknown
114
+ ) ) )
115
+ }
107
116
} )
108
117
}
109
118
}
@@ -119,7 +128,6 @@ impl ToHeader for AssetKind {
119
128
AssetKind :: Erc20 ( erc20) => Header :: with_str_value ( "erc20" )
120
129
. with_parameter ( "address" , erc20. token_contract ) ?
121
130
. with_parameter ( "quantity" , erc20. quantity ) ?,
122
- unknown @ AssetKind :: Unknown ( _) => return Err ( fail_serialize_unknown ( unknown) ) ,
123
131
} )
124
132
}
125
133
}
@@ -151,7 +159,6 @@ mod tests {
151
159
swap_protocols:: { ledger:: ethereum, HashFunction } ,
152
160
} ;
153
161
use bitcoin:: Amount ;
154
- use spectral:: prelude:: * ;
155
162
156
163
#[ test]
157
164
fn erc20_quantity_to_header ( ) -> Result < ( ) , serde_json:: Error > {
@@ -171,15 +178,6 @@ mod tests {
171
178
Ok ( ( ) )
172
179
}
173
180
174
- #[ test]
175
- fn serializing_unknown_ledgerkind_doesnt_panic ( ) {
176
- let ledger_kind = LedgerKind :: Unknown ( "USD" . to_string ( ) ) ;
177
-
178
- let header = ledger_kind. to_header ( ) ;
179
-
180
- assert_that ( & header) . is_err ( ) ;
181
- }
182
-
183
181
#[ test]
184
182
fn swap_protocol_to_header ( ) {
185
183
// From comit-network/RFCs/RFC-003-SWAP-Basic.md SWAP REQUEST example.
0 commit comments