mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-28 17:21:34 +00:00
*: Format with rustfmt (#2188)
Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
This commit is contained in:
@ -20,10 +20,13 @@
|
||||
|
||||
use crate::rpc_proto;
|
||||
use crate::topic::Topic;
|
||||
use libp2p_core::{InboundUpgrade, OutboundUpgrade, UpgradeInfo, PeerId, upgrade};
|
||||
use futures::{
|
||||
io::{AsyncRead, AsyncWrite},
|
||||
AsyncWriteExt, Future,
|
||||
};
|
||||
use libp2p_core::{upgrade, InboundUpgrade, OutboundUpgrade, PeerId, UpgradeInfo};
|
||||
use prost::Message;
|
||||
use std::{error, fmt, io, iter, pin::Pin};
|
||||
use futures::{Future, io::{AsyncRead, AsyncWrite}, AsyncWriteExt};
|
||||
|
||||
/// Implementation of `ConnectionUpgrade` for the floodsub protocol.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
@ -61,21 +64,18 @@ where
|
||||
let mut messages = Vec::with_capacity(rpc.publish.len());
|
||||
for publish in rpc.publish.into_iter() {
|
||||
messages.push(FloodsubMessage {
|
||||
source: PeerId::from_bytes(&publish.from.unwrap_or_default()).map_err(|_| {
|
||||
FloodsubDecodeError::InvalidPeerId
|
||||
})?,
|
||||
source: PeerId::from_bytes(&publish.from.unwrap_or_default())
|
||||
.map_err(|_| FloodsubDecodeError::InvalidPeerId)?,
|
||||
data: publish.data.unwrap_or_default(),
|
||||
sequence_number: publish.seqno.unwrap_or_default(),
|
||||
topics: publish.topic_ids
|
||||
.into_iter()
|
||||
.map(Topic::new)
|
||||
.collect(),
|
||||
topics: publish.topic_ids.into_iter().map(Topic::new).collect(),
|
||||
});
|
||||
}
|
||||
|
||||
Ok(FloodsubRpc {
|
||||
messages,
|
||||
subscriptions: rpc.subscriptions
|
||||
subscriptions: rpc
|
||||
.subscriptions
|
||||
.into_iter()
|
||||
.map(|sub| FloodsubSubscription {
|
||||
action: if Some(true) == sub.subscribe {
|
||||
@ -117,12 +117,15 @@ impl From<prost::DecodeError> for FloodsubDecodeError {
|
||||
impl fmt::Display for FloodsubDecodeError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match *self {
|
||||
FloodsubDecodeError::ReadError(ref err) =>
|
||||
write!(f, "Error while reading from socket: {}", err),
|
||||
FloodsubDecodeError::ProtobufError(ref err) =>
|
||||
write!(f, "Error while decoding protobuf: {}", err),
|
||||
FloodsubDecodeError::InvalidPeerId =>
|
||||
write!(f, "Error while decoding PeerId from message"),
|
||||
FloodsubDecodeError::ReadError(ref err) => {
|
||||
write!(f, "Error while reading from socket: {}", err)
|
||||
}
|
||||
FloodsubDecodeError::ProtobufError(ref err) => {
|
||||
write!(f, "Error while decoding protobuf: {}", err)
|
||||
}
|
||||
FloodsubDecodeError::InvalidPeerId => {
|
||||
write!(f, "Error while decoding PeerId from message")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -179,32 +182,30 @@ impl FloodsubRpc {
|
||||
/// Turns this `FloodsubRpc` into a message that can be sent to a substream.
|
||||
fn into_bytes(self) -> Vec<u8> {
|
||||
let rpc = rpc_proto::Rpc {
|
||||
publish: self.messages.into_iter()
|
||||
.map(|msg| {
|
||||
rpc_proto::Message {
|
||||
from: Some(msg.source.to_bytes()),
|
||||
data: Some(msg.data),
|
||||
seqno: Some(msg.sequence_number),
|
||||
topic_ids: msg.topics
|
||||
.into_iter()
|
||||
.map(|topic| topic.into())
|
||||
.collect()
|
||||
}
|
||||
publish: self
|
||||
.messages
|
||||
.into_iter()
|
||||
.map(|msg| rpc_proto::Message {
|
||||
from: Some(msg.source.to_bytes()),
|
||||
data: Some(msg.data),
|
||||
seqno: Some(msg.sequence_number),
|
||||
topic_ids: msg.topics.into_iter().map(|topic| topic.into()).collect(),
|
||||
})
|
||||
.collect(),
|
||||
|
||||
subscriptions: self.subscriptions.into_iter()
|
||||
.map(|topic| {
|
||||
rpc_proto::rpc::SubOpts {
|
||||
subscribe: Some(topic.action == FloodsubSubscriptionAction::Subscribe),
|
||||
topic_id: Some(topic.topic.into())
|
||||
}
|
||||
subscriptions: self
|
||||
.subscriptions
|
||||
.into_iter()
|
||||
.map(|topic| rpc_proto::rpc::SubOpts {
|
||||
subscribe: Some(topic.action == FloodsubSubscriptionAction::Subscribe),
|
||||
topic_id: Some(topic.topic.into()),
|
||||
})
|
||||
.collect()
|
||||
.collect(),
|
||||
};
|
||||
|
||||
let mut buf = Vec::with_capacity(rpc.encoded_len());
|
||||
rpc.encode(&mut buf).expect("Vec<u8> provides capacity as needed");
|
||||
rpc.encode(&mut buf)
|
||||
.expect("Vec<u8> provides capacity as needed");
|
||||
buf
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user