11use super :: * ;
22#[ cfg( not( feature = "std" ) ) ]
33use alloc:: {
4- borrow:: ToOwned ,
4+ borrow:: { Cow , ToOwned } ,
55 string:: { String , ToString } ,
66} ;
7+ #[ cfg( feature = "std" ) ]
8+ use std:: borrow:: Cow ;
79
8- use schemars:: { gen :: SchemaGenerator , schema :: Schema , JsonSchema } ;
10+ use schemars:: { json_schema , JsonSchema , Schema , SchemaGenerator } ;
911
1012impl JsonSchema for H160 {
11- fn schema_name ( ) -> String {
12- "HexEncoded20Bytes" . to_owned ( )
13+ fn schema_name ( ) -> Cow < ' static , str > {
14+ "HexEncoded20Bytes" . into ( )
1315 }
1416
15- fn json_schema ( gen : & mut SchemaGenerator ) -> Schema {
16- let mut schema = gen . subschema_for :: < String > ( ) . into_object ( ) ;
17- schema . metadata ( ) . description = Some ( "Hex encoded 20 bytes" . to_string ( ) ) ;
18- schema . string ( ) . pattern = Some ( "^0(x|X)[a-fA-F0-9]{40}$" . to_string ( ) ) ;
19- schema . into ( )
17+ fn json_schema ( _ : & mut SchemaGenerator ) -> Schema {
18+ json_schema ! ( {
19+ " description" : "Hex encoded 20 bytes" ,
20+ " pattern" : "^0(x|X)[a-fA-F0-9]{40}$" ,
21+ } )
2022 }
2123}
2224
2325impl JsonSchema for U256 {
24- fn schema_name ( ) -> String {
25- "U256String" . to_string ( )
26+ fn schema_name ( ) -> Cow < ' static , str > {
27+ "U256String" . into ( )
2628 }
2729
28- fn json_schema ( gen : & mut SchemaGenerator ) -> Schema {
29- let mut schema = gen . subschema_for :: < String > ( ) . into_object ( ) ;
30- schema . metadata ( ) . description = Some ( "256-bit Unsigned Integer" . to_string ( ) ) ;
31- schema . string ( ) . pattern = Some ( "^(0|[1-9][0-9]{0,77})$" . to_string ( ) ) ;
32- schema . into ( )
30+ fn json_schema ( _ : & mut SchemaGenerator ) -> Schema {
31+ json_schema ! ( {
32+ " description" : "256-bit Unsigned Integer" ,
33+ " pattern" : "^(0|[1-9][0-9]{0,77})$" ,
34+ } )
3335 }
3436}
3537
@@ -44,14 +46,17 @@ mod tests {
4446
4547 #[ test]
4648 fn hex_encoded_20_bytes ( ) {
47- let schema = H160 :: json_schema ( & mut schemars:: gen :: SchemaGenerator :: default ( ) ) ;
49+ let schema = H160 :: json_schema ( & mut schemars:: SchemaGenerator :: default ( ) ) ;
4850 let schema_json = serde_json:: to_value ( & schema) . unwrap ( ) ;
4951 let schema = jsonschema:: Validator :: options ( )
5052 . with_draft ( Draft :: Draft7 )
5153 . build ( & schema_json)
5254 . unwrap ( ) ;
5355 let value = serde_json:: to_value ( "0x55086adeca661185c437d92b9818e6eda6d0d047" ) . unwrap ( ) ;
54- assert ! ( schema. validate( & value) . is_ok( ) ) ;
56+ schema
57+ . validate ( & value)
58+ . map_err ( |e| e. into_iter ( ) . map ( |e| e. to_string ( ) ) . collect :: < Vec < _ > > ( ) )
59+ . unwrap ( ) ;
5560 let value = serde_json:: to_value ( "0X0E9C8DA9FD4BDD3281879D9E328D8D74D02558CC" ) . unwrap ( ) ;
5661 assert ! ( schema. validate( & value) . is_ok( ) ) ;
5762
@@ -61,7 +66,7 @@ mod tests {
6166
6267 #[ test]
6368 fn u256 ( ) {
64- let schema = U256 :: json_schema ( & mut schemars:: gen :: SchemaGenerator :: default ( ) ) ;
69+ let schema = U256 :: json_schema ( & mut schemars:: SchemaGenerator :: default ( ) ) ;
6570 let schema_json = serde_json:: to_value ( & schema) . unwrap ( ) ;
6671 let schema = jsonschema:: Validator :: options ( )
6772 . with_draft ( Draft :: Draft7 )
0 commit comments