use std::sync::mpsc::Receiver;
#[cfg(feature = "connection-manager-notification-iter-try-next")]
use std::sync::mpsc::TryRecvError;
#[cfg(feature = "connection-manager-notification-iter-try-next")]
use super::error::ConnectionManagerError;
#[derive(Debug, PartialEq, Clone)]
pub enum ConnectionManagerNotification {
Connected {
endpoint: String,
},
InboundConnection {
endpoint: String,
connection_id: String,
},
Disconnected {
endpoint: String,
},
ReconnectionFailed {
endpoint: String,
attempts: u64,
},
}
pub struct NotificationIter {
pub(super) recv: Receiver<ConnectionManagerNotification>,
}
#[cfg(feature = "connection-manager-notification-iter-try-next")]
impl NotificationIter {
pub fn try_next(
&self,
) -> Result<Option<ConnectionManagerNotification>, ConnectionManagerError> {
match self.recv.try_recv() {
Ok(notifications) => Ok(Some(notifications)),
Err(TryRecvError::Empty) => Ok(None),
Err(TryRecvError::Disconnected) => Err(ConnectionManagerError::SendMessageError(
"The connection manager is no longer running".into(),
)),
}
}
}
impl Iterator for NotificationIter {
type Item = ConnectionManagerNotification;
fn next(&mut self) -> Option<Self::Item> {
match self.recv.recv() {
Ok(notification) => Some(notification),
Err(_) => {
None
}
}
}
}