1
1
use futures:: future:: { BoxFuture , FutureExt } ;
2
- use primitives:: adapter:: { Adapter , AdapterError , AdapterResult , DummyAdapterOptions , Session } ;
2
+ use primitives:: adapter:: {
3
+ Adapter , AdapterErrorKind , AdapterResult , DummyAdapterOptions , Error as AdapterError , Session ,
4
+ } ;
3
5
use primitives:: channel_validator:: ChannelValidator ;
4
6
use primitives:: config:: Config ;
5
7
use primitives:: { Channel , ToETHChecksum , ValidatorId } ;
6
8
use std:: collections:: HashMap ;
9
+ use std:: fmt;
7
10
8
11
#[ derive( Debug , Clone ) ]
9
12
pub struct DummyAdapter {
@@ -30,16 +33,28 @@ impl DummyAdapter {
30
33
}
31
34
}
32
35
36
+ #[ derive( Debug ) ]
37
+ pub struct Error { }
38
+ impl fmt:: Display for Error {
39
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
40
+ write ! ( f, "Dummy Adapter error occurred!" )
41
+ }
42
+ }
43
+
44
+ impl AdapterErrorKind for Error { }
45
+
33
46
impl Adapter for DummyAdapter {
34
- fn unlock ( & mut self ) -> AdapterResult < ( ) > {
47
+ type AdapterError = Error ;
48
+
49
+ fn unlock ( & mut self ) -> AdapterResult < ( ) , Self :: AdapterError > {
35
50
Ok ( ( ) )
36
51
}
37
52
38
53
fn whoami ( & self ) -> & ValidatorId {
39
54
& self . identity
40
55
}
41
56
42
- fn sign ( & self , state_root : & str ) -> AdapterResult < String > {
57
+ fn sign ( & self , state_root : & str ) -> AdapterResult < String , Self :: AdapterError > {
43
58
let signature = format ! (
44
59
"Dummy adapter signature for {} by {}" ,
45
60
state_root,
@@ -53,7 +68,7 @@ impl Adapter for DummyAdapter {
53
68
signer : & ValidatorId ,
54
69
_state_root : & str ,
55
70
signature : & str ,
56
- ) -> AdapterResult < bool > {
71
+ ) -> AdapterResult < bool , Self :: AdapterError > {
57
72
// select the `identity` and compare it to the signer
58
73
// for empty string this will return array with 1 element - an empty string `[""]`
59
74
let is_same = match signature. rsplit ( ' ' ) . take ( 1 ) . next ( ) {
@@ -64,17 +79,22 @@ impl Adapter for DummyAdapter {
64
79
Ok ( is_same)
65
80
}
66
81
67
- fn validate_channel < ' a > ( & ' a self , channel : & ' a Channel ) -> BoxFuture < ' a , AdapterResult < bool > > {
82
+ fn validate_channel < ' a > (
83
+ & ' a self ,
84
+ channel : & ' a Channel ,
85
+ ) -> BoxFuture < ' a , AdapterResult < bool , Self :: AdapterError > > {
68
86
async move {
69
- match DummyAdapter :: is_channel_valid ( & self . config , self . whoami ( ) , channel) {
70
- Ok ( _) => Ok ( true ) ,
71
- Err ( e) => Err ( AdapterError :: InvalidChannel ( e. to_string ( ) ) ) ,
72
- }
87
+ DummyAdapter :: is_channel_valid ( & self . config , self . whoami ( ) , channel)
88
+ . map ( |_| true )
89
+ . map_err ( AdapterError :: InvalidChannel )
73
90
}
74
91
. boxed ( )
75
92
}
76
93
77
- fn session_from_token < ' a > ( & ' a self , token : & ' a str ) -> BoxFuture < ' a , AdapterResult < Session > > {
94
+ fn session_from_token < ' a > (
95
+ & ' a self ,
96
+ token : & ' a str ,
97
+ ) -> BoxFuture < ' a , AdapterResult < Session , Self :: AdapterError > > {
78
98
async move {
79
99
let identity = self
80
100
. authorization_tokens
@@ -95,7 +115,7 @@ impl Adapter for DummyAdapter {
95
115
. boxed ( )
96
116
}
97
117
98
- fn get_auth ( & self , _validator : & ValidatorId ) -> AdapterResult < String > {
118
+ fn get_auth ( & self , _validator : & ValidatorId ) -> AdapterResult < String , Self :: AdapterError > {
99
119
let who = self
100
120
. session_tokens
101
121
. iter ( )
0 commit comments