1use {ErrorCode, IndyHandle};
2
3use std::ffi::CString;
4use std::time::Duration;
5use std::ptr::null;
6
7use utils::callbacks::ClosureHandler;
8use utils::results::ResultHandler;
9
10use native::pairwise;
11use native::{ResponseEmptyCB,
12 ResponseStringCB,
13 ResponseBoolCB};
14
15pub struct Pairwise {}
16
17impl Pairwise {
18 pub fn does_exist(wallet_handle: IndyHandle, their_did: &str) -> Result<bool, ErrorCode> {
19 let (receiver, command_handle, cb) = ClosureHandler::cb_ec_bool();
20
21 let err = Pairwise::_does_exist(command_handle, wallet_handle, their_did, cb);
22
23 ResultHandler::one(err, receiver)
24 }
25
26 pub fn does_exist_timeout(wallet_handle: IndyHandle, their_did: &str, timeout: Duration) -> Result<bool, ErrorCode> {
28 let (receiver, command_handle, cb) = ClosureHandler::cb_ec_bool();
29
30 let err = Pairwise::_does_exist(command_handle, wallet_handle, their_did, cb);
31
32 ResultHandler::one_timeout(err, receiver, timeout)
33 }
34
35 pub fn does_exist_async<F: 'static>(wallet_handle: IndyHandle, their_did: &str, closure: F) -> ErrorCode where F: FnMut(ErrorCode, bool) + Send {
40 let (command_handle, cb) = ClosureHandler::convert_cb_ec_bool(Box::new(closure));
41
42 Pairwise::_does_exist(command_handle, wallet_handle, their_did, cb)
43 }
44
45 fn _does_exist(command_handle: IndyHandle, wallet_handle: IndyHandle, their_did: &str, cb: Option<ResponseBoolCB>) -> ErrorCode {
46 let their_did = c_str!(their_did);
47
48 ErrorCode::from(unsafe {
49 pairwise::indy_is_pairwise_exists(command_handle, wallet_handle, their_did.as_ptr(), cb)
50 })
51 }
52
53 pub fn create(wallet_handle: IndyHandle, their_did: &str, my_did: &str, metadata: Option<&str>) -> Result<(), ErrorCode> {
54 let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
55
56 let err = Pairwise::_create(command_handle, wallet_handle, their_did, my_did, metadata, cb);
57
58 ResultHandler::empty(err, receiver)
59 }
60
61 pub fn create_timeout(wallet_handle: IndyHandle, their_did: &str, my_did: &str, metadata: Option<&str>, timeout: Duration) -> Result<(), ErrorCode> {
63 let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
64
65 let err = Pairwise::_create(command_handle, wallet_handle, their_did, my_did, metadata, cb);
66
67 ResultHandler::empty_timeout(err, receiver, timeout)
68 }
69
70 pub fn create_async<F: 'static>(wallet_handle: IndyHandle, their_did: &str, my_did: &str, metadata: Option<&str>, closure: F) -> ErrorCode where F: FnMut(ErrorCode) + Send {
75 let (command_handle, cb) = ClosureHandler::convert_cb_ec(Box::new(closure));
76
77 Pairwise::_create(command_handle, wallet_handle, their_did, my_did, metadata, cb)
78 }
79
80 fn _create(command_handle: IndyHandle, wallet_handle: IndyHandle, their_did: &str, my_did: &str, metadata: Option<&str>, cb: Option<ResponseEmptyCB>) -> ErrorCode {
81 let their_did = c_str!(their_did);
82 let my_did = c_str!(my_did);
83 let metadata_str = opt_c_str!(metadata);
84
85 ErrorCode::from(unsafe {
86 pairwise::indy_create_pairwise(command_handle, wallet_handle, their_did.as_ptr(), my_did.as_ptr(), opt_c_ptr!(metadata, metadata_str), cb)
87 })
88 }
89
90 pub fn list(wallet_handle: IndyHandle) -> Result<String, ErrorCode> {
91 let (receiver, command_handle, cb) = ClosureHandler::cb_ec_string();
92
93 let err = Pairwise::_list(command_handle, wallet_handle, cb);
94
95 ResultHandler::one(err, receiver)
96 }
97
98 pub fn list_timeout(wallet_handle: IndyHandle, timeout: Duration) -> Result<String, ErrorCode> {
100 let (receiver, command_handle, cb) = ClosureHandler::cb_ec_string();
101
102 let err = Pairwise::_list(command_handle, wallet_handle, cb);
103
104 ResultHandler::one_timeout(err, receiver, timeout)
105 }
106
107 pub fn list_async<F: 'static>(wallet_handle: IndyHandle, closure: F) -> ErrorCode where F: FnMut(ErrorCode, String) + Send {
112 let (command_handle, cb) = ClosureHandler::convert_cb_ec_string(Box::new(closure));
113
114 Pairwise::_list(command_handle, wallet_handle, cb)
115 }
116
117 fn _list(command_handle: IndyHandle, wallet_handle: IndyHandle, cb: Option<ResponseStringCB>) -> ErrorCode {
118 ErrorCode::from(unsafe {
119 pairwise::indy_list_pairwise(command_handle, wallet_handle, cb)
120 })
121 }
122
123 pub fn get(wallet_handle: IndyHandle, their_did: &str) -> Result<String, ErrorCode> {
124 let (receiver, command_handle, cb) = ClosureHandler::cb_ec_string();
125
126 let err = Pairwise::_get(command_handle, wallet_handle, their_did, cb);
127
128 ResultHandler::one(err, receiver)
129 }
130
131 pub fn get_timeout(wallet_handle: IndyHandle, their_did: &str, timeout: Duration) -> Result<String, ErrorCode> {
133 let (receiver, command_handle, cb) = ClosureHandler::cb_ec_string();
134
135 let err = Pairwise::_get(command_handle, wallet_handle, their_did, cb);
136
137 ResultHandler::one_timeout(err, receiver, timeout)
138 }
139
140 pub fn get_async<F: 'static>(wallet_handle: IndyHandle, their_did: &str, closure: F) -> ErrorCode where F: FnMut(ErrorCode, String) + Send {
145 let (command_handle, cb) = ClosureHandler::convert_cb_ec_string(Box::new(closure));
146
147 Pairwise::_get(command_handle, wallet_handle, their_did, cb)
148 }
149
150 fn _get(command_handle: IndyHandle, wallet_handle: IndyHandle, their_did: &str, cb: Option<ResponseStringCB>) -> ErrorCode {
151 let their_did = c_str!(their_did);
152
153 ErrorCode::from(unsafe {
154 pairwise::indy_get_pairwise(command_handle, wallet_handle, their_did.as_ptr(), cb)
155 })
156 }
157
158 pub fn set_metadata(wallet_handle: IndyHandle, their_did: &str, metadata: Option<&str>) -> Result<(), ErrorCode> {
159 let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
160
161 let err = Pairwise::_set_metadata(command_handle, wallet_handle, their_did, metadata, cb);
162
163 ResultHandler::empty(err, receiver)
164 }
165
166 pub fn set_metadata_timeout(wallet_handle: IndyHandle, their_did: &str, metadata: Option<&str>, timeout: Duration) -> Result<(), ErrorCode> {
168 let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
169
170 let err = Pairwise::_set_metadata(command_handle, wallet_handle, their_did, metadata, cb);
171
172 ResultHandler::empty_timeout(err, receiver, timeout)
173 }
174
175 pub fn set_metadata_async<F: 'static>(wallet_handle: IndyHandle, their_did: &str, metadata: Option<&str>, closure: F) -> ErrorCode where F: FnMut(ErrorCode) + Send {
180 let (command_handle, cb) = ClosureHandler::convert_cb_ec(Box::new(closure));
181
182 Pairwise::_set_metadata(command_handle, wallet_handle, their_did, metadata, cb)
183 }
184
185 fn _set_metadata(command_handle: IndyHandle, wallet_handle: IndyHandle, their_did: &str, metadata: Option<&str>, cb: Option<ResponseEmptyCB>) -> ErrorCode {
186 let their_did = c_str!(their_did);
187 let metadata_str = opt_c_str!(metadata);
188
189 ErrorCode::from(unsafe {
190 pairwise::indy_set_pairwise_metadata(command_handle, wallet_handle, their_did.as_ptr(), opt_c_ptr!(metadata, metadata_str), cb)
191 })
192 }
193}