diff --git a/core/src/lib.rs b/core/src/lib.rs index c40e64c5d8b..a11480047bd 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -47,8 +47,6 @@ mod proto { /// Multi-address re-export. pub use multiaddr; -use std::fmt; -use std::fmt::Formatter; pub type Negotiated = multistream_select::Negotiated; #[deprecated(since = "0.39.0", note = "Depend on `libp2p-identity` instead.")] @@ -130,16 +128,5 @@ pub use transport::Transport; pub use upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeError, UpgradeInfo}; #[derive(Debug, thiserror::Error)] -pub struct DecodeError(String); - -impl From for DecodeError { - fn from(e: quick_protobuf::Error) -> Self { - Self(e.to_string()) - } -} - -impl fmt::Display for DecodeError { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.0) - } -} +#[error(transparent)] +pub struct DecodeError(quick_protobuf::Error); diff --git a/core/src/peer_record.rs b/core/src/peer_record.rs index 35433dc245f..a73168a30f1 100644 --- a/core/src/peer_record.rs +++ b/core/src/peer_record.rs @@ -36,8 +36,7 @@ impl PeerRecord { let (payload, signing_key) = envelope.payload_and_signing_key(String::from(DOMAIN_SEP), PAYLOAD_TYPE.as_bytes())?; let mut reader = BytesReader::from_bytes(payload); - let record = - proto::PeerRecord::from_reader(&mut reader, payload).map_err(DecodeError::from)?; + let record = proto::PeerRecord::from_reader(&mut reader, payload).map_err(DecodeError)?; let peer_id = PeerId::from_bytes(&record.peer_id)?; diff --git a/core/src/signed_envelope.rs b/core/src/signed_envelope.rs index a50146f87d1..19a0cac4f82 100644 --- a/core/src/signed_envelope.rs +++ b/core/src/signed_envelope.rs @@ -97,8 +97,7 @@ impl SignedEnvelope { use quick_protobuf::MessageRead; let mut reader = BytesReader::from_bytes(bytes); - let envelope = - proto::Envelope::from_reader(&mut reader, bytes).map_err(DecodeError::from)?; + let envelope = proto::Envelope::from_reader(&mut reader, bytes).map_err(DecodeError)?; Ok(Self { key: PublicKey::try_decode_protobuf(&envelope.public_key)?, diff --git a/transports/noise/src/io/handshake.rs b/transports/noise/src/io/handshake.rs index 18816a3b28d..3027fbfbd19 100644 --- a/transports/noise/src/io/handshake.rs +++ b/transports/noise/src/io/handshake.rs @@ -28,7 +28,7 @@ mod proto { use crate::io::{framed::NoiseFramed, Output}; use crate::protocol::{KeypairIdentity, STATIC_KEY_DOMAIN}; -use crate::Error; +use crate::{DecodeError, Error}; use bytes::Bytes; use futures::prelude::*; use libp2p_identity as identity; @@ -140,7 +140,8 @@ where { let msg = recv(state).await?; let mut reader = BytesReader::from_bytes(&msg[..]); - let pb = proto::NoiseHandshakePayload::from_reader(&mut reader, &msg[..])?; + let pb = + proto::NoiseHandshakePayload::from_reader(&mut reader, &msg[..]).map_err(DecodeError)?; state.id_remote_pubkey = Some(identity::PublicKey::try_decode_protobuf(&pb.identity_key)?); diff --git a/transports/noise/src/lib.rs b/transports/noise/src/lib.rs index e3b03db24cd..b7360c0c799 100644 --- a/transports/noise/src/lib.rs +++ b/transports/noise/src/lib.rs @@ -69,8 +69,6 @@ use libp2p_core::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}; use libp2p_identity as identity; use libp2p_identity::PeerId; use snow::params::NoiseParams; -use std::fmt; -use std::fmt::Formatter; use std::pin::Pin; /// The configuration for the noise handshake. @@ -211,29 +209,12 @@ pub enum Error { BadSignature, #[error("Authentication failed")] AuthenticationFailed, - #[error(transparent)] - InvalidPayload(DecodeError), + #[error("failed to decode protobuf ")] + InvalidPayload(#[from] DecodeError), #[error(transparent)] SigningError(#[from] libp2p_identity::SigningError), } #[derive(Debug, thiserror::Error)] -pub struct DecodeError(String); - -impl fmt::Display for DecodeError { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.0) - } -} - -impl From for DecodeError { - fn from(e: quick_protobuf::Error) -> Self { - Self(e.to_string()) - } -} - -impl From for Error { - fn from(e: quick_protobuf::Error) -> Self { - Error::InvalidPayload(e.into()) - } -} +#[error(transparent)] +pub struct DecodeError(quick_protobuf::Error); diff --git a/transports/plaintext/src/error.rs b/transports/plaintext/src/error.rs index 133cca746af..2daac496f12 100644 --- a/transports/plaintext/src/error.rs +++ b/transports/plaintext/src/error.rs @@ -41,7 +41,7 @@ pub enum PlainTextError { } #[derive(Debug)] -pub struct DecodeError(quick_protobuf::Error); +pub struct DecodeError(pub(crate) quick_protobuf::Error); impl fmt::Display for DecodeError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -87,9 +87,9 @@ impl From for PlainTextError { } } -impl From for PlainTextError { - fn from(err: quick_protobuf::Error) -> PlainTextError { - PlainTextError::InvalidPayload(DecodeError(err)) +impl From for PlainTextError { + fn from(err: DecodeError) -> PlainTextError { + PlainTextError::InvalidPayload(err) } } diff --git a/transports/plaintext/src/handshake.rs b/transports/plaintext/src/handshake.rs index b1e322459af..46dd6119d92 100644 --- a/transports/plaintext/src/handshake.rs +++ b/transports/plaintext/src/handshake.rs @@ -18,7 +18,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::error::PlainTextError; +use crate::error::{DecodeError, PlainTextError}; use crate::proto::Exchange; use crate::PlainText2Config; @@ -74,7 +74,7 @@ impl HandshakeContext { exchange_bytes: BytesMut, ) -> Result, PlainTextError> { let mut reader = BytesReader::from_bytes(&exchange_bytes); - let prop = Exchange::from_reader(&mut reader, &exchange_bytes)?; + let prop = Exchange::from_reader(&mut reader, &exchange_bytes).map_err(DecodeError)?; let public_key = PublicKey::try_decode_protobuf(&prop.pubkey.unwrap_or_default())?; let peer_id = PeerId::from_bytes(&prop.id.unwrap_or_default())?;