Skip to content

Commit 1bf6264

Browse files
feat: properly encapsulate quick_protobuf::Error
I noticed that we also still leak that dependency in several crates by providing a `From` impl so I've removed that one as well. Resolves #3534. Pull-Request: #3894.
1 parent 6985d72 commit 1bf6264

File tree

7 files changed

+17
-50
lines changed

7 files changed

+17
-50
lines changed

core/src/lib.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ mod proto {
4747

4848
/// Multi-address re-export.
4949
pub use multiaddr;
50-
use std::fmt;
51-
use std::fmt::Formatter;
5250
pub type Negotiated<T> = multistream_select::Negotiated<T>;
5351

5452
#[deprecated(since = "0.39.0", note = "Depend on `libp2p-identity` instead.")]
@@ -130,16 +128,5 @@ pub use transport::Transport;
130128
pub use upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeError, UpgradeInfo};
131129

132130
#[derive(Debug, thiserror::Error)]
133-
pub struct DecodeError(String);
134-
135-
impl From<quick_protobuf::Error> for DecodeError {
136-
fn from(e: quick_protobuf::Error) -> Self {
137-
Self(e.to_string())
138-
}
139-
}
140-
141-
impl fmt::Display for DecodeError {
142-
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
143-
write!(f, "{}", self.0)
144-
}
145-
}
131+
#[error(transparent)]
132+
pub struct DecodeError(quick_protobuf::Error);

core/src/peer_record.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ impl PeerRecord {
3636
let (payload, signing_key) =
3737
envelope.payload_and_signing_key(String::from(DOMAIN_SEP), PAYLOAD_TYPE.as_bytes())?;
3838
let mut reader = BytesReader::from_bytes(payload);
39-
let record =
40-
proto::PeerRecord::from_reader(&mut reader, payload).map_err(DecodeError::from)?;
39+
let record = proto::PeerRecord::from_reader(&mut reader, payload).map_err(DecodeError)?;
4140

4241
let peer_id = PeerId::from_bytes(&record.peer_id)?;
4342

core/src/signed_envelope.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ impl SignedEnvelope {
9797
use quick_protobuf::MessageRead;
9898

9999
let mut reader = BytesReader::from_bytes(bytes);
100-
let envelope =
101-
proto::Envelope::from_reader(&mut reader, bytes).map_err(DecodeError::from)?;
100+
let envelope = proto::Envelope::from_reader(&mut reader, bytes).map_err(DecodeError)?;
102101

103102
Ok(Self {
104103
key: PublicKey::try_decode_protobuf(&envelope.public_key)?,

transports/noise/src/io/handshake.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ mod proto {
2828

2929
use crate::io::{framed::NoiseFramed, Output};
3030
use crate::protocol::{KeypairIdentity, STATIC_KEY_DOMAIN};
31-
use crate::Error;
31+
use crate::{DecodeError, Error};
3232
use bytes::Bytes;
3333
use futures::prelude::*;
3434
use libp2p_identity as identity;
@@ -140,7 +140,8 @@ where
140140
{
141141
let msg = recv(state).await?;
142142
let mut reader = BytesReader::from_bytes(&msg[..]);
143-
let pb = proto::NoiseHandshakePayload::from_reader(&mut reader, &msg[..])?;
143+
let pb =
144+
proto::NoiseHandshakePayload::from_reader(&mut reader, &msg[..]).map_err(DecodeError)?;
144145

145146
state.id_remote_pubkey = Some(identity::PublicKey::try_decode_protobuf(&pb.identity_key)?);
146147

transports/noise/src/lib.rs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ use libp2p_core::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
6969
use libp2p_identity as identity;
7070
use libp2p_identity::PeerId;
7171
use snow::params::NoiseParams;
72-
use std::fmt;
73-
use std::fmt::Formatter;
7472
use std::pin::Pin;
7573

7674
/// The configuration for the noise handshake.
@@ -211,29 +209,12 @@ pub enum Error {
211209
BadSignature,
212210
#[error("Authentication failed")]
213211
AuthenticationFailed,
214-
#[error(transparent)]
215-
InvalidPayload(DecodeError),
212+
#[error("failed to decode protobuf ")]
213+
InvalidPayload(#[from] DecodeError),
216214
#[error(transparent)]
217215
SigningError(#[from] libp2p_identity::SigningError),
218216
}
219217

220218
#[derive(Debug, thiserror::Error)]
221-
pub struct DecodeError(String);
222-
223-
impl fmt::Display for DecodeError {
224-
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
225-
write!(f, "{}", self.0)
226-
}
227-
}
228-
229-
impl From<quick_protobuf::Error> for DecodeError {
230-
fn from(e: quick_protobuf::Error) -> Self {
231-
Self(e.to_string())
232-
}
233-
}
234-
235-
impl From<quick_protobuf::Error> for Error {
236-
fn from(e: quick_protobuf::Error) -> Self {
237-
Error::InvalidPayload(e.into())
238-
}
239-
}
219+
#[error(transparent)]
220+
pub struct DecodeError(quick_protobuf::Error);

transports/plaintext/src/error.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub enum PlainTextError {
4141
}
4242

4343
#[derive(Debug)]
44-
pub struct DecodeError(quick_protobuf::Error);
44+
pub struct DecodeError(pub(crate) quick_protobuf::Error);
4545

4646
impl fmt::Display for DecodeError {
4747
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -87,9 +87,9 @@ impl From<IoError> for PlainTextError {
8787
}
8888
}
8989

90-
impl From<quick_protobuf::Error> for PlainTextError {
91-
fn from(err: quick_protobuf::Error) -> PlainTextError {
92-
PlainTextError::InvalidPayload(DecodeError(err))
90+
impl From<DecodeError> for PlainTextError {
91+
fn from(err: DecodeError) -> PlainTextError {
92+
PlainTextError::InvalidPayload(err)
9393
}
9494
}
9595

transports/plaintext/src/handshake.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1919
// DEALINGS IN THE SOFTWARE.
2020

21-
use crate::error::PlainTextError;
21+
use crate::error::{DecodeError, PlainTextError};
2222
use crate::proto::Exchange;
2323
use crate::PlainText2Config;
2424

@@ -74,7 +74,7 @@ impl HandshakeContext<Local> {
7474
exchange_bytes: BytesMut,
7575
) -> Result<HandshakeContext<Remote>, PlainTextError> {
7676
let mut reader = BytesReader::from_bytes(&exchange_bytes);
77-
let prop = Exchange::from_reader(&mut reader, &exchange_bytes)?;
77+
let prop = Exchange::from_reader(&mut reader, &exchange_bytes).map_err(DecodeError)?;
7878

7979
let public_key = PublicKey::try_decode_protobuf(&prop.pubkey.unwrap_or_default())?;
8080
let peer_id = PeerId::from_bytes(&prop.id.unwrap_or_default())?;

0 commit comments

Comments
 (0)