Skip to content

Commit b9ebb0b

Browse files
committed
Format API rpc/message.rs
Signed-off-by: Lee Smet <[email protected]>
1 parent 88c0b64 commit b9ebb0b

File tree

1 file changed

+45
-33
lines changed

1 file changed

+45
-33
lines changed

mycelium-api/src/rpc/message.rs

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use jsonrpc_core::{Error, ErrorCode, Result as RpcResult};
44
use std::time::Duration;
55
use tracing::debug;
66

7-
use mycelium::metrics::Metrics;
87
use mycelium::message::{MessageId, MessageInfo};
8+
use mycelium::metrics::Metrics;
99

10-
use crate::HttpServerState;
1110
use crate::message::{MessageReceiveInfo, MessageSendInfo, PushMessageResponse};
1211
use crate::rpc::models::error_codes;
1312
use crate::rpc::traits::MessageApi;
13+
use crate::HttpServerState;
1414

1515
/// Implementation of Message-related JSON-RPC methods
1616
pub struct MessageRpc<M>
@@ -28,10 +28,11 @@ where
2828
pub fn new(state: HttpServerState<M>) -> Self {
2929
Self { state }
3030
}
31-
31+
3232
/// Convert a base64 string to bytes
3333
fn decode_base64(&self, s: &str) -> Result<Vec<u8>, Error> {
34-
base64::engine::general_purpose::STANDARD.decode(s.as_bytes())
34+
base64::engine::general_purpose::STANDARD
35+
.decode(s.as_bytes())
3536
.map_err(|e| Error {
3637
code: ErrorCode::InvalidParams,
3738
message: format!("Invalid base64 encoding: {}", e),
@@ -44,19 +45,24 @@ impl<M> MessageApi for MessageRpc<M>
4445
where
4546
M: Metrics + Clone + Send + Sync + 'static,
4647
{
47-
fn pop_message(&self, peek: Option<bool>, timeout: Option<u64>, topic: Option<String>) -> RpcResult<MessageReceiveInfo> {
48+
fn pop_message(
49+
&self,
50+
peek: Option<bool>,
51+
timeout: Option<u64>,
52+
topic: Option<String>,
53+
) -> RpcResult<MessageReceiveInfo> {
4854
debug!(
4955
"Attempt to get message via RPC, peek {}, timeout {} seconds",
5056
peek.unwrap_or(false),
5157
timeout.unwrap_or(0)
5258
);
53-
59+
5460
let topic_bytes = if let Some(topic_str) = topic {
5561
Some(self.decode_base64(&topic_str)?)
5662
} else {
5763
None
5864
};
59-
65+
6066
// A timeout of 0 seconds essentially means get a message if there is one, and return
6167
// immediately if there isn't.
6268
let result = tokio::task::block_in_place(|| {
@@ -72,7 +78,7 @@ where
7278
.await
7379
})
7480
});
75-
81+
7682
match result {
7783
Ok(Ok(m)) => Ok(MessageReceiveInfo {
7884
id: m.id,
@@ -94,22 +100,26 @@ where
94100
}),
95101
}
96102
}
97-
98-
fn push_message(&self, message: MessageSendInfo, reply_timeout: Option<u64>) -> RpcResult<PushMessageResponse> {
103+
104+
fn push_message(
105+
&self,
106+
message: MessageSendInfo,
107+
reply_timeout: Option<u64>,
108+
) -> RpcResult<PushMessageResponse> {
99109
let dst = match message.dst {
100110
crate::message::MessageDestination::Ip(ip) => ip,
101111
crate::message::MessageDestination::Pk(pk) => pk.address().into(),
102112
};
103-
113+
104114
debug!(
105115
message.dst=%dst,
106116
message.len=message.payload.len(),
107117
"Pushing new message via RPC",
108118
);
109-
119+
110120
// Default message try duration
111121
const DEFAULT_MESSAGE_TRY_DURATION: Duration = Duration::from_secs(60 * 5);
112-
122+
113123
let result = tokio::task::block_in_place(|| {
114124
tokio::runtime::Handle::current().block_on(async {
115125
self.state.node.lock().await.push_message(
@@ -121,7 +131,7 @@ where
121131
)
122132
})
123133
});
124-
134+
125135
let (id, sub) = match result {
126136
Ok((id, sub)) => (id, sub),
127137
Err(_) => {
@@ -132,14 +142,16 @@ where
132142
});
133143
}
134144
};
135-
145+
136146
if reply_timeout.is_none() {
137147
// If we don't wait for the reply just return here.
138-
return Ok(PushMessageResponse::Id(crate::message::MessageIdReply { id }));
148+
return Ok(PushMessageResponse::Id(crate::message::MessageIdReply {
149+
id,
150+
}));
139151
}
140-
152+
141153
let mut sub = sub.unwrap();
142-
154+
143155
// Wait for reply with timeout
144156
let reply_result = tokio::task::block_in_place(|| {
145157
tokio::runtime::Handle::current().block_on(async {
@@ -183,13 +195,13 @@ where
183195
}
184196
})
185197
});
186-
198+
187199
match reply_result {
188200
Ok(response) => Ok(response),
189201
Err(e) => Err(e),
190202
}
191203
}
192-
204+
193205
fn push_message_reply(&self, id: String, message: MessageSendInfo) -> RpcResult<bool> {
194206
let message_id = match MessageId::from_hex(&id) {
195207
Ok(id) => id,
@@ -201,22 +213,22 @@ where
201213
});
202214
}
203215
};
204-
216+
205217
let dst = match message.dst {
206218
crate::message::MessageDestination::Ip(ip) => ip,
207219
crate::message::MessageDestination::Pk(pk) => pk.address().into(),
208220
};
209-
221+
210222
debug!(
211223
message.id=id,
212224
message.dst=%dst,
213225
message.len=message.payload.len(),
214226
"Pushing new reply to message via RPC",
215227
);
216-
228+
217229
// Default message try duration
218230
const DEFAULT_MESSAGE_TRY_DURATION: Duration = Duration::from_secs(60 * 5);
219-
231+
220232
tokio::task::block_in_place(|| {
221233
tokio::runtime::Handle::current().block_on(async {
222234
self.state.node.lock().await.reply_message(
@@ -227,10 +239,10 @@ where
227239
);
228240
})
229241
});
230-
242+
231243
Ok(true)
232244
}
233-
245+
234246
fn get_message_info(&self, id: String) -> RpcResult<MessageInfo> {
235247
let message_id = match MessageId::from_hex(&id) {
236248
Ok(id) => id,
@@ -242,15 +254,14 @@ where
242254
});
243255
}
244256
};
245-
257+
246258
debug!(message.id=%id, "Fetching message status via RPC");
247-
259+
248260
let result = tokio::task::block_in_place(|| {
249-
tokio::runtime::Handle::current().block_on(async {
250-
self.state.node.lock().await.message_status(message_id)
251-
})
261+
tokio::runtime::Handle::current()
262+
.block_on(async { self.state.node.lock().await.message_status(message_id) })
252263
});
253-
264+
254265
match result {
255266
Some(info) => Ok(info),
256267
None => Err(Error {
@@ -260,4 +271,5 @@ where
260271
}),
261272
}
262273
}
263-
}
274+
}
275+

0 commit comments

Comments
 (0)