Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core/: Add display implementation for DialError<THandler> #2456

Closed
wants to merge 7 commits into from
46 changes: 45 additions & 1 deletion core/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ impl NetworkConfig {
}

/// Possible (synchronous) errors when dialing a peer.
#[derive(Debug, Clone, Error)]
#[derive(Clone, Error)]
pub enum DialError<THandler> {
/// The dialing attempt is rejected because of a connection limit.
ConnectionLimit {
Expand All @@ -578,12 +578,56 @@ pub enum DialError<THandler> {
},
/// The dialing attempt is rejected because the peer being dialed is the local peer.
LocalPeerId { handler: THandler },
/// The dialing attempt is rejected because the PeerId is invalid.
InvalidPeerId {
handler: THandler,
multihash: Multihash,
},
}

impl<THandler> fmt::Debug for DialError<THandler> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
DialError::ConnectionLimit { limit, handler: _ } => f
.debug_struct("DialError::ConnectionLimit")
.field("limit", limit)
.finish(),
DialError::LocalPeerId { handler: _ } => {
f.debug_struct("DialError::LocalPeerId").finish()
}
DialError::InvalidPeerId {
handler: _,
multihash,
} => f
.debug_struct("DialError::InvalidPeerId")
.field("multihash", multihash)
.finish(),
}
}
}

impl<THandler> fmt::Display for DialError<THandler> {
Frederik-Baetens marked this conversation as resolved.
Show resolved Hide resolved
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
DialError::ConnectionLimit { limit, handler: _ } => {
write!(
f,
"The dialing attempt was rejected because of a connection limit: {limit}"
)
}
DialError::LocalPeerId { .. } => {
write!(f, "The dialing attempt was rejected because the peer being dialed is the local peer.")
}
DialError::InvalidPeerId {
multihash,
handler: _,
} => {
write!(f, "The dialing attempt was rejected because a valid PeerId could not be constructed from: {:?}", multihash)
}
}
}
}

/// Options to configure a dial to a known or unknown peer.
///
/// Used in [`Network::dial`].
Expand Down