use std::{fmt::Debug, iter::FusedIterator};
use crate::{ClientHandle, error::ConnectionClosedError, subscribe::{Subscriber, SubscriptionOptions}};
use super::Topic;
#[derive(Debug, Clone)]
pub struct TopicCollection {
names: Vec<String>,
handle: ClientHandle,
}
impl IntoIterator for TopicCollection {
type Item = Topic;
type IntoIter = IntoIter;
fn into_iter(self) -> Self::IntoIter {
IntoIter::new(self)
}
}
impl PartialEq for TopicCollection {
fn eq(&self, other: &Self) -> bool {
self.names == other.names
}
}
impl Eq for TopicCollection { }
impl TopicCollection {
pub(crate) fn new(
names: Vec<String>,
handle: ClientHandle,
) -> Self {
Self { names, handle }
}
pub fn names(&self) -> &Vec<String> {
&self.names
}
pub fn names_mut(&mut self) -> &mut Vec<String> {
&mut self.names
}
pub async fn subscribe(&self, options: SubscriptionOptions) -> Result<Subscriber, ConnectionClosedError> {
Subscriber::new(self.names.clone(), options, self.handle.announced_topics.clone(), self.handle.server_send.clone(), self.handle.client_send.subscribe()).await
}
}
pub struct IntoIter {
name_iter: std::vec::IntoIter<String>,
handle: ClientHandle,
}
impl Iterator for IntoIter {
type Item = Topic;
fn next(&mut self) -> Option<Self::Item> {
self.name_iter.next()
.map(|name| Topic::new(name, self.handle.clone()))
}
}
impl DoubleEndedIterator for IntoIter {
fn next_back(&mut self) -> Option<Self::Item> {
self.name_iter.next_back()
.map(|name| Topic::new(name, self.handle.clone()))
}
}
impl ExactSizeIterator for IntoIter {
fn len(&self) -> usize {
self.name_iter.len()
}
}
impl FusedIterator for IntoIter { }
impl IntoIter {
pub(self) fn new(collection: TopicCollection) -> Self {
IntoIter {
name_iter: collection.names.into_iter(),
handle: collection.handle,
}
}
}