use super::*;
#[derive(Debug, Clone)]
pub struct RelayEventPage {
pub events: Vec<RelayEvent>,
pub through_ordinal: u64,
pub through_digest: String,
}
#[derive(Debug, Clone)]
pub struct RelayCatchUp {
pub state: RelayOperationalState,
pub frontier: RelayCursor,
pub first_page: RelayEventPage,
}
#[derive(Debug)]
pub struct RelayRejected(pub RelayProtocolError);
impl std::fmt::Display for RelayRejected {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
formatter,
"relay rejected request ({:?}): {}",
self.0.code, self.0.message
)
}
}
impl std::error::Error for RelayRejected {}
impl RelayRejected {
pub fn is_desynchronized(&self) -> bool {
self.0.code == RelayErrorCode::Desynchronized
}
pub fn is_retryable(&self) -> bool {
self.0.retryable
}
}
#[derive(Debug)]
pub struct RelayTransportDead {
pub(super) message: String,
pub(super) handshake_failed: bool,
}
impl std::fmt::Display for RelayTransportDead {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str(&self.message)
}
}
impl std::error::Error for RelayTransportDead {}
impl RelayTransportDead {
pub fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
handshake_failed: false,
}
}
pub(super) fn from_io(error: std::io::Error, kind: ExchangeKind) -> Self {
Self::during_exchange(error.to_string(), kind)
}
pub(super) fn during_exchange(message: impl Into<String>, kind: ExchangeKind) -> Self {
Self {
message: message.into(),
handshake_failed: kind == ExchangeKind::Handshake,
}
}
pub fn marks(error: &anyhow::Error) -> bool {
error.downcast_ref::<Self>().is_some()
}
pub fn marks_failed_handshake(error: &anyhow::Error) -> bool {
error
.downcast_ref::<Self>()
.is_some_and(|failure| failure.handshake_failed)
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub(super) enum ExchangeKind {
Handshake,
Call,
}
pub(super) struct ConnectFailure {
pub(super) error: anyhow::Error,
pub(super) transport_rejected: bool,
}
impl ConnectFailure {
pub(super) fn plain(error: anyhow::Error) -> Self {
Self {
error,
transport_rejected: false,
}
}
}
impl From<anyhow::Error> for ConnectFailure {
fn from(error: anyhow::Error) -> Self {
Self::plain(error)
}
}