mod identity;
pub use identity::{InvalidNotificationIdentity, NotificationEpoch, NotificationRequestId};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SQLNotification {
pub process_id: i32,
pub channel: String,
pub payload: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NotificationIdentity {
pub epoch: NotificationEpoch,
pub request_id: Option<NotificationRequestId>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum NotificationFailureKind {
InvalidRequest,
Authentication,
AuthorityRevoked,
Unsupported,
Capacity,
Backpressure,
Protocol,
SourceUnavailable,
Transport,
Timeout,
ServerDraining,
Cancelled,
SequenceExhausted,
}
impl NotificationFailureKind {
pub const fn code(self) -> &'static str {
match self {
Self::InvalidRequest => "NOTIFICATION_INVALID_REQUEST",
Self::Authentication => "NOTIFICATION_AUTHENTICATION",
Self::AuthorityRevoked => "NOTIFICATION_AUTHORITY_REVOKED",
Self::Unsupported => "NOTIFICATION_UNSUPPORTED",
Self::Capacity => "NOTIFICATION_CAPACITY",
Self::Backpressure => "NOTIFICATION_BACKPRESSURE",
Self::Protocol => "NOTIFICATION_PROTOCOL",
Self::SourceUnavailable => "NOTIFICATION_SOURCE_UNAVAILABLE",
Self::Transport => "NOTIFICATION_TRANSPORT",
Self::Timeout => "NOTIFICATION_TIMEOUT",
Self::ServerDraining => "NOTIFICATION_SERVER_DRAINING",
Self::Cancelled => "NOTIFICATION_CANCELLED",
Self::SequenceExhausted => "NOTIFICATION_SEQUENCE_EXHAUSTED",
}
}
}
#[derive(Clone, PartialEq, Eq)]
pub enum NotificationEvent {
Notification {
identity: NotificationIdentity,
sequence: u64,
notification: SQLNotification,
},
ResyncRequired {
identity: NotificationIdentity,
cause: NotificationFailureKind,
},
Reconnected {
identity: NotificationIdentity,
},
}
impl std::fmt::Debug for NotificationEvent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Notification {
identity, sequence, ..
} => f
.debug_struct("Notification")
.field("identity", identity)
.field("sequence", sequence)
.finish_non_exhaustive(),
Self::ResyncRequired { identity, cause } => f
.debug_struct("ResyncRequired")
.field("identity", identity)
.field("cause", cause)
.finish(),
Self::Reconnected { identity } => f
.debug_struct("Reconnected")
.field("identity", identity)
.finish(),
}
}
}
#[cfg(test)]
mod tests;